sizeof struct problem

Programming, for all ages and all languages.
Post Reply
FlashBurn

sizeof struct problem

Post 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!
Kemp

Re:sizeof struct problem

Post 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...
FlashBurn

Re:sizeof struct problem

Post 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?
bluecode

Re:sizeof struct problem

Post by bluecode »

You have to cast the pointer to unsigned int/unsigned long add the value and cast it back to the pointer type.
Kemp

Re:sizeof struct problem

Post 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++;
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:sizeof struct problem

Post 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...
Every good solution is obvious once you've found it.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:sizeof struct problem

Post 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>
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:sizeof struct problem

Post 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.
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:sizeof struct problem

Post 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.
Post Reply