PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > simple pointer question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
simple pointer question

Réponse
 
LinkBack Outils de la discussion
Vieux 25/11/2007, 23h46   #1
questions?
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut simple pointer question

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?
  Réponse avec citation
Vieux 26/11/2007, 00h04   #2
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple pointer question


"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

  Réponse avec citation
Vieux 26/11/2007, 00h05   #3
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple pointer question

"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.
  Réponse avec citation
Vieux 26/11/2007, 00h14   #4
Ivar Rosquist
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple pointer question

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?




  Réponse avec citation
Vieux 26/11/2007, 00h14   #5
Ben Pfaff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple pointer question

"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;}}}
  Réponse avec citation
Vieux 26/11/2007, 00h17   #6
questions?
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple pointer question

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!
  Réponse avec citation
Vieux 26/11/2007, 06h31   #7
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple pointer question

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.

  Réponse avec citation
Vieux 26/11/2007, 20h51   #8
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: simple pointer question

"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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h48.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15661 seconds with 16 queries