943,972 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 14909
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 5th, 2006
0

reasons why malloc fails?

Expand Post »
hi,

will someone point me the reasons why a call to malloc fails even if we have enough memory ?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
prasath is offline Offline
4 posts
since Nov 2005
Apr 5th, 2006
0

Re: reasons why malloc fails?

Quote originally posted by prasath ...
will someone point me the reasons why a call to malloc fails even if we have enough memory ?
show your code which is failing and make sure you are not passing -1 in malloc.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Apr 5th, 2006
0

Re: reasons why malloc fails?

Every process in Windows NT has one heap called the default heap. The Win32 subsystem uses the default heap for all global and local memory management functions, and the C run-time library uses the default heap for supporting malloc functions.

maaloc can fail as it cant allocate memory bigger than default heap. not all system memory is available to malloc.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Apr 5th, 2006
0

Re: reasons why malloc fails?

The in MS-Windows the default heap is not static -- the os will expand it as needed. But since malloc() takes an size_t integer as the parameter the largest amount of memory malloc can allocate at one time is the largest value that can be stored in the size_t integer (see limits.h). And yes, all the computer's available and unused memory can be used by malloc with the previously mentioned restriction.

malloc() normally fails today for one of two reason: (1) attempt to allocate more memory then is available, or (2) memory has been previously trashed (most common reason) such as buffer overflows and using uninitialized pointers (although there are a whole host of other causes).
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 7th, 2006
0

Re: reasons why malloc fails?

> will someone point me the reasons why a call to malloc fails even if we have enough memory ?
My guess is you're still using crusty old TurboC which is limited to 640K no matter how many GB of memory you have on your pentium powered, XP hosted machine.

"Hello, yes I'd like to buy a Ferrari please"
"Certainly sir, will you be wanting to replace the engine with an elastic band?"
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 27th, 2007
0

Re: reasons why malloc fails?

The in MS-Windows the default heap is not static -- the os will expand it as needed. But since malloc() takes an size_t integer as the parameter the largest amount of memory malloc can allocate at one time is the largest value that can be stored in the size_t integer (see limits.h). And yes, all the computer's available and unused memory can be used by malloc with the previously mentioned restriction.

malloc() normally fails today for one of two reason: (1) attempt to allocate more memory then is available, or (2) memory has been previously trashed (most common reason) such as buffer overflows and using uninitialized pointers (although there are a whole host of other causes).
HI, I am also facing a same problem, " Malloc fails". Later I found the reason why malloc was failing. It was failing due to Buffer Overflow. But I am not able to understand why malloc fails due to Buffer Overflow. Can any one explain what exactly happens when there is a buffer over flow and how does it affect malloc. Thank you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
glmohan2002 is offline Offline
2 posts
since Dec 2007
Dec 27th, 2007
0

Re: reasons why malloc fails?

>Can any one explain what exactly happens when there
>is a buffer over flow and how does it affect malloc.
No, because that's an implementation detail. It depends on how malloc works for your system as well as what you're doing in your code. Post your code that fails (preferably a small example that still fails) and I can describe what you're doing wrong and speculate about what's happening behind the scenes.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 27th, 2007
0

Re: reasons why malloc fails?

Click to Expand / Collapse  Quote originally posted by Narue ...
>Can any one explain what exactly happens when there
>is a buffer over flow and how does it affect malloc.
No, because that's an implementation detail. It depends on how malloc works for your system as well as what you're doing in your code. Post your code that fails (preferably a small example that still fails) and I can describe what you're doing wrong and speculate about what's happening behind the scenes.
The code is very simple. (I am giving the example)
One buffer is defined as "char buf[1001]" and "buf" is assigned with 1000 characters.
Later inside the code I am doing the following..

char *name, *temp;

name = (char *)malloc(500);

strcpy(name, buf);

temp = (char *)malloc(sizeof(char));

In this piece of code, name is allocated with 500 bytes and 1000 bytes are copied into this memory resulting in a buffer overflow. Due to this malloc at "temp = (char *)malloc(sizeof(char));" instruction is failing. I am trying to understand how this buffer overflow is causing a problem to malloc even though there is a sufficient memory is available.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
glmohan2002 is offline Offline
2 posts
since Dec 2007
Dec 27th, 2007
0

Re: reasons why malloc fails?

>I am trying to understand how this buffer overflow is causing a problem
>to malloc even though there is a sufficient memory is available.
It's not about there being sufficient memory. If you've corrupted the memory manager's housekeeping data, it can't properly do its job. A buffer overflow can easily cause that kind of corruption.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 27th, 2007
0

Re: reasons why malloc fails?

Your heap is divided into segments you can have maximum 64 segments and each segment is divided into blocks. The blocks are the actuall memory you allocate.

The first heap block is an allocation of 8 bytes. This block is also a free block.
The second heap block that follows it is an allocation of 16 bytes. This block is not free.

  1. _______________________________________________________________________________________________________________________
  2. |<--------------------------------------------------- HEAP SEGMENT ---------------------------------------------------->
  3. |<------------------- HEAP BLOCK -------------->|<------------------------------ HEAP BLOCK --------------------------->
  4. |<------- header ------>|<----- user data ----->|<------- header ------>|<----------------- user data ----------------->
  5. |s- -- p- -- s- f- u- t- d- d- d- d- d- d- d- d- s- -- p- -- s- f- u- t- d- d- d- d- d- d- d- d- d- d- d- d- d- d- d- d-
  6. |02 00 03 00 03 00 00 00 88 01 07 00 00 fa 0d 0c 03 00 02 00 03 01 0c 00 08 37 e2 7c 48 5e 77 05 01 00 00 00 00 00 00 00
  7. | 0002 0003 0003 0000 0188 0007 fa00 0c0d 0003 0002 0103 000c 3708 7ce2 5e48 0577 0001 0000 0000 0000
  8. | 00030002 00000003 00070188 0c0dfa00 00020003 000c0103 7ce23708 05775e48 00000001 00000000
  9. |
  10. |00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17
  11. |00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27
  12. |<------- header ------>|<----- user data ----->|<------- header ------>|<----------------- user data ----------------->
  13. |_______________________________________________________________________________________________________________________
So in a heap block, the first 8 bytes is the heap header. In my case 02 00 03 00 03 00 00 00 is the heap header of the first block
03 00 02 00 03 01 0c 00 is the heap header of the second heap block.

First four bytes are the size of a heap block Once you run your code you actually corrupt the next heap block. You just overwrite the size. This does not effect functioning of malloc but it corrups the memory, once you try to access that memory you expect a crash due to access denied error ..!!
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Calling two different C program via OS command line
Next Thread in C Forum Timeline: compiler flags





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC