Malloc keeps returning NULL, and its not an out of memory condition.

Started by
6 comments, last by mfawcett 18 years, 9 months ago
Hello All, Well I'm stumped and my googling skills aren't picking the right keywords to find a solution to my friends dilema. He is having to program in pure C for his class. He uses VS .NET 2003 for the initial development and then gets his program to compile on Unix per his instructors instructions. Not too hard as it is your typical bubble sort/string manipulation exercises. Anyway... anytime he calls malloc more than once, malloc will just start returning NULL. However when he compiles and runs it on the Unix box it works just fine. Here is an example:

void main()
{
    int ii;
    for( ii = 0; ii < 3; ii++ )
        AllocMemory();
}

void AllocMemory()
{
    char * mem_leak = NULL;
    mem_leak = (char *)malloc( sizeof( char ) * 3 ); //3 byte allocation.
    if( !mem_leak )
        printf( "It returned NULL on a 3 byte memory allocation" );
    else
        printf( "Everything is fine and dandy" );
}


So the on the second call it starts returning NULL from then on out. This is a pretty contrived and trivial example but it illustrates the type of code he's written for the most part. I have tried using the CRT's debugging memory features to no avail. I've even used BoundsChecker to see if it would detect a bad allocation/stack corruption. He is using the "Compile as C Code" compiler option as he needs to write pure C and he is linking against the Multithreaded Debug/Release Standard Library DLL. I was wondering if anyone else might have had some strangeness using Visual Studio .NET 2003 and compiling it as pure C. I just don't understand what is going on. It seems to be a Windows only issue at the moment. Any suggestions, advice, or constructive comments are welcome... Thanks for your time and help!
Advertisement
The code you posted works fine for me... sounds like somthing's broken with your friend's C libraries.
I guess I'm asking, has anyone had malloc fail for them other than an out of memory condition. That one would be pretty easy to diagnose. This allocation error is a lot more difficult to track down. Anyone else? Alignment problems? Using dll version of std library vs. static lib?
One time where you can get weird results back from malloc/new is if you've somehow trashed the heap in some way. Generally, writing somewhere you're not supposed to is pretty much guaranteed to cause problems. If you can't track down the problem then you can use _CrtCheckMemory (Windows only AFAIK) to get the debug heap to validate itself and make sure all of its structures are still intact, e.g.

#include <crtdbg.h>
#include <stdio.h>
#include <malloc.h>

#define CHECKMEM if ( !_CrtCheckMemory() ) { printf( "Heap is corrupt!\n" ); return -1; }

int main( int argc, char *argv[] )
{
char *pMem = malloc( sizeof(char)*2 );

CHECKMEM

pMem[0] = 0;
CHECKMEM

pMem[1] = 1;
CHECKMEM

// this will corrupt the heap
pMem[2] = 2;
// this will spot the corruption
CHECKMEM

return 0;
}

You don't supply the code you're actually having problems with, so I can only guess at what's wrong, but I'd hazard a guess such as not allocating enough memory for the data types/structures you're using.

HTH.
I also like to point out that main should be returning int, this is in the C standard. I would hope his teacher would give him a lot of marks off for that goof. Unless your talking grade 8 programming here.

Oh and it worked fine for me and I have VS .NET 2k3 also. Enjoy.
Quote:Original post by Anonymous Poster
I also like to point out that main should be returning int, this is in the C standard.


Not true. It is in the C++ standard, but back in the C era they didn't really think these things through, and now they're stuck with backwards-compatibility issues.
Quote:Original post by Zahlman
Quote:Original post by Anonymous Poster
I also like to point out that main should be returning int, this is in the C standard.
Not true. It is in the C++ standard, but back in the C era they didn't really think these things through, and now they're stuck with backwards-compatibility issues.
'Tis yucky, though. Best to change it to int.
Quote:Original post by ArmitageIII87
I guess I'm asking, has anyone had malloc fail for them other than an out of memory condition. That one would be pretty easy to diagnose. This allocation error is a lot more difficult to track down. Anyone else? Alignment problems? Using dll version of std library vs. static lib?


I saw this usually with my students when they had a previous call to malloc get passed a negative number (underflow leading to size_t's max or something near it). That malloc would succeed, but the next call would fail. In saying that...it was still an out of memory condition, heh, but hopefully it gives you something to look for. Go back to every malloc and ensure that the size you are giving it is appropriate. And be careful of your signed/unsigned arithmetic.
--Michael Fawcett

This topic is closed to new replies.

Advertisement