0

When you declare variable x of type int in C or C++, I understand it'll take 4 bytes of memory. Therefore 4 memory addresses are allocated for this variable. My question is can I see all four memory address allocated this variable? I know this ins't a typical question, but I am curious. Or is it 100% always just the memory address &x and the 3 next bits? So if it was memory address 1000, the next will be 1001, 1002, and 1003? Does it ever deviate from this?

Also say I set x = 5;

Does this mean in this memory location 1000 to 1002, it'll be filled with zeroes and in 1003, there's a 101 (binary representation of 5) in the rightmost position?

Thank you

Arrow
  • 691
  • 2
  • 7
  • 15
  • 1
    The memory for an `int` is guaranteed to be contiguous, though it's not necessarily 4 bytes. The other question depends on the endianness of your system. – T.C. Aug 31 '14 at 18:27
  • What is [contiguous memory](http://stackoverflow.com/questions/4059363/what-is-a-contiguous-memory-block). – Igor Pejic Aug 31 '14 at 18:28
  • `&x` will give you adress of the first byte – Quest Aug 31 '14 at 18:29
  • Memory is made up of multiple blocks of memory chips. They are organized in a form of 2D matrix, the column represents the memory address while each row is made up of a block of memory that is 4 byte long. A row can have a 32-byte or 64-byte contiguous memory block. An int is 4 byte long so it will only take one address and one block of memory, out of a 32 byte long memory chip. – Juniar Aug 31 '14 at 19:35

3 Answers3

7

An int can be four bytes, though that really depends on the compiler. Hence why there are specific-size types, like uint32_t. But let's say for argument's sake that your int is four bytes.

Yes, the four bytes are consecutive. So you are correct that &x and the next three bytes are the location of your variable.

However, your assumption that the right-most byte is the least significant is only true for big-endian machines. On a little-endian architecture (like the x86), the left-most bytes will contain the 101 to represent the number 5.

T.C.
  • 133,968
  • 17
  • 288
  • 421
chrisaycock
  • 36,470
  • 14
  • 88
  • 125
  • 1
    "An int can be four bytes, though that really depends on the compiler." More like "depends on the *architecture/ABI*". – Kijewski Aug 31 '14 at 18:34
  • Cool! I'm glad you included big endian and little endian, I am taking a machine organization and just started talking about that stuff. Thanks. – Arrow Aug 31 '14 at 18:46
2

Size of int is not always 4 bytes, it is know only that

char <= short <= int <= long <= long long

You can go through each byte of int like this

#include <stdio.h>

void main() {
    int A = 10;
    int *intPtr;
    char *charPtr;
    size_t size = sizeof(int);
    size_t i;

    intPtr = &A;
    printf("%d\n", *intPtr);

    charPtr = (char*) intPtr;
    for (i = 0; i < size; i++) {
        printf("%d\n", *(charPtr++));
    }

}
Ivan Ivanov
  • 2,076
  • 16
  • 33
1

Several inaccuracies:

  1. The size of an int is platform-dependent (although it is indeed 4 bytes in most cases).

  2. What do you mean by "see the address"? If you want to observe it through the debugger, then just go ahead, and you will most likely see the adjacent addresses as well. If you want to access a specific address within an int variable, then you can easily do so with a char pointer:

    int x = 5;

    char* p = (char*)&x;

    You can use p[i] in order to access any address in the range 0<=i<=sizeof(int)-1.

  3. The contents of addresses 1000-1003 depend on the endianess of the underlying HW architecture:

    • On a Big-Endian processor, the contents (from low address to high address) will be 0 0 0 5
    • On a Little-Endian processor, the contents (from low address to high address) will be 5 0 0 0
barak manos
  • 29,648
  • 10
  • 62
  • 114