20

Given a struct definition that contains one double and three int variables (4 variables in all), if p is a pointer to this struct with a value 0x1000, what value does p++ have?

This is not a homework problem, so don't worry. I'm just trying to prepare for a test and I can't figure out this practice problem. Thanks

This is in C. Yes I want the value of p after it is incremented. This is a 32-bit machine

user950891
  • 953
  • 3
  • 10
  • 13
  • 2
    The result of `p = 0x1000, p++` is `0x1000` as post-increment results in the old value. Is this a trick question or did you mean to ask for the value of `p` *after* `p++` = the result of `++p` = the result of `p + 1`? –  Oct 01 '11 at 23:54
  • 1
    Aside from all that, and assuming what you really wanted was the new value of `p`, and that this is C or C++ (since very few other languages have all of pointers, `struct`, and `int`), the answer is dependent on the size of the struct's members, which is implementation-defined. Nothing says an int has to be 4 bytes; sometimes it's 2, sometimes 8, sometimes some other size altogether. – cHao Oct 01 '11 at 23:59
  • My immediate guess would be that it is incremented by 4 bytes on 32b systems and 8 bytes on 64b. – Radu Oct 01 '11 at 23:59
  • 4
    @Radu: your guess would be wrong -- if `p` is a `mystruct *`, then incrementing it adds `sizeof(mystruct)` to the address. – cHao Oct 02 '11 at 00:03
  • 1
    @Radu - Really? On my 32-bit system, an `int` is 4 bytes and a `double` is 8, so 4 bytes isn't enough to put it past the first variable. – Chris Lutz Oct 02 '11 at 00:15
  • Sorry, was thinking of an array of pointers to the structure. – Radu Oct 02 '11 at 01:44

5 Answers5

30
struct foobar *p;
p = 0x1000; 
p++;

is the same as

struct foobar *p;
p = 0x1000 + sizeof(struct foobar);
tangrs
  • 9,709
  • 1
  • 38
  • 53
5

The answer is that it is at least

sizeof(double) + (3*sizeof(int))

Thew reason it's "at least" is that the compiler is more or less free to add padding as needed by the underlying architecture to make it suit alignment constraints.

Let's say, for example, that you have a machine with a 64-bit word, like an old CDC machine. (Hell, some of them had 60-bit words, so it would get weirder yet.) Further assume that on that machine, sizeof(double) is 64 bits, while sizeof(int) is 16 bits. The compiler might then lay out your struct as

| double     | int | int | int | 16 bits padding |

so that the whole struct could be passed through the machine in 2 memory references, with no shifting or messing about needed. In that case, sizeof(yourstruct_s) would be 16, where sizeof(double)+ (3*sizeof(int)) is only 48 14.

Update

Observe this could be true on a 32-bit machine as well: then you might need padding to fit it into three words. I don't know of a modern machine that doesn't address down to the byte, so it may be hard to find an example now, but a bunch of older architectures ould need this.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • Note that compilers generally will add padding such that the struct members is suitable aligned when you make an array of your struct. if the first member here is a double, it'll be aligned such that all elements in an array of that struct start at an address suitably aligned for doubles. (and there might be padding added for the other elements as well so they end up nicely on an address according to their type) – nos Oct 02 '11 at 00:57
  • 1
    @ChrisLutz I'm a mathematician, not an accountant. You're right of course. – Charlie Martin Oct 02 '11 at 02:10
5

Pointer arithmetic is done in units of the size of the pointer type.

So if you do p++ on a pointer to your struct, p will advance by sizeof *p bytes. i.e. just ask your compiler for how big your struct is with the sizeof operator.

nos
  • 223,662
  • 58
  • 417
  • 506
3
p = p + sizeof(YourStruct)

The compiler is free to decide what sizeof will return if you don't turn padding off.

James
  • 9,064
  • 3
  • 31
  • 49
  • 6
    You should add a cast to `char *` to make it clearer that you're doing byte-level pointer arithmetic rather than C-level pointer arithmetic. – Chris Lutz Oct 02 '11 at 00:52
0

A Increment in base address of a data type is equal to base address + sizeof(data type)