Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can RAM handle different data type sizes?

Tags:

c++

ram

int, char and bool usually have different sizes. Where int>char>bool, I suppose.

  • But does the RAM even support this?
  • How is it built up?
  • Can it take advantage of bool being only 1 byte and store it in a small "register"?
like image 241
tstbu Avatar asked May 06 '10 14:05

tstbu


People also ask

Do all data types use the same amount of memory?

Every data type requires a different amount of memory. C++ supports a wide variety of data types and the programmer can select the data type appropriate to the needs of the application. Data types specify the size and types of value to be stored.

Which data types are used by RAM model?

Answer: The correct answer of this question is Char, Short, Int and Long Types are used by RAM model.

Which data type takes less memory?

byte datatype has a range from -128 to 127 and it requires very little memory (only 1 byte). It can be used in place of int where we are sure that the range will be very small.


2 Answers

On a normal, modern computer all memory is byte addressable. That is each byte-sized storage location in RAM has a unique number assigned to it. If you want to store a one-byte value such as a bool (although bool s are not required to be one byte in C++, they just usually are), it takes a single storage location, say location 42.

If you want to store something larger than one byte, say an int, then it will take multiple consecutive storage locations. For example, if your int type is 16 bits (2 bytes) long, then half of it will be stored in location 42 and the other half in location 43. This generalizes to larger types. Say you have a 64-bit (8-byte) long long int type. A value of this type might be stored across locations 42, 43, 44, 45, 46, 47, 48, and 49.

There are some more advanced considerations called "alignment" that some sorts of processors need to have respected. For example, a processor might have a rule that a two-byte value must begin on an even address, or that a four-byte value must begin on an address that is divisible by 4. Your compiler will take care of the details of this for you.

The compiler also knows how long each type is, so when it generates the machine code for your program, it will know at which address the storage for each variable begins, and it will know how many consecutive bytes the variable is stored in.

"Registers" on the other hand, are something that exist in the processor, not in RAM, and are usually a fixed size. One use of processor registers is to store a value retrieved from RAM. For example, if your processor has 32 bit (4 byte) registers, then a bool value loaded from RAM will still consume an entire 4-byte register, even though it consumed only one byte when it was in RAM.

like image 195
Tyler McHenry Avatar answered Nov 01 '22 21:11

Tyler McHenry


Computer memory is organized into "words", a sequence of bytes of a given size (often a 2-power). Memory is usually read and written in these units which are often compatible with the size of the registers and the CPU's native support for arithmetic operators. This is typically the source of the "bit rating" of a machine (e.g., a 32 bit CPU, a 64 bit CPU, the old 8-bit video game consoles).

Of course, you often need a different size from the native word size. Machine instructions and smart coding allows you to break these words into smaller units by applying various bit-level logical operators, or to combine them into larger units by "combining" multiple words.

For instance, if you have a 32 bit word, you could AND a word against a pattern like 0xff0000ff to get the first and last byte in that word, or 0x0000ffff to get just the contents of the second 16-bit int.

In the case of bools, it is common to use memory as a bitmap. You can essentially place X "bools" in an X-bit word and access a specific bit by ANDing or ORing against a "mask" that refers to that bool. E.g., 1 for the first bit, 2 for the second bit, 4 for the fourth bit, etc.

In most machines, it is inadvisable to split a smaller data type across two words (this is called alighment).

When you work with a higher level language like C or C++, you usually don't have to worry about all this memory organization stuff. If you allocate an int, a short, and a double, the compiler will generate the appropriate machine code. You only do this directly when you want to smartly organize things in dynamically allocated memory, for example when manually implementing a bitmap.

When working with larger units than the native word size, the compiler will again handle most things for you. For instance, on a 32-bit machine you can easily handle 32-bit int operations, but to run the same code on an 8-bit machine or a 16-bit machine the compiler would generate code to do the smaller operations and combine them to get the results. This is partially why it is generally considered advisable to run a 64-bit OS on a 64-bit machine, since otherwise you might be performing multiple instructions and read/writes to simulate 64-bit on a 32-bit OS rather than a single instruction or memory access.

like image 41
Uri Avatar answered Nov 01 '22 23:11

Uri