Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    59
    Thanks
    3
    Thanked 0 Times in 0 Posts

    [C] core dumping on calloc()

    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:

    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?

    Darren
    Last edited by Darren; 11-02-2007 at 12:25 AM.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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
    Last edited by ralph l mayo; 11-02-2007 at 06:00 PM.

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    59
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

    Darren
    Attached Files Attached Files

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •