Archived

This topic is now archived and is closed to further replies.

nosajghoul

technical musings about memory

Recommended Posts

Ok, been really trying to understand memory, and Im stumped on a few things. Seems theres alot more going on than is mentioned in every other tutorial / book Ive ever seen. It all started when I started digging into pointers. A pointer holds the address of another variable. Ok fine, got that. given: int *pInt; &pInt = the address of pInt *pInt = the thing pInt points at pInt = the address of the thing pInt points at so memory has some attributes. It has an address, which is easy to grasp: 0x0001 0x0002 0x0003 0x0004 etc...... 0xFFFF (or similar) And memory can hold a number: 0x0001 holds the value 0x23FC4 etc.... (Not sure of the range of the numbers possible for one ''box'' of memory, or the number of ''boxes'') But memory can ALSO hold the TYPE of value in it somehow: 0x0001 is an int and is the number 32 so each memory location has : a maximum size of its address (maybe something like FFFF FFFF) a maximum value it can hold a number it is currently holding the type of value it is holding Is this an accurate mental model of how it works? Am I missing any major pieces? -Jason

Share this post


Link to post
Share on other sites
You''re right on all the pointers stuff... but as for the underlying architecture, afaik its implementation specific - such that Windows/Mac/Unix/Whatever may well represent it differently. I know the specific memory addressing values can change (depends on the tag/line/entry sizes).

I''m not 100% sure on this, but this is my take (feel free to correct me )... but its the compiler that enforces types - not the memory as such. So you allocate your ''int'' in C/C++ and you''ll get back a 32bit (again, OS dependent) chunk of memory - what you do with it is pretty much upto your compiler.

Likewise with the maximum value it can hold, this is limited by the amount of data allocated, and not by the OS/Compiler/Memory - its just a fact there are only so many combinations of binary numbers that fit in to a ''n-bit'' space.

The OS (or windows) has some method of dividing RAM into code/data segments; and writing one to the other will cause the OS to get all unfriendly with your program

maybe my ramblings will be of some use

Jack

Share this post


Link to post
Share on other sites
jollyjeffers is right.

Memory itself is dead simple, you have an address and a corispoding value. Like a row of boxes marked 1-1000, and a number in each of them, no other information is stored there.

The compiler forces the types itself in code. I dont actualy know assemby but from what ive seen it appears that the actual instruction to load a value from the memory into the processor specifies both the address AND the type.

The maximum value just depends on the type, so you wouldnt need to store that anyway. I have no idea what you mean about maximum address.

Windows or even C++ *may* mark a block with some header info but at the end of the day its still just address and values, just the same as your structs are stored.

As for dividing RAM into code/data segments, this is implimented through protected mode, which you can google if you wish. Unless your writing an os (or dos program maybe) its not important.

Share this post


Link to post
Share on other sites
the above posts are right .. just adding a few little details for you:

1) All "common" general purpose procs (not counting embedded procs, like those in your microwave) use what''s called BYTE ADDRESSED MEMORY ... which simple means each ADDRESS, refers to a BYTE in memory ... an a byte on all current common architectures is 8 bits. A bit is 1 binary number (0 or 1). So there are 256 combinations that can be stored in each byte ...

2) Memory is just the list of boxes (bytes) ... no extra knowledge added. The numbered boxes example is good, but it''s worth driving that concept home ... it''s more like this: imagine you are in a mail room, with UNNUMBERED / UNLABELED boxes lined up end-to-end. Now someone says "Put this package in box 3" ... you would basically count to the third box and drop the package in it ... but if you are standing in front of the third box and got amnesia, there is nothing special about it that makes it "third" ... you''d have to count them again.

3) The original concept of TYPE is something added by the processors and lanuages to allow programmers to write programs that do USEFULL things easier than figuring out exactly what they need to do with what boxes all the time ... an int ... is logically a signed number that takes up 4 boxes (on a 32 bit platform) ... the processor has instructions of how to add one series of 4 boxes to another, in such a way that the result is correct, it even deals with signed numbers correctly, AND can detect if the result is too big or small to fit back into the same sized area (langauge like C hide this ability though).

Share this post


Link to post
Share on other sites