Re: size of pointer variables



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
.



Relevant Pages

  • Re: komplexes Problem mit Funktionszeigern
    ... Die Funktion gibt einen struct "by value" zurueck und bei der ... am Borland Compiler, da der Microsoft Compiler ... Man kann Argumente fuer eine Funktion z.B auf dem Stack uebergeben. ... Das funktioniert natuerlich nur fuer "Basis Typen" die in ein Register passen. ...
    (de.comp.lang.c)
  • Re: Naming typedefs
    ... problem that a scalar could be returned in the A register ... to have waste precious cpu cycles copying the struct there ... of the hardware or twentieth century compiler technology. ... # to include the header defining it into every other header (assuming ...
    (comp.unix.programmer)
  • Re: assembly language and reverse engineering
    ... "return arguments to struct pointed to by register" and ... assumptions of CDECL-like conventions (also supports STDCALL, ... arg for handling struct return (slightly compiler, and compiler version, ...
    (alt.lang.asm)
  • Re: HardBound and SoftBound (was "The State of Software")
    ... I will explain to you one last time that the problem is not whether a compiler can generate checks for all buffer overflows. ... By the way, in the last example of such code that I saw, it had been changed to use the address of the struct, not of the first element of the struct: ... to take address of struct rather than address of first element. ... such tricks are used to create a pointer which is then passed on. ...
    (comp.arch)
  • Some issues with using sizeof() in template definitions
    ... I have two issues with VC++, which might or might not be related, but both have to do with using the sizeof inside template definitions. ... When using sizeof inside my allocator, such as to assign to an enum value defined in the class, the compiler complains about errors in. ... templatestruct MemberCheck ...
    (microsoft.public.vc.language)