|
||||||
How to Use C++ Data Types and VariablesExamining C++ Variables and Their Effect on Memory Usage
C++, like all programming languages, needs a way of working with values (such as numbers). That's done by using variables.
Variables and constants are used in the majority of programming languages and C++ is no different. Both variables and constants are used to store information within a program and this information only exists whilst the program is running. Therefore variables and constants are very similar, but with one major difference. Once set the value of a constant cannot be changed and so, before moving on to constants, it is important to understand C++ variables. What is a C++ Variable?A C++ variable is, quite simply, a location in a computer's memory and a program can either read from this memory location or write to it. The programmer allocates some of the computer's memory for use by a variable when they declare it in their code. Declaring a Variable in C++A C++ variable is declared by telling the program:
for example, to declare a new integer the code would be: #include <iostream.h>
int main () {
int x = 3;
int y, z;
y = 4;
z = x * y;
cout << z << "\n";
return 0;
}
Here 3 and 4 are multiplied together to produce 12 (which is output to the screen). Interestingly all three values take up the same amount of memory. C++ Variables and Memory UsageThe amount of memory used by a variable does not depend on the value of the variable. It depends on its data type: int x = 1;
int y = 100;
int z = 1000000;
cout << sizeof(x) << " " << sizeof(y) << " " << sizeof(z) << "\n";
In each case the sizeof method will show that 4 bytes of memory is used by an integer regardless of the value of that integer. However, the memory used by the integer can be changed by using the long and short key words: short int x = 1;
long int z = 1000000;
cout << sizeof(x) << " " << sizeof(z) << "\n";
In this case the short integer will use 2 bytes and the long integer will use 4. This, of course, means that the maximum number that a short integer can hold is half that of a long integer and an interesting thing happens if the maximum number is exceeded: C++ and the Sign of a VariableThe maximum value that a small integer can hold is 65535 but it is worth seeing what happens if this is exceeded: short int x = 65535;
x += 2;
cout < x << "\n";
The program will not crash in this instance but neither will it return 65537. Instead it will return 1. This is because the short integer is unsigned (or positive) by default and can contain the values 0 to 65535. When the maximum value is exceeded it just loops back to the minimum. If negative numbers are requires then a signed integer should be used: signed short int x = 32767; The size of the integer will still be 2 bytes but now the range is -32768 to 32767. C++ Variable Types and RangesEach variable type has its own predefined memory usage. This, therefore,limits what can be stored in any variable:
It is then just a matter of the programmer ensuring that they use the correct data types for their variables. By doing that they will ensure that the program does not produce incorrect results due to any memory limitations. SummaryVariables are an important part of any C++ program and are defined by:
The variables are easy to use but each one uses a certain amount of memory (and that's dependent on the data type) and and variable may only use a very strict range of values (and again that's dependent on the data type). However, provided that the limitations are observed correctly, the programmer can manage the data used by a program and its memory usage in an efficient and professional manner.
The copyright of the article How to Use C++ Data Types and Variables in C Programming is owned by Mark Alexander Bain. Permission to republish How to Use C++ Data Types and Variables in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||