When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
75 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
The address is in bytes, not nibbles.
hex is just a convenient way to display a byte
but just because it uses 2 digits doesn't mean that the underlying storage is actually nibbles
you can represent a byte using 3 digits of decimal, doesn't mean the underlying storage is 3 decimal digits
If I do sizeof(char); I'm getting 1. So a char is 1 byte. Which means 2 nibbles -> 2 hex digits.
is the misunderstanding
by "addressed in bytes" we meant that 2 address that are next to each other, with a value difference of 1, are 1 byte away
address 1 is 1 byte above address 0
This was my guess but wanted to verify. Everywhere I looked was referring to a hex digit as a nibble, so I got confused. It wouldn't much make sense to use nibbles
Still, I feel like I'm missing something, so I'll have to give it some thought. Thanks for answering
I think youre confusing memory address and memory contents
inside a byte the hex digit does represent a nibble
but an address is basically just a number
we use hex because its convenient when comparing different addresses since they are always the same size
So the value that represents the memory address has nothing to do with the size of the content?
Is this correct?
it addresses bytes if thats what youre saying
i.e. each step moves 1 byte
not 1 nibble
so if I have the memory af cd 05 a0 ...
and I increment my address, I now have the memory cd 05 a0 ...
the address actually points to the af byte
and then to the cd byte
because addresses in modern day computers work by the byte
I think I'll have to completely get rid of the nibble stuff I have in my mind and just think in terms of bytes. But not sure I understood your example ?
each step of an address steps 1 byte, which is 2 digits of hex
you're welcome to try and use a text encoding that has 1 base 256 digit
but turns out its easier just to use 2 base 16 digits
@fierce edge Has your question been resolved? If so, run !solved :)
Okay from what I understand:
Memory address != memory content
My mind right now thinks of this memory address thing like this:
0x...00 -> memory contents
0x...02 -> memory contents
0x...04 -> ...
0x...06
The memory contents has nothing nothing to do with the addresses
But now I have more questions than before. I think I need to understand more fundamentally how memory works under the hood to fully understand the concept. I'll have to give it some thought, because I can't understand it with just some simple examples. I'd appreciate if you linked me some good resources that explain this topic if you have
Memory is arranged as slots, each slot contains one byte, and they are numbered sequentially as 0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10;...
I think I understand this. And these 0, 1, 2 we actually represent them in hexadecimal
Yes, it's just that.
each slot is increasing the hex digits by 2
No.
The size of the slots does not matter.
Just like numbering of houses doesn't depend on how large those houses are.
So these addresses:
0x7ffc6484adbc, 0x7ffc6484adbd, 0x7ffc6484adbe1
do not represent the actual memory size of the value.
like int, char etc
Yes.
They only identify the slot in the memory.
That's why it's wrong to think of pointers as just addresses.
Ohh I see. A pointer is just an address that points to this "slot", or to be more precise to the start of the slot (is this correct thing to say?). But how the size fits into this?
If I do something similar with int
int nums[] = {1, 2, 3, 4, 5};
and then
printf("%p, %p, %p, %p", &nums[0], &nums[1], &nums[2], &nums[3]);
The memory addresses are being increased by 4
0x7ffe606c5a10, 0x7ffe606c5a14, 0x7ffe606c5a18, 0x7ffe606c5a1c
so the memory address is being affected by the size of the content
No it's not the correct thing to say.
I literally just said this.
A pointer points to an object.
&nums[1] points to the second element of the array.
When using %p, the implementation prints out the address of the pointer, more precisely, the integer identifying the first memory slot that the object the pointer points to occupies in.
the pointer takes into account the same of said thing, to increment correctly. It knows an int is 4 bytes/chars and so the underlying thing works in sizes of that. But thats just an implementation detail of how it might work. Technically c says nothing about that
But that is how its usually implemented
And why you get outputs like this
It's better not to take that as the explanation anyway as the language level mechanic is much more intuitive.
Interesting, because pointer arithmetic totally relies on this mechanic.
What mechanic?
does it?
I wasn't aware
apart from perhaps being able it inspect the bytes via char*
every other operations with pointers takes into account the size
They haven't stated what mechanic they are referring to.
"The address of the pointer". That says to me that this address is not the address of the thing the pointer points to but the address of the pointer itself.
Now I'm starting to get even more confused. Does the %p format do something I don't understand? is the &num address different from what we get with the printed format %p?
I mean the address of the thing the pointer points to.
It's just common to abbreviate that to "the address of the pointer".
Another attempt to understand it.
A memory address it's like an index.
Every address has 1 byte of memory content.
I found this image and I think I'm starting to get it now
Am I correct so far?
We increase the address every time by a nibble( a hex digit ) but that doesn't have anything to do with the value
So again
char is 1 byte
we start from 0x00
and we move to 0x01
int is 4 bytes
we start from 0x00
next is at 0x03
This makes sense
Why the hell am I confused over this
I guess I just needed an example like this.. Thank y'all for the help. I finally figured it out💯
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity