NO

Author Topic: Is there any difference?  (Read 4529 times)

CommonTater

  • Guest
Is there any difference?
« on: October 04, 2010, 09:38:05 PM »
I've always wondered if there is a difference, or if there is a condition in which there is a difference between the size of a type and the size of a variable of a type...
Code: [Select]
int x;
int y;

y = sizeof (INT);
y = sizeof (x);

Would, for example the sizeof a struct ever be different than the sizeof it's type?


JohnF

  • Guest
Re: Is there any difference?
« Reply #1 on: October 04, 2010, 09:42:59 PM »
I've always wondered if there is a difference, or if there is a condition in which there is a difference between the size of a type and the size of a variable of a type...
Code: [Select]
int x;
int y;

y = sizeof (INT);
y = sizeof (x);

Would, for example the sizeof a struct ever be different than the sizeof it's type?



I can't think of any situation where there would be a difference.

EDIT: Why do you ask, have you noticed something odd?

John
« Last Edit: October 05, 2010, 12:49:05 PM by JohnF »

CommonTater

  • Guest
Re: Is there any difference?
« Reply #2 on: October 06, 2010, 10:50:24 AM »
Thank you John.

The reason I asked is that sometimes it's easier to reference the variable itself than the type, which might be defined on other pages or buried in libraires.

I haven't noticed any odd behavior, but didn't want to make common practice without being sure.

JohnF

  • Guest
Re: Is there any difference?
« Reply #3 on: October 06, 2010, 11:45:54 AM »
There's one thing - structs sometimes have padding bytes added by the compiler but doubt that this would effect you.

John

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Is there any difference?
« Reply #4 on: October 06, 2010, 12:15:56 PM »
Just take care of the #pragma pack use.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

CommonTater

  • Guest
Re: Is there any difference?
« Reply #5 on: October 06, 2010, 01:18:45 PM »
Ahhh... now that may want looking into.  I'll have to do some tests I guess.

It's not that I want to make this common practice in my code.  It's just that in a couple of cases I'm using structs and variables that I don't have clear definitions for and have had to use the sizeof(variable) to get around it.  It hasn't been a problem yet... but you do make a good point.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Is there any difference?
« Reply #6 on: October 06, 2010, 02:40:26 PM »
The correct programming practice, and this is ageneral rule, is to always put definitions in one place (normally an .h file). And when I say "definitions" I mean anything user defined or customized!
 In this way the object is unanbiguously defined over the whole project (and if there is an error you have the same everywhere).
The example below shows the correct use of of the packing option.
Code: [Select]
........                         //Some definitions
#pragma pack (push)    //Save previous packing
#pragma pack (x)         //Define required packing
........                        //Packed definitions
#pragma pack (pop)     //Restore previous packing
.......                         //Other stuff
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

CommonTater

  • Guest
Re: Is there any difference?
« Reply #7 on: October 06, 2010, 07:15:28 PM »
Thank you Frankie. 

I've been programming in C for quite a while now and I (hope that I) understand .h and pragma usage...

Now project organization...  that's something else entirely.


nicolas.sitbon

  • Guest
Re: Is there any difference?
« Reply #8 on: October 11, 2010, 11:32:25 AM »
I've always wondered if there is a difference, or if there is a condition in which there is a difference between the size of a type and the size of a variable of a type...
Code: [Select]
int x;
int y;

y = sizeof (INT);
y = sizeof (x);

Would, for example the sizeof a struct ever be different than the sizeof it's type?


VLA (variable length array) is such a condition where sizeof behaves differently and in which case sizeof is evaluated dynamically at runtime. The prefer way is always to choose the variable name version, which is more maintainable.

CommonTater

  • Guest
Re: Is there any difference?
« Reply #9 on: October 14, 2010, 10:27:29 PM »
Thank you Nicolas...