Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Malloc assines a NULL pointer with tons of memory left.

Posted on 2004-08-02
6
Medium Priority
?
279 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 152 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 148 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

916 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