#Trying to understand memory representation in hex.

75 messages · Page 1 of 1 (latest)

mystic hamletBOT
#

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.

terse torrent
#

The address is in bytes, not nibbles.

wispy coral
#

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

fierce edge
#

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

wispy coral
#

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

fierce edge
#

So the value that represents the memory address has nothing to do with the size of the content?

#

Is this correct?

wispy coral
#

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

fierce edge
#

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 ?

wispy coral
#

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 kekw but turns out its easier just to use 2 base 16 digits

mystic hamletBOT
#

@fierce edge Has your question been resolved? If so, run !solved :)

fierce edge
#

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

terse torrent
#

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

fierce edge
terse torrent
#

Yes, it's just that.

fierce edge
#

each slot is increasing the hex digits by 2

terse torrent
#

No.

#

0 in decimal is 0 in hexadecimal, 1 in decimal is 1 in hexadecimal.

fierce edge
#

So each hex digit is being represented by 8bits

#

This is what I'm getting

terse torrent
#

No.

#

The size of the slots does not matter.

#

Just like numbering of houses doesn't depend on how large those houses are.

fierce edge
#

So these addresses:

0x7ffc6484adbc, 0x7ffc6484adbd, 0x7ffc6484adbe1

do not represent the actual memory size of the value.

#

like int, char etc

terse torrent
#

Yes.

#

They only identify the slot in the memory.

#

That's why it's wrong to think of pointers as just addresses.

fierce edge
#

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

terse torrent
#

No it's not the correct thing to say.

terse torrent
#

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.

wispy coral
#

But that is how its usually implemented

#

And why you get outputs like this

terse torrent
#

It's better not to take that as the explanation anyway as the language level mechanic is much more intuitive.

steep zodiac
#

Interesting, because pointer arithmetic totally relies on this mechanic.

terse torrent
#

What mechanic?

wispy coral
#

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

terse torrent
#

They haven't stated what mechanic they are referring to.

fierce edge
terse torrent
#

I mean the address of the thing the pointer points to.

#

It's just common to abbreviate that to "the address of the pointer".

fierce edge
#

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

mystic hamletBOT
#

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