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.cplus > Re: Strcut padding?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: Strcut padding?

Réponse
 
LinkBack Outils de la discussion
Vieux 24/02/2008, 02h10   #1
wahaha
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Strcut padding?

On Feb 22, 6:22 pm, diligent.sn...@gmail.com wrote:
> On Feb 23, 2:58 am, "webinfin...@gmail.com" <webinfin...@gmail.com>
> wrote:
>
>
>
> > #include <iostream>
> > #include <string>

>
> > using namespace std;

>
> > int main() {
> > char t1;
> > struct t2{
> > char x;
> > };

>
> > cout << sizeof(t1) << " " << sizeof(t2) << endl;

>
> > typedef char& x1;
> > struct x2{
> > x1 x;
> > };

>
> > cout << sizeof(x1) << " " << sizeof(x2) << endl;

>
> > }

>
> > output:
> > 1 1
> > 1 4

>
> > Can anybody tell me why the size of x2 is 4? Why it's not 1?

>
> Hello,
>
> This question was answered here:http://groups.google.com/group/alt.c...+/browse_threa...
> (in particular Karl Heinz Buchegger's answer)
>
> It all comes down to the fact that the implementation of a reference
> is compiler dependent, and a compiler may use a pointer to implement
> reference semantics (which probably explains size of 4 bytes).
>
> Regards.


Thank you. But how it will explain sizeof(char&) = 1 but sizeof(struct
x2) = 4?
  Réponse avec citation
Vieux 24/02/2008, 11h13   #2
Bo Persson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Strcut padding?

wahaha wrote:
> On Feb 22, 6:22 pm, diligent.sn...@gmail.com wrote:
>> On Feb 23, 2:58 am, "webinfin...@gmail.com" <webinfin...@gmail.com>
>> wrote:
>>
>>
>>
>>> #include <iostream>
>>> #include <string>

>>
>>> using namespace std;

>>
>>> int main() {
>>> char t1;
>>> struct t2{
>>> char x;
>>> };

>>
>>> cout << sizeof(t1) << " " << sizeof(t2) << endl;

>>
>>> typedef char& x1;
>>> struct x2{
>>> x1 x;
>>> };

>>
>>> cout << sizeof(x1) << " " << sizeof(x2) << endl;

>>
>>> }

>>
>>> output:
>>> 1 1
>>> 1 4

>>
>>> Can anybody tell me why the size of x2 is 4? Why it's not 1?

>>
>> Hello,
>>
>> This question was answered
>> here:http://groups.google.com/group/alt.c...+/browse_threa...
>> (in particular Karl Heinz Buchegger's answer)
>>
>> It all comes down to the fact that the implementation of a
>> reference
>> is compiler dependent, and a compiler may use a pointer to
>> implement
>> reference semantics (which probably explains size of 4 bytes).
>>
>> Regards.

>
> Thank you. But how it will explain sizeof(char&) = 1 but
> sizeof(struct x2) = 4?


When applied to a reference, the sizeof operator gives the size of the
item referred to. In this case a char.

When applied to a struct, the sizeof operator gives the size of the
struct.


Bo Persson


  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 03h56.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09895 seconds with 10 queries