Solved

Malloc assines a NULL pointer with tons of memory left.

Posted on 2004-08-02
6
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

628 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