View Full Version : [C] core dumping on calloc()

11-02-2007, 12:16 AM
I need a hint here. I've never encountered a situation where malloc or calloc failed to return a requested allocation. According to the man pages, if they can't allocate the memory, they return a null pointer. Well, that's not what is happening. I have the following snippet of code:

char *myfunc()
char *output;

printf( "debug 1\n" );
output = (char*)calloc( 1000, sizeof( char ) );
printf( "debug 2\n" );

return output;
}There are many more function in the same source file, but nothing that looks like it ought to have an effect here. Everything compiles without even a warning. The output of the program is as follows:

debug 1
Segmentation Fault(coredump)

The program never reaches the statement "debug 2", so I have to assume that the calloc statement is causing the core dump. If I chop this function out and make it a program all by itself, it compiles and runs fine. I've programmed in C for years and years. I've never encountered this. Anyone got a clue as to what could cause this?


ralph l mayo
11-02-2007, 05:58 PM
You're mismanaging memory elsewhere and confusing the allocator, this is just where it happens to catch up with you.

Compile with debugging symbols and run it under valgrind or -lefence and you should hopefully get some tips on what the initial cause is.

It's bad form to cast the return pointer from allocators as well.

edit: google says to look particularly for a doubled free with this symptom

11-05-2007, 06:41 PM
Ok... here's the bad news (I have no good news)...

We don't have valgrind or the efence library.

There is no free() being executed--I'm only testing functionality right now.

The file aes.c and aes.h are reduced versions of the xyssl files freely available online; while the prog3.c file is just a temporary main for testing.

Let me go ahead and attach full files and maybe you can spot what I'm doing wrong.


ralph l mayo
11-05-2007, 08:16 PM
It works fine for me as long as the second parameter is 16 characters long. Any more or less and there are ton of uninitialized reads and writes.