Davlog
Davlog

Reputation: 2238

How much bytes does an address take?

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.

Upvotes: 5

Views: 17760

Answers (6)

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143099

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).

Upvotes: 2

user1770426
user1770426

Reputation: 193

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.

Upvotes: 0

rjp
rjp

Reputation: 1820

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.

Upvotes: 4

Barmar
Barmar

Reputation: 781058

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.

Upvotes: 1

Christian Ternus
Christian Ternus

Reputation: 8492

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.

Upvotes: 2

Jonathan Wood
Jonathan Wood

Reputation: 67203

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.

Upvotes: 0

Related Questions