yamp (yet another malloc problem) 
Author Message
 yamp (yet another malloc problem)

I have the following code fragment

52  printf("diag0\n");
53      if((ptr2=(float *)malloc(nobj*sizeof(float)))==NULL)
54          error("FMEAN:  MALLOC\n");
55  printf("diag1\n");

(of course the numbers are just line numbers from my editor).
At the top of the program, I of course have the following:

    34  #include <malloc.h>

and

    43      float *ptr2;

and furthermore I have checked and nobj is a (relatively) small
integer (54).  Yet, from the diagnostic prints, I know I'm getting
a core dump from the malloc statement!  No error, just a core dump.

I have read the man page and I think I understand what is going on,
but still I have the problem.  Any suggestions?  Thanks for any
help you can send me.


Jim Klavetter
Physics and Astronomy
CSUS



Thu, 24 Oct 1996 14:07:55 GMT  
 yamp (yet another malloc problem)
First, thanks for those of you who pointed me to the solution (or
at least the faq!).  It was indeed a free() problem:  in another
file I was freeing a block after I incremented the pointer.  This
is just a thanks to those who responded and warning to all to 1)
watch those free()'s, and 2) read the faq.

Another thing I learned was the way I was coddled before with a
sparcstation running gcc.  I used this code there with no problem and
now that I have transferred the exact same code to a mac a/ux system
running gcc I see how many mistakes were "covered" by the other
system!

Again, thanks.


Jim Klavetter
Physics and Astronomy
CSUS



Fri, 25 Oct 1996 04:02:36 GMT  
 yamp (yet another malloc problem)

|> I have the following code fragment
|>
|> 52  printf("diag0\n");
|> 53      if((ptr2=(float *)malloc(nobj*sizeof(float)))==NULL)
|> 54          error("FMEAN:  MALLOC\n");
|> 55  printf("diag1\n");
|>
[snipped some]
|> and furthermore I have checked and nobj is a (relatively) small
|> integer (54).  Yet, from the diagnostic prints, I know I'm getting
|> a core dump from the malloc statement!  No error, just a core dump.

It's hidden away in a place where you won't find so quickly, but the good
old faq does have an answer on this one:

faq> 17.22:  What do "Segmentation violation" and "Bus error" mean?
faq>
faq> A:      These generally mean that your program tried to access memory it
faq>         shouldn't have, invariably as a result of improper pointer use,
faq>         often involving uninitialized or improperly allocated pointers
faq>         (see questions 3.1 and 3.2), or malloc (see question 17.23), or
faq>         perhaps scanf (see question 11.3).

In your particular case you probably messed up memory that malloc() uses
for its internal housekeeping. My guess is that you're indexing one or some
other small number off the end of a malloc()ed array.

IMHO it would be a good thing to include a pointer to this Answer in the
section on library functions, something like "12.XX Is my malloc() broken?
It gives me a core dump! A: See question 17.22"

--
                                       ^^

University of Twente                 =x=  \        tel. +31 53 893747
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/

``Look up the words in a dictionary. -- Ian Stewart - Does God Play Dice?
    Mega: big                          (Referring to the use of high-performance
    Flop: failure''                     computers for weather prediction.)



Fri, 25 Oct 1996 19:26:14 GMT  
 yamp (yet another malloc problem)

|> |> I have the following code fragment
|> |>
|> |> 52  printf("diag0\n");
|> |> 53      if((ptr2=(float *)malloc(nobj*sizeof(float)))==NULL)
|> |> 54          error("FMEAN:  MALLOC\n");
|> |> 55  printf("diag1\n");
|> |>
|> [snipped some]
|> |> and furthermore I have checked and nobj is a (relatively) small
|> |> integer (54).  Yet, from the diagnostic prints, I know I'm getting
|> |> a core dump from the malloc statement!  No error, just a core dump.
|>
|> It's hidden away in a place where you won't find so quickly, but the good
|> old faq does have an answer on this one:
|>
|> faq> 17.22:    What do "Segmentation violation" and "Bus error" mean?
[munched]
|>
|> IMHO it would be a good thing to include a pointer to this Answer in the
|> section on library functions, something like "12.XX Is my malloc() broken?
|> It gives me a core dump! A: See question 17.22"

Sorry, I overlooked question 17.23 which deals with exactly this...

--
                                       ^^

University of Twente                 =x=  \        tel. +31 53 893747
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/

``Look up the words in a dictionary. -- Ian Stewart - Does God Play Dice?
    Mega: big                          (Referring to the use of high-performance
    Flop: failure''                     computers for weather prediction.)



Fri, 25 Oct 1996 20:13:47 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Capturing WM_NCPAINT like Winamp/Sonique/Yamp etc.

2. Yet another small problem

3. Yet-another-realloc problem

4. Yet Another Time Problem

5. Yet another newbie problem.

6. Yet another var-arg problem...

7. Yet another SAFEARRAY problem...

8. vc++ 5.0 debugger problems, any solutions yet?

9. easy yet frustrating variable order problem

10. Yet another problem with virtual functions

11. Yet another CString to char* problem

12. Yet another release/debug problem

 

 
Powered by phpBB® Forum Software