How much bytes does an address take?

13,188

Solution 1

The sizes of the built-in types (char, short, int, long) are implementation specific and platform specific. If we assume your int is 32 bits, then we can address some of your questions:

If i resides at 0xdeadbeef, then 0xdeadbeef, 0xdeadbef0, 0xdeadbef1, and 0xdeadbef2 byte addresses would all be used to store i. If you were to set iPtr to 0xdeadbeee and write a value, 0xdeadbeee and the following three addresses would then contain the value you wrote. If you then attempt to read c or i, you would find the value corrupted.

Some things to consider: not all architectures allow byte addressing. A char may be one byte on your system, but due to limitations, 4 bytes may be reserved. Likewise, you may not be able to read or write a pointer that points to non-aligned addresses. For example, a system that can only access memory on 32 bit boundaries could only access 0xdeadbeec or 0xdeadbef0.

Solution 2

Yes, the address &i+1byte is an address of the second byte of i.

If you live on Memory Street 100 in 4 houses, you have four addresses. But those addresses address different buildings. Although, depending on your postal service the mail may not be delivered if it's not the canonical address (the same goes for memory access — depends on the platform).

Solution 3

How could you find this out? How about:

printf("%zu\n", sizeof(iPtr));

But, as @H2CO3 points out, you're really asking about pointer arithmetic. Read up more on that for more info.

Solution 4

You can find out how many bytes a pointer takes by using sizeof:

size_t int_ptr_size = sizeof(int*);

If you try to access data through a pointer that isn't properly aligned for the type, you invoke undefined behavior, so it's unpredictable what will happen. On some architectures, the program will crash with a Bus Error.

Share:
13,188

Related videos on Youtube

Davlog
Author by

Davlog

Learning PHP, HTML, C / C++, Python.

Updated on June 04, 2022

Comments

  • Davlog
    Davlog almost 2 years

    Basically my question is how much bytes does a single address take / have?

    I mean a char takes 1 byte on my platform and has 1 address. But an int takes 4 bytes. How many addresses does this int take? Does it still have only 1 address or does it have 4?

    For example :

    char c = 'A'; //Address at 0xdeadbeee
    int i = 45846; //Address at 0xdeadbeef
    int* iPtr = &i;
    iPtr++; //Address at 0xdeadbef3 now
    

    What happens with the addresses between 0xdeadbeef and 0xdeadbef3? Are they all reserved for i? What happens to i when I point to 0xdeadbeee(should be exactly one address | byte or whatever under i) and change it's value?

    Edit: for those who will still answer, I don't want to know how big an integer is. I want to know if it has also 4 addresses when taking 4 bytes of memory and what happens (if it has 4 addresses) when changing one of these addresses' value.

    I hope it's clearer now.

    • fvdalcin
      fvdalcin over 10 years
      And on most processors (does anyone have a counter example?), the memory is byte oriented, i.e. each byte has an unique address. So, literally speaking, an int does have 4 addresses.
  • Michael Krelin - hacker
    Michael Krelin - hacker over 10 years
    Although this is a correct answer to the question how many bytes does the address have, further prose suggests that it's not what the OP really wanted to ask.
  • Davlog
    Davlog over 10 years
    @ChristianTernus If I'm not wrong I get the size of iPtr which is 8 bytes on my platform. I already know the sizes. My question was if it has as many addresses as bytes.