Memory address size

Pages: 12
So after watching the video I get where the guy is coming from,exactly what you guys were explaining I think I'm on the right track,hear me out

lets say for example we have an array of integers, int arr[5] lets pretend arr[0] starts at memory address 1000 and occupies the space up until and including 1003,so the next int arr[1] will start at 1004, so is this 1004 the same memory address? is it the same memory address but with a different memory position? (If so I'm guessing memory positions are further broken down into chunks of bytes)

also going back to the example program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
    int first = 5; // 4 bytes
    char second = 'A'; // 1 byte

    int third[200]; // 4 * 200 bytes

    int* fourth = new int[300];
    char* fifth = new char[1000]; // 1000 bytes
}


as mentioned before lets say first is at memory address 1000 and takes space at that memory address up until and including 1003,so will the char second be at the same memory address as first? but just as another position in that memory(1004)? or will it be in a totally separate address(if so this seems kind of wasteful)

and does the same go for third an array on the stack.

obviously both fourth and fifth are different as they are on the heap so they will obviously have a different memory address than first,second and third but will fourth and fifth also have separate addresses? or is it possible that fourth could occupy 300 * 4 bytes of the 32 bit address and then fifth occupy the same address after position fourth[300 * 4] ( at a different memory position) or this depend on the OS?

lets say for example we have an array of integers, int arr[5] lets pretend arr[0] starts at memory address 1000 and occupies the space up until and including 1003,so the next int arr[1] will start at 1004, so is this 1004 the same memory address?

1000 != 1004, so no, they are not the same.
1000, 1001, 1002 and 1003 are also not the same address even though they refer to positions within the same object.
It's that simple. Don't overthink it.
Last edited on
True Peter I am 100% over thinking it no doubt lol

but the thing that is just confusing me is that address 1000 which holds the first 8 bits of the integer is just that it is 8 bits in size(the amount the address at that point) can store, so people are saying that memory addresses are 32 bits in size or I should say can store 32 bits of data inside of them but from what I can see is they are capable of storing a byte or 8 bits?

thanks
(If so I'm guessing memory positions are further broken down into chunks of bytes)
Two things to note.
First, a byte is not necessarily eight bits long. A byte is merely the smallest unit of memory that is addressable by the CPU. Therefore, by definition a memory position contains always exactly one byte, although the number of bits it contains depends on the architecture (it could be 1, 8, 12, etc.).
Second, a memory position is not further divisible, except at the electronics level. All you can do with a memory position from software is read it entirely or overwrite it entirely.

lets say first is at memory address 1000 and takes space at that memory address up until and including 1003,so will the char second be at the same memory address as first?
If two variables were to exist at "the same" memory address then their memory regions would overlap, then when you wrote to one of them you'd be indirectly changing the value of the other.

will the char second be at the same memory address as first? but just as another position in that memory(1004)?
These two suppositions are contradictory. If two variables are at the same address (&first == &second) then they occupy the same memory positions. If they occupy different memory positions then they have different addresses.

Case 1 - first and second at same address (1000)
Memory position 1000: [first byte of 'first'] [second] <-- these two values are the same
Memory position 1001: [second byte of 'first']
Memory position 1002: [third byte of 'first']
Memory position 1003: [fourth byte of 'first']

Case 2 - first and second at different addresses (1000 and 1010)
Memory position 1000: [first byte of 'first']
Memory position 1001: [second byte of 'first']
Memory position 1002: [third byte of 'first']
Memory position 1003: [fourth byte of 'first']
(Gap)
Memory position 1010: [second]

or will it be in a totally separate address(if so this seems kind of wasteful)
Why do you think this would be wasteful? Are you perhaps confusing the ideas of "address" and "address space"? An address space is something entirely unrelated to this discussion; it has to do with how the operating system manages virtual memory between different processes.

EDIT:
but the thing that is just confusing me is that address 1000 which holds the first 8 bits of the integer is just that it is 8 bits in size(the amount the address at that point) can store, so people are saying that memory addresses are 32 bits in size or I should say can store 32 bits of data inside of them but from what I can see is they are capable of storing a byte or 8 bits?
Think back to the hotel example. A room number may be seven digits long, but this tells you nothing about how many people can fit in a hotel room.
Likewise, an address may be 32 bits long, but again this tells you nothing about how many bits can be stored at the memory position referenced by a given address.
Last edited on
Ah ok helios now I get what you guys are trying to say,

so as you said a byte is just the smallest unit of memory that is addressable to a computer this could be 6,8,10 but in most cases I'm guessing it is 8, a memory address storage capacity is a byte.

when you say memory positions it could be different memory addresses,as what you said for case 1(first) it will start at memory address 1000 this is the first memory position of that object or data, then 1001 will hold the next byte and this will be the next address and also the next memory position?

I think my wording was wrong I should have said is the storage capacity of an address a 32 bits or 8 bits and ofcourse the answer is a byte.

what people must have thought I meant was how big is a memory address like how many memory addresses are there, for example if a memory address is 8 bits long it can only have an address space between 0-255.

That's more or less correct ^^ right ?
Basically, yes.
thanks guys for sticking with me :)

I think my wording of the question caused confusion
Topic archived. No new replies allowed.
Pages: 12