reasons why malloc fails?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2005
Posts: 4
Reputation: prasath is an unknown quantity at this point 
Solved Threads: 0
prasath's Avatar
prasath prasath is offline Offline
Newbie Poster

reasons why malloc fails?

 
0
  #1
Apr 5th, 2006
hi,

will someone point me the reasons why a call to malloc fails even if we have enough memory ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: reasons why malloc fails?

 
0
  #2
Apr 5th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: reasons why malloc fails?

 
0
  #3
Apr 5th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: reasons why malloc fails?

 
0
  #4
Apr 5th, 2006
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).
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: reasons why malloc fails?

 
0
  #5
Apr 7th, 2006
> 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?"
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2
Reputation: glmohan2002 is an unknown quantity at this point 
Solved Threads: 0
glmohan2002 glmohan2002 is offline Offline
Newbie Poster

Re: reasons why malloc fails?

 
0
  #6
Dec 27th, 2007
Originally Posted by Ancient Dragon View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: reasons why malloc fails?

 
0
  #7
Dec 27th, 2007
>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.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2
Reputation: glmohan2002 is an unknown quantity at this point 
Solved Threads: 0
glmohan2002 glmohan2002 is offline Offline
Newbie Poster

Re: reasons why malloc fails?

 
0
  #8
Dec 27th, 2007
Originally Posted by Narue View Post
>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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: reasons why malloc fails?

 
0
  #9
Dec 27th, 2007
>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.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: reasons why malloc fails?

 
0
  #10
Dec 27th, 2007
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 ..!!
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 8645 | Replies: 16
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC