Solved

Malloc assines a NULL pointer with tons of memory left.

Posted on 2004-08-02
6
260 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

821 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