0

i have such an code and this prints the address of local variables. as i understand that int reserve 4 bytes and char reserve 1 byte. when i run the script it shows the address of variables. buf: 006ffdc0 cookie:006ffe1c The question is that if i subtract 006ffe1c-006ffdc0 it should give me 80 bytes, right? but when i calculate this on internet it gives me 92. i am confused here. i kindly ask you where 12 byte added from.

int main(int argc, char **argv) {

int cookie;
char buf[80];

printf("buf: %08x cookie: %08x\n", &buf, &cookie);}
Tom Clerk
  • 11
  • 2
  • *right?* Not right. It's up to implementation. 92 looks like a 16-byte multiple size. – 273K May 25 '22 at 06:33
  • Variables adjacent in code don't need to be adjacent in memory; see for example [this other question](https://stackoverflow.com/questions/58400528/why-two-variables-declared-one-after-another-are-not-next-to-each-other-in-memor) – codeling May 25 '22 at 06:35
  • 2
    automatic variables that aren't members of one class? It's not defined at all. Memory layout is defined ONLY for named class-types. Most likely, cookie has an address only because you asked for an address, it could be a register otherwise, unlike array it isn't guaranteed to have a memory location. – Swift - Friday Pie May 25 '22 at 06:35
  • Addresses should be printed with `%p`. – Goswin von Brederlow May 25 '22 at 13:44

0 Answers0