If I have some array of ints, let’s just say:
int iarr[5] = {0, 7, 3, 12, 99};
Which is present at address 0xbfdf53a8; and I want to print out the values of this with a loop, I can do something like this:
for(i=0; i<5; i++)
printf("value of iarr[%d] = %d\n", i, *(iarr+i));
This loop works because we’re incrementing by one “int” each time (the address is bumped by 4 bytes each iteration on a typical 32-bit machine).
My question is, when is the type specified byte offset applied? Is that part of the compile process (meaning it’s in the assembly somewhere), or is that done at the OS level?
I would assume how this is handled in a standardized manner, but I haven’t been able to find that answer yet.
It’s part of the compilation process, the compiler generates code that takes the size of the operands into account.
Note that you can increment and do arithmetic with a pointer to a 5412-byte
structjust as easily as a pointer to a 4-byteint. The compiler inspects the type of the pointer expression and generates the proper code.The operating system is not involved at all in these low-level details, it’s just code in the process from it’s perspective.