0
#include<stdio.h>
int main()
{
int x,y;
char m,h;
int e,f;
int j;
printf("%u\n",&x);
printf("%u\n",&y);
printf("%u\n",&m);
printf("%u\n",&h);
printf("%u\n",&e);
printf("%u\n",&f);
printf("%u\n",&j);
return 0;
}

output:

1804449352
1804449348
1804449347
1804449346
1804449340
1804449336
1804449332

it's depend on os or language

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 9
    The compiler can store the variables however it likes. There are no requirements on the order (or that they are packed closely together). – BoP Apr 24 '23 at 12:47
  • 4
    You should use %p to format pointers. – xihtyM Apr 24 '23 at 12:49
  • 2
    The gap is 2, not 1. The integers need to be aligned. The correct format for printing addresses is `%p` and the address should be a `void *`. – Jonathan Leffler Apr 24 '23 at 12:49
  • https://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4-bytes Might answer half of your question – vvv444 Apr 24 '23 at 12:51
  • @JonathanLeffler *on that very specific platform* the alignment **seems to be** 8 byte. But as BoP correctly said, nothing says that these are variables are actually packed sequentially. Also, I'm almost certain that the platorm Ashutosh compiles for doesn't have an integer alignment requirement stricter than 1B; the compiler just decides, for nicer performance/easier loads, to do this alignment. – Marcus Müller Apr 24 '23 at 12:52
  • 4
    It is not true that "In C language int has 4 byte". Sometimes that is true, but often it is not. In C, an integer has `sizeof(int)` bytes. That value changes. – William Pursell Apr 24 '23 at 12:53
  • @MarcusMüller: how do you deduce an 8-byte alignment. Except for the two characters, the addresses are multiples of 4 with a gap of 4 between printed values. – Jonathan Leffler Apr 24 '23 at 12:55
  • @JonathanLeffler brain-error on my end, but the alignment is probably still not necessarily 4B, just more elegantly so. – Marcus Müller Apr 24 '23 at 12:57
  • @MarcusMüller: you will note that I merely said that integers must be aligned. I did not stipulate what the alignment must be as that varies with the hardware and o/s conventions and the compiler. Plain `int` can be as small as 2 bytes and still be standard C. In principle, they could be 8 bytes long. But 4 is normal. – Jonathan Leffler Apr 24 '23 at 13:01
  • but that's the thing, on most platform there's no such alignment requirement. You can deal with the integer in `struct __attribute__((packed)) { char a; int b; } a_char_and_an_int` just as you could deal with any other integer, and it will be at the oddest address you'll want to find. – Marcus Müller Apr 24 '23 at 13:05
  • OT: Printing a pointer correct is `printf("%p\n",(void*)&x);` – Support Ukraine Apr 24 '23 at 13:15
  • @SupportUkraine xihtyM already said so, second comment from the top. – Marcus Müller Apr 24 '23 at 13:22
  • Does this answer your question? [Is the size of C "int" 2 bytes or 4 bytes?](https://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4-bytes) – Gereon Apr 24 '23 at 13:23

1 Answers1

7

The layout of variables in memory is an implementation detail. Not only can it can differ from one compiler to another, but also in the same compiler based on optimization level, the number of variables, etc.

Unless you're attempting to exploit vulnerable code, you shouldn't write code that depends on the layout of variables.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Variables might even be located in registers only and not in memory. – vvv444 Apr 24 '23 at 12:52
  • 5
    @vvv444 not in this case, though, when you get the address of one, you "force" the compiler to assign a memory location. – Marcus Müller Apr 24 '23 at 12:56
  • 2
    ... and conversely, even though `register` storage class does not force the compiler to actually keep the variable in a register, it *does* make it invalid for you to apply the unary `&` operator to that variable. – John Bollinger Apr 24 '23 at 13:00