5

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.

Davlog
  • 2,162
  • 8
  • 36
  • 60
  • Each byte is addressable, but only one address is for the `int`. – Fred Larson Oct 25 '13 at 19:31
  • "Basically my question is how much bytes does a single address take / have?" -> `sizeof(T *)` will tell you that. But your question is not really about the "size of an address". Once again, you are asking why pointer arithmetic works the way it works. –  Oct 25 '13 at 19:31
  • possible duplicate of [Pointer Arithmetic](http://stackoverflow.com/questions/394767/pointer-arithmetic) –  Oct 25 '13 at 19:32
  • @JonathanWood "the size of the address doesn't depend on the size of the data." - Actually, in C, it's not a requirement that `sizeof(T1 *) == sizeof(T2 *)`... –  Oct 25 '13 at 19:33
  • 1
    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. – fvdalcin Oct 25 '13 at 19:35
  • @fvdalcin: The “Unit of address resolution” section in [this page](http://en.wikipedia.org/wiki/Memory_address) lists some word-addressable computers. – Eric Postpischil Oct 25 '13 at 19:59
  • Thanks for the information @EricPostpischil, I've never worked with anything like that :) – fvdalcin Oct 25 '13 at 20:05
  • @fvdalcin Microchip sell over 1 billion PIC processors a year (2013). Would that count as "most"? [Harvard Architecture](http://en.wikipedia.org/wiki/Harvard_architecture) quite often used there. Function addresses increment on 8 and 16 and other bit boundaries. Further, the address must be even (14 (with 2 phantom bits) and 32 (with 8 phantom bits). sizeof int ranges from 1 (not C99 compliant), 2, and 4 bytes. The world of variant processors is much bigger than many realize. – chux - Reinstate Monica Oct 25 '13 at 20:35
  • Yes, I do realize that @chux. Maybe I should change it to "most of the processors I've worked with". I'm reading some stuff on that an there are processors which don't have power-of-2 words, like in this table: http://en.wikipedia.org/wiki/Word_(computer_architecture)#Table_of_word_sizes – fvdalcin Oct 25 '13 at 20:40

6 Answers6

3

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.

rjp
  • 1,760
  • 13
  • 15
  • Thanks! That's an answer I was looking for. – Davlog Oct 25 '13 at 19:41
  • 1
    By definition in C, a `char` is an addressable unit. In the C model, every byte is addressable through a `char` pointer. If the underlying hardware does not support directly addressing its bytes, then the C implementation must either create such addressing (by loading a whole unit and extracting parts of it and conversely by merging parts and storing a whole unit) or must define its `char` size to be that of the hardware unit. There cannot be any padding for `char` object; it always uses one byte, however the C implementation defines a “byte”. – Eric Postpischil Oct 25 '13 at 19:56
  • I will concede that point. I was thinking in terms of structures and single `char` variables (static, stack, global) that may be padded out. In the example code, an `int` pointer is used, though. Does the C standard address this unaligned access of non-`char` types? – rjp Oct 25 '13 at 20:04
2

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.

Community
  • 1
  • 1
Christian Ternus
  • 8,406
  • 24
  • 39
  • 1
    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. – Michael Krelin - hacker Oct 25 '13 at 19:35
  • @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. – Davlog Oct 25 '13 at 20:42
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).

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • `&i+1` is the address of the `int` that would follow `i`. `(char *) &x + 1` is the address of the second byte of `i`. – Eric Postpischil Oct 25 '13 at 19:50
  • @EricPostpischil, sorry, I edited too much. The original was +1`*byte*` and then I removed byte along with stars. Will fix. Wasn't supposed to be avalid expression, but rather an explanation. – Michael Krelin - hacker Oct 25 '13 at 19:52
  • Good analogy, though. Consider a letter posted to "101, Memory St." -- and the postman actually *delivers* it at that address. However, you might never get to see that letter if you only check your mail at the canonical address. (The analogy sadly breaks there: if you write something at the wrong *memory* address, your C program might come crashing down. Houses don't do that.) – Jongware Oct 26 '13 at 00:27
  • @Jongware, memory doesn't either, but postman can if he's totally unprepared and the whole postal service may collapse ;-) – Michael Krelin - hacker Oct 26 '13 at 07:54
1

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

An address refers to the start of the data. So the size of the address doesn't change depending on the size of the data.

The actual size of the address will, however, depend on the platform. On many newer systems, that size will be 64 bits. But we can't say exactly without knowing your platform.

You can use sizeof() in your code to get the size of an address.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

Everything depends on your hardware platform memory organization. In case the memory is organized in 4 bytes cells, the variable which length is bellow or equal to 4 bytes (assuming correct memory adjustment), is hold in just one single memory cells, so it is pointed by only one single address value.

user1770426
  • 173
  • 1
  • 2
  • 12