Memory Part 2. (Memory Storage)

BlitzPlus Forums/BlitzPlus Programming/Memory Part 2. (Memory Storage)

Jono(Posted 2005) [#1]
This is what I've compiled so far:

Bit = 0 or 1

Bit = 1
1 Byte = 8 Bits
1 KiloByte = 1024 Bytes
1 MegaByte = 1024 KiloBytes
1 GigaByte = 1024 MegaBytes

Integer = 4 Bytes
Small Integer = 2 Bytes

I'm working with 1KB of memory, hehe. I would show the diagrams but I don't know how to display images. Just a few questions.

How many bytes is a string variable?
What does a byte address look like?
What happens if I want more variables than there is space in the computers memory?
Why 1024, and not 1000?

Thanks all in advance. :)


Beaker(Posted 2005) [#2]
A string is one byte per character plus anywhere from 1 to 4 bytes extra (depending on language).

What do you mean: what does a byte address look like?

The computer will swap memory in and out of the hard drive if you use too much memory.

1024 is a binary friendly number (10000000000) whereas 1000 isn't (1111101000).


Jono(Posted 2005) [#3]
Each byte has an address so the computer knows where the put and grab data from in the memory, I've never seen one, and was wondering if anygood could give a good example how addresses are listed against bytes.


Who was John Galt?(Posted 2005) [#4]
A register is a piece of memory in a CPU that values from memory must be read into before they can be operated on and stored back to memory. An address is just another number that gives an index of a piece of memory, and is stored as a numer of bits just like any other number in the computer. 10 bits can store integer values from 0-1023=1024 possibilities, hence the definition of 'kilo'=1024 as it is close to 1000 and uses the full addressing capability of a whole number of bits. Search the net for some info on addressing modes if you want more detail.

Take a computer that can use a register of up to 2bytes (16 bits) to address memory. This gives 2^16=65536 possible addresses so can address 65536 bytes=64 Kilobytes. 64K will ring a bell if you ever owned a C64 or Amstrad CPC464. The swapping of memory Beaker's talking about is known as 'virtual memory' and was not available on these old machines.

I think there are various definitions of how many bytes constitute an Integer,
Small Integer etc depending on the machine.


Grey Alien(Posted 2005) [#5]
If you don't have virtual memory and you want more variables than the memory allows, you could a) optimise you code so you don't need so many! or b) compress part of the memory and decompress it when you need to access the variables (best for variables not accessed very much such as level creation variables or something).

Thing is with Integer is yes it is generally accepted as being 32bits (4 bytes) now but it is really platform dependent, on some comptuers an integer was only 16bits (sometimes called a word) and 32 bits was called long word.

You can display images by uploading to the web and the posting a link in this forum.

One format for a string is an initial byte which stores the length (max 255 chars) and then up to 255 chars (bytes) of text following it. Another format is the text followed by a final 0 byte (the string terminator).

1024 is got by going 2*2 = 4, then 4*2 = 8, 8*2 = 16 and so on e.g. 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024. You may notice that those numbers are all pretty computery right ;-)

Don't forget about Hex (base 16 numbering system) that is very useful for converting to binary and back and is often used to represent a memory address (blue screen of death for instance). PCs can access 4Gb of memory via a 32bit address, in hex you would specify a byte address like so $12569aef (for example); each of the numbers or letters is 4 bits, there are 8 numbers/letters, 8x4 = 32bits.


Bot Builder(Posted 2005) [#6]
Another string format, the blitz one is a 4 byte integer for the length followed by the actual charachters. Each letter of Text is converted to numbers - http://www.lookuptables.com/

Oh, and its not exactly correct that PCs can acceess only 4Gb of memory - grab a 64 bit processor and 64 bit OS, and you can theoretically have up to 17.2 billion GB of ram. AMD Athalon 64s, however, have a 40 bit address bus limiting you to a terabyte (1024 GB).

One interesting thing about mem is that if I remember correctly, a program is loaded into ram when run, and variable space begins at the end of a block of mem initially allocated for the program. Then, variables are written backwards from this point. Not possible in a lang like blitz, however, in C/C++/especially ASM it is fairly easy to use it up and begin writing in the program data. This is a buffer overflow, and is associated with many methods of hacking. A particularly clever hacker might manage to actually erplace the program with his own.


Grey Alien(Posted 2005) [#7]
I didn't wanna confuse the guy with 64 bit memory ... note that I didn't use "only 4Gb", I just described 32 bit addressing. Not trying to be funny, just a perfectionist u know ;-)