As you learned in the lesson on basic addressing, memory on modern machines is typically organized into byte-sized pieces, with each piece having a unique address. Up to this point, it has been useful to think of memory as a bunch of cubbyholes or mailboxes where we can put and retrieve information, and variables as names for accessing those cubbyholes or mailboxes.
However, this analogy is not quite correct in one regard — most variables actually take up more than 1 byte of memory. Consequently, a single variable may use 2, 4, or even 8 consecutive memory addresses. The amount of memory that a variable uses is based on it’s data type. Fortunately, because we typically access memory through variable names and not memory addresses, the compiler is largely able to hide the details of working with different sized variables from us.
There are several reasons it is useful to know how much memory a variable takes up.
First, the more memory a variable takes up, the more information it can hold. Because each bit can only hold a 0 or a 1, we say that bit can store 2 values. 2 bits can store 4 different values:
| bit 0 | bit 1 |
|---|---|
| 0 | 0 |
| 0 | 1 |
| 1 | 0 |
| 1 | 1 |
3 bits can store 8 values. n bits can store 2^n values. Because a byte is 8 bits, a byte can store 2^8 (256) values.
The size of the variable puts a limit on the amount of information it can store — variables that are bigger can hold larger numbers. We will address this issue further when we get into the different types of variables.
Second, computers have a finite amount of free memory. Every time we declare a variable, a small portion of that free memory is used as long as the variable is in existence. Because modern computers have a lot of memory, this often isn’t a problem, especially if only declaring a few variables. However, for programs that need a large amount of variables (eg. 100,000), the difference between using 1 byte and 8 byte variables can be significant.
The obvious next question is “how much memory do variables of different data types take?”. The size of a given data type is dependent on the compiler and/or the computer architecture. On most 32-bit machines (as of this writing), a char is 1 byte, a bool is 1 byte, a short is 2 bytes, an int is 4 bytes, a long is 4 bytes, a float is 4 bytes, and a double is 8 bytes.
In order to determine the size of data types on a particular machine, C++ provides an operator named sizeof. The sizeof operator is a unary operator that takes either a type or a variable, and returns its size in bytes. You can compile and run the following program to find out how large your data types are:
#include <iostream>
int main()
{
using namespace std;
cout << "bool:\t\t" << sizeof(bool) << " bytes" << endl;
cout << "char:\t\t" << sizeof(char) << " bytes" << endl;
cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << endl;
cout << "short:\t\t" << sizeof(short) << " bytes" << endl;
cout << "int:\t\t" << sizeof(int) << " bytes" << endl;
cout << "long:\t\t" << sizeof(long) << " bytes" << endl;
cout << "float:\t\t" << sizeof(float) << " bytes" << endl;
cout << "double:\t\t" << sizeof(double) << " bytes" << endl;
cout << "long double:\t" << sizeof(long double) << " bytes" << endl;
return 0;
}
Here is the output from the author’s Pentium 4 machine, using Visual Studio 2005 Express:
bool: 1 bytes char: 1 bytes wchar_t: 2 bytes short: 2 bytes int: 4 bytes long: 4 bytes float: 4 bytes double: 8 bytes long double: 8 bytes
Your results may vary if you are using a different type of machine, or a different compiler.
If you’re wondering what \t is in the above program, it’s a special symbol that inserts a tab. We will cover \t and other special symbols when we talk about the char data type.
Interestingly, the sizeof operator is one of only three operators in C++ that is a word instead of a symbol. The other two are new and delete.
You can also use the sizeof operator on a variable name:
int x;
cout << "x is " << sizeof(x) << " bytes"<<endl;
x is 4 bytes
Now you know enough about variables that we can start discussing the different data types!
2.4 — Integers
|
Index
|
2.2 — Keywords and naming identifiers
|
2.4 — Integers
Index
2.2 — Keywords and naming identifiers
Never heard of wchar_t before……is that a new data type?What kind of data does it hold?
There’s more information about wchar_t on wikipedia. In short, it was meant to be used to hold “wide characters” (eg. those that take more than 8 bits to represent). However, the size varies depending on platform (and can be as small as 8 bits), so I’m not sure I see the practical use.
Pocket PC/Windows Mobile only uses wide characters. Beyond that the practical use is for foreign languages. Some languages (like Chinese) have a lot more than 128 characters.
“On most 32-bit machines …. ”
I am not sure if this is the right place to ask this question .
What is a “32-bit machine ” ?
Thanks,
Nikki
Computers work by moving binary digits (bits) around. However, most computers do not work with individual bits — rather, they move data around in chunks. This chunk size is called a “word”. Typically, when we speak of the bit-ness of a machine, we speak of the size of a word. Thus, a 32-bit machine has a 32-bit word size, which means it moves information around 32-bits at a time.
Typically, modern computers use one word to address memory. With a 32-bit word, this means there are about 2^32 (4 billion) unique memory addresses that can be addressed. This is why 32-bit machines can only address 4GB of memory.
my long double takes 12 bytes
In the code above, all the “\t” characters are showing as “\\t” in my browser. This makes it show up as “\\t\\t” when you run the program instead of tabs. I don’t know if this is a browser displaying that code wrong (I’m running Firefox) or if you just typed that in incorrectly. Easily fixed by deleting the extra backslash.
Looks like it’s just a mistake. I’d expect anything containing “\\t” or “\\n” to purposely have two backslashes so they don’t become actual tabs or newlines, but the apparent “output” is there. I still think it’s a mistake.
Each
above should actually be
, otherwise a literal ‘\t’ will be printed rather than the intended tab character.
Hi, I would like to know what is the sizeof(long double); ? In this tutorial it is mentioned 8 bytes. Where as i tried in Dev C++ compiler it is giving 12.
Thanks,
syed
The size of a long double can vary from machine to machine. On most machines, it is either 8 or 12. The only way to know for sure is to use sizeof(long double) just as you have.
My int takes 2 bytes.
I am working with tourbo C .
My PC is 32-bit.