Solved

Malloc assines a NULL pointer with tons of memory left.

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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