Re: size of pointer variables
- From: Zoran Cutura <zoran.cutura@xxxxxx>
- Date: Wed, 8 Jun 2005 08:15:59 +0000 (UTC)
junky_fellow@xxxxxxxxxxx wrote:
> <snip>
>> > Can you please specify why sizeof(*pstr_node) is better as compared
>> > to sizeof(struct node_t) ? I thought they both are equivalent.
>>
>> To the compiler, yes, they're equivalent. But consider what happens if
>> you change pstr_node from a node_t to a newnode_t. If you use
>> sizeof(node_t), you still pass the size of a node_t, which may be larger
>> or smaller than the size of a newnode_t. This would mean that bzero()
>> only zeroes part of the struct, or writes over its end. If you use
>> sizeof *pstr_node, you always pass in the right size: the size of the
>> object which pstr_node points at.
>>
> <snip>
>
> I wrote a small test program to test your theory. But it doesn't seem
> to
> work.
You missunderstood the above explanations.
>
> #include <stdio.h>
> struct Type1 {
> int iVal1;
> int iVal2;
> char cVal3;
> char cVal4;
> };
> struct Type2 {
> long lVal1;
> long lVal2;
> int iVal3;
> int iVal4;
> char cVal5;
> };
> int main(void)
> {
> struct Type1 strType1;
> struct Type2 strType2;
> struct Type1 * pstrType1;
>
> pstrType1 = &strType1;
> printf("\nsizeof strType1 = %d\n",sizeof(struct Type1));
> printf("sizeof *pstrType1 = %d\n",sizeof(* pstrType1));
>
> pstrType1 = (struct Type1 *)&strType2;
> /* change the underlying structure
> */
> printf("sizeof strType1 = %d\n",sizeof(struct Type1));
> printf("sizeof *pstrType1 = %d\n",sizeof(* pstrType1));
>
> }
>
> # a.out
> sizeof strType1 = 12
> sizeof *pstrType1 = 12
> sizeof strType1 = 12
> sizeof *pstrType1 = 12 <--- even after changing the underlying object
> the size of object remains the same.
The above statement was rather meant to say that when during development
of your program you recognize, that pstrType1 should rather be of type
"struct Type2 *" and you change the declaration/definition of the pointer
rather than the underlaying object, you don't have to check all the
sizeof(struct Type1) uses to see whether they need change or not.
OTOH some might argue that when you change the name of the pointer you
need to change it where ever it is used (so in the sizeofs this would
need work) but this can be done reliably by search and replace commands
of most editors, while you can't simply change every sizeof(TypeBlah).
>
>
> So, it seems to me that if a pointer "p" is declared as type "T" ( T
> *p),
> sizeof(T) or sizeof(*p) should always be same even though the
> underlying object has been changed.
That is correct.
>
> Also, when is the "sizeof" actually calculated ? Is it done at compile
> time
> or at run time ? Is this a function that is compiler built in ?
>
sizeof is calculated at compile time and it ain't a function at
all. It's a operator as is "+" or "*".
--
Z (zoran.cutura@xxxxxx)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
.
- Follow-Ups:
- Re: size of pointer variables
- From: Michael Mair
- Re: size of pointer variables
- References:
- Re: size of pointer variables
- From: junky_fellow
- Re: size of pointer variables
- Prev by Date: Re: Comma operator
- Next by Date: Re: Computation slow with float than double.
- Previous by thread: Re: size of pointer variables
- Next by thread: Re: size of pointer variables
- Index(es):
Relevant Pages
|