Solved

Malloc assines a NULL pointer with tons of memory left.

Posted on 2004-08-02
6
231 Views
Last Modified: 2010-04-01
char* tempTransMsg;
   tempTransMsg = (char*)malloc(len);  This line returns a NULL pointer even though I have over 600mb of memory left.  len is an unsigned int and is normaly about 750.  This is not the only time malloc is used in this program and it worked the other times.
Thankyou for your time.
0
Comment
Question by:An-Odd-Programmer
6 Comments
 
LVL 22

Accepted Solution

by:
grg99 earned 38 total points
ID: 11699533
You probably overstepped the bounds of one of your previous mallocs.

Check the other places where you fill the malloc()'ed arrays.  Check very carefully that you don't overflow them, not even by one byte.  Remember that strings need an extra byte for the terminating '\0', and array indices run from 0 to len-1.

0
 

Author Comment

by:An-Odd-Programmer
ID: 11710140
This is the first time that I have used it for this variable, but I have used it other times in the program.
0
 

Assisted Solution

by:jjlloorrttiizz
jjlloorrttiizz earned 37 total points
ID: 11719797
When you say "... even though I have over 600mb of memory left..." - I assume that you mean available physical memory on the PC
If that's the case, this sound just like an issue that I also had (My Application's process used less than 100MB, all other Processes totaled less than 300MB, however my the entire PC had over 600 available physical memory out of 1 GB

I also assume that it is a Windows PC
If that's the case  ...
Since Windows OS allows only 2 GB of Virtual Memory per process.  Check the VM size for your process, thru the Task manager, Performance Monitoring(Perfmon), or Programatically via the following:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/globalmemorystatus.asp

My overuse of VM was mainly due to hundreds of threads being created, each allocating the default stack space of 1MB
- This was corrected by using a Linker option with a smaller size (e.g., /stack:0x30000)
- VC++ IDE Location is :  Project Settings->Link Tab->Output Category->Stack Allocations=Reserve

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now