Tuesday, July 28, 2009

Visual C malloc() returns NULL but GlobalMemoryStatus() says there are 1.7GB memory available?

I'm writing DLL in Visusal C/C++ (6.0) using malloc(). When it returns NULL, I call GlobalMemoryStatus() to see memory status. It tells me there are 1.7GB physical memory available (2GB total). My malloc() only tries to allocate about 200 Bytes. Why it fails?





Also, the machine has 4GB physical memory and we set 30GB virtual memory. GlobalMemoryStatus() indicates only 2GB physical and 2GB virtual (and virtual is running low).





Is there a limit of 2GB boundary for code developed using Visual Studio 6.0?

Visual C malloc() returns NULL but GlobalMemoryStatus() says there are 1.7GB memory available?
Actually visual c++ 6.0 is dumb, i too faced some kind of unexpected behavior in it. A variabled 'i' of type 'unsigned long' that was used in a loop was unable to store values more than 62942, when the next i++ occurs, i = 61700, in that way it became an infinite loop! i switched to vc++ 2005, its fine.





ok now.


try using:





ptr = (type-cast)(new char[size]);





instead of :





ptr = (type-cast)(malloc(size));





because in malloc(size_t size) u'll give the size in bytes, sizeof(char) is also 1 byte, why dont u try this?





for cleaning up allocated memory, use:





delete [size] ((char *)ptr);





make sure u do a char * type cast, bcoz 'size' is number of elements and not number of bytes, so u'll end up in prob.





I recommend you to switch to vc++ 2005. free 90 day trial of visual studio pro edition is available for download. it comes with msdn. it also supports .NET. i'm sure you'll like it more than 6.0


hope this helps you


good luck
Reply:Well, first of all, your math isn't making sense here. You say that you have 2 GB physical memory available and it is showing that 1.7 GB is left, so you only allocate about 300 Mega Bytes (MB) not 200 bytes or MB. First thing to realize is that unless this is a full blown Win32 executable running in a GUI, Which it doesn't sound like since malloc() is involved, you are only working in a VIRTUAL MACHINE. That is to say, that Windows whatever flavor is doling out a certain amount of memory to your application and then saying "NO MORE!". It sounds to me like you're working in a console environment with older C routines that aren't used in Win32. You'll have to decide if that's a problem for your application or not.





I hope this helps you along.

flower beds

No comments:

Post a Comment