Thread: malloc returning NULL with plenty of memory

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    malloc returning NULL with plenty of memory

    ive run into a problem that i have no idea how to debug.

    malloc is returning NULL on this line:

    Code:
    scriptString = (char *)malloc( (iFileBufferCounter * sizeof(char)) + 1 );
    there is a lot of code before and after, but ive run it through my debugger and when it hits the line that it fails, iFileBufferCounter is equal to 157. last time i checked i had 300+ megs of available memory while the program was running, so i know thats not the problem.

    anyone know of any other possibilities? if you need more info, let me know.

    using MSVC++.NET compiler, windows XP pro, p4 2.0, 512 RAM
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    did you include stdlib.h?

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Remove
    (char *)
    In C there is no reason to cast void pointers like that.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'd say that you've probably corrupted the malloc memory pool sometime earlier in your program, and now you're just seeing the after effect.

    The other usual effect is that the program crashes inside malloc itself, with some random looking memory address violation.

    Read about "malloc debug" in your manual pages. VC.NET has some extensive debug tools for checking the integrity of the memory pool, and putting deadzones around each block it allocates, to detect under and overwrites.

    Easy thing to do is check all your malloc calls have got a sizeof in them.

    > scriptString = (char *)malloc( (iFileBufferCounter * sizeof(char)) + 1 );
    The +1 also needs to be *sizeof char as well, not that it matters. If it were an array of int, you'd be in trouble.
    To insulate from type changes, do this
    scriptString = malloc( (iFileBufferCounter+1) * sizeof *scriptString );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  3. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  4. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM