|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Let's say;
char * first, *second; first=malloc(10*sizeof(char)); second=malloc(100*sizeof(char)); what would be the difference between first and second other than the difference in the address. I mean, when I use free() function to free the memory, how does the system know how large the block to free? Where is the information about size stored? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"questions?" <universal_used@hotmail.com> wrote in message > Let's say; > > char * first, *second; > first=malloc(10*sizeof(char)); > second=malloc(100*sizeof(char)); > > what would be the difference between first and second other than the > difference in the address. > I mean, when I use free() function to free the memory, how does the > system know how large the block to free? > Where is the information about size stored? > The trick is to store information in the space immediately before the pointer. free() can then subtract a few bytes, and get all the information it needs. However modern systems use a variety of techniques. See my "memory games" chapter of Basic Algorithms, which is free, to understand a few simple allocation strategies. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"questions?" <universal_used@hotmail.com> writes:
> char * first, *second; > first=malloc(10*sizeof(char)); > second=malloc(100*sizeof(char)); > > what would be the difference between first and second other than the > difference in the address. > I mean, when I use free() function to free the memory, how does the > system know how large the block to free? > Where is the information about size stored? You have asked a FAQ: http://c-faq.com/malloc/freesize.html -- Ben. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Mon, 26 Nov 2007 00:04:27 +0000, Malcolm McLean wrote:
> "questions?" <universal_used@hotmail.com> wrote in message >> Let's say; >> >> char * first, *second; >> first=malloc(10*sizeof(char)); >> second=malloc(100*sizeof(char)); >> >> what would be the difference between first and second other than the >> difference in the address. >> I mean, when I use free() function to free the memory, how does the >> system know how large the block to free? Where is the information about >> size stored? >> > The trick is to store information in the space immediately before the > pointer. free() can then subtract a few bytes, and get all the > information it needs. > > However modern systems use a variety of techniques. See my "memory > games" chapter of Basic Algorithms, To the OP: Please, don't. McLean's text has been shown in this group to be so riddled with conceptual errors that chances are that whatever you read in it will contain at least one. > which is free, It has to be: Who would pay to publish such a collection of errors? |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
"questions?" <universal_used@hotmail.com> writes:
> I mean, when I use free() function to free the memory, how does the > system know how large the block to free? This is in the FAQ. 7.26: How does free() know how many bytes to free? A: The malloc/free implementation remembers the size of each block as it is allocated, so it is not necessary to remind it of the size when freeing. 7.27: So can I query the malloc package to find out how big an allocated block is? A: Unfortunately, there is no standard or portable way. -- char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[] ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p =b,i=24;for( +=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}} |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Nov 25, 4:05 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> "questions?" <universal_u...@hotmail.com> writes: > > char * first, *second; > > first=malloc(10*sizeof(char)); > > second=malloc(100*sizeof(char)); > > > what would be the difference between first and second other than the > > difference in the address. > > I mean, when I use free() function to free the memory, how does the > > system know how large the block to free? > > Where is the information about size stored? > > You have asked a FAQ:http://c-faq.com/malloc/freesize.html > > -- > Ben. Thank you guys. Good to know the trick, Awesome! |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
In article
<8faf5686-0204-4984-a340-2306d015bac0@e6g2000prf.googlegroups.com>, questions? <universal_used@hotmail.com> wrote on Monday 26 Nov 2007 5:47 am: > On Nov 25, 4:05 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote: >> "questions?" <universal_u...@hotmail.com> writes: >> > char * first, *second; >> > first=malloc(10*sizeof(char)); >> > second=malloc(100*sizeof(char)); >> >> > what would be the difference between first and second other than >> > the difference in the address. >> > I mean, when I use free() function to free the memory, how does the >> > system know how large the block to free? >> > Where is the information about size stored? >> >> You have asked a FAQ:http://c-faq.com/malloc/freesize.html > Thank you guys. Good to know the trick, Awesome! What trick? That FAQ in essence says that you (the programmer) should not depend on the details of the implementation, unless necessary. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
"santosh" <santosh.k83@gmail.com> wrote in message
> >> Thank you guys. Good to know the trick, Awesome! > > What trick? > > That FAQ in essence says that you (the programmer) should not depend on > the details of the implementation, unless necessary. > The question was "how is this magic achieved?". The FAQ doesn't answer that. It tells you how to use malloc(), and that there is no portable way of retrieving the size. But that's a slightly different question. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm |
|
![]() |
| Outils de la discussion | |
|
|