Page 1 of 1

sizeof struct problem

Posted: Thu Aug 17, 2006 10:15 am
by FlashBurn
I have a structure which has 3 ints and if I print the sizeof value of this structure I get 12 which is right. But if I do the following:

Code: Select all

struct foo_t *act;

act+= sizeof(struct foo_t);
It doesn?t add the size of the structure instead it adds some other value (144 for a 12byte structure and 64 for a 8byte structure). Why doesn?t the code work?

I also tried:

Code: Select all

struct __attribute__((__packed__)) foo_t
{
int a,b,c;
};
This doesn?t help, too :( I use gcc 3.4.5!

Re:sizeof struct problem

Posted: Thu Aug 17, 2006 10:43 am
by Kemp
Adding 1 to a pointer will increment it by the size of what it points to, so adding sizeof(blah) to it will add that many sizes to it. Note that 12 x 12 = 144...

Re:sizeof struct problem

Posted: Thu Aug 17, 2006 10:53 am
by FlashBurn
Ok, thanks I?m not used to C programming, normaly I write my code in asm.

So how is the right code when I want to use the value of a pointer (the address) and want to add some other value?

Re:sizeof struct problem

Posted: Thu Aug 17, 2006 12:52 pm
by bluecode
You have to cast the pointer to unsigned int/unsigned long add the value and cast it back to the pointer type.

Re:sizeof struct problem

Posted: Thu Aug 17, 2006 1:04 pm
by Kemp
Well if you simply want to get a working version of your posted code

Code: Select all

struct foo_t *act;
act+= sizeof(struct foo_t);
just change it to

Code: Select all

struct foo_t *act;
act+= 1;
or indeed act++;

Re:sizeof struct problem

Posted: Thu Aug 17, 2006 1:19 pm
by Solar
bluecode wrote: You have to cast the pointer to unsigned int/unsigned long add the value and cast it back to the pointer type.
In C99, you have the type intptr_t (located in <stdint.h>) for this, so you don't have to worry about whether a pointer fits into int or long or long long...

Re:sizeof struct problem

Posted: Thu Aug 17, 2006 3:14 pm
by Pype.Clicker
when you're doing <pointer> + <offset>, always keep in mind that the behaviour works the same way as <array>[<offset>]. If you have an array of dwords, you expect array[1] to be 4 bytes farther than array[ 0 ], right ? well the very same say array+1 is far bytes farther than array and (&(array[1]) - &(array[ 0 ])) is one (not four).

And yes, that's something typically disturbing when you come from ASM and that even made me write a function pointer_offset(ptr,offset_in_bytes) when i wrote my first C program ...

<edit: damn [ 0 ] thing>

Re:sizeof struct problem

Posted: Thu Aug 17, 2006 10:31 pm
by Solar
Hmmm, 'twas late yesterday...

When I'm not tired, I wouldn't cast pointer to integer, but rather cast the pointer I have to (char *) to add the required value.

Re:sizeof struct problem

Posted: Thu Aug 17, 2006 11:12 pm
by Candy
FlashBurn wrote: Ok, thanks I?m not used to C programming, normaly I write my code in asm.

So how is the right code when I want to use the value of a pointer (the address) and want to add some other value?
Well... if you want to actually do plain math with pointers, you'll have to cast it either to a char * or to an intptr_t (at your preference) and then do the math.

The idea behind the C behaviour is that you have a pointer to a type X, which is an array of X. Indexing between these is not really useful, so you can index each object by adding a number indicating the number of objects to skip. For all normal programs, this is good.

If you're interfacing with normal code and not actually hacking around some normal code, I would cast it to an intptr_t for clarity that you're messing about with pointers as ints (and not a char * which you're seeking in) and that it's easier to search for "intptr_t" than for "char" if you want to remove it again.