1
$\begingroup$

I'm a beginner. I would like to know if the size of a variable (and generally speaking of a storage space) is:

  1. Its capacity (the amount of data that can be put in it).
  2. The amount of data it contains
$\endgroup$

closed as unclear what you're asking by David Richerby, fade2black, Evil, Luke Mathieson, Rick Decker Dec 13 '17 at 17:35

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ In what context did you see the phrase "size of a variable"? I'm not convinced this is really a question about computer science. $\endgroup$ – David Richerby Dec 10 '17 at 19:31
2
$\begingroup$

Different programming languages have different ways of implementing the concept of a "variable". The "size" of a variable in a computer program will vary depending on a number of characteristics that determines how the target programming language implements variables, the type of the variable, and how the target hardware handles memory access.

The size of the memory area for a variable may include not only the memory for the data of the variable but also memory for variable memory management. In a programming language such as Java or C# that has garbage collection, variables have additional data that is associated with the variable indicating whether the variable is still in use or not. So the actual memory size used for these variables will be larger than what the data type implies because not only is there memory for the variable's data but also memory for managing the variable's life time.

Programming language may matter

Some programming languages are much more flexible about how variables are created and used. An example of such a flexible language is JavaScript which is quite extraordinary in what the language allows you to do with variable assignments. Php has a similar flexibility in that a variable can be assigned a text string value at one point and a numeric value at another point and it will do automatic conversions from text to numeric for you as well.

So with programming languages such as JavaScript and Php, the idea of a variable and the type of value of a variable (text or integer number or floating point number or whatever) is quite fluid.

In the C++ programming language an object created from a class may be larger than the individual variable members of the class would indicate even with padding considered (see below about padding in C which is also used in C++). This can come from the addition of a v-table or virtual method table that the compiler generates when a function with the virtual keyword is part of the class.

Machine hardware may matter

In the C programming language there is a variable type of int which is used to define or declare a variable as an integer value. The actual physical or memory size of an int variable can vary depending on the target hardware. With 16 bit hardware the size of an int may be 16 bits while on 32 bit hardware the size of an int may be 32 bits and on 64 bit hardware the size of an int may be 64 bits.

These variations also means that the range of values that an int variable may assume or have will vary between different types of hardware.

The C programming language has a way to allocate memory at run time using the malloc() function of the Standard Library. When you make a call to request a memory area, the memory area that is actually allocated will be larger than the size you specified. This additional, hidden memory is used to contain information about the block of memory that was allocated. In addition the memory area may include padding or extra memory the memory allocator adds to your request to make its job of managing memory easier.

Finally in some cases you may define a variable which the C compiler will eliminate. There are a number of reasons why an optimizing C compiler will eliminate a variable.

The most obvious is because the compiler sees that while the variable is defined, it is not actually used anywhere so the compiler eliminates the variable in order to save memory space.

A second case is when the variable is used in a temporary fashion so rather than allocating memory for the variable, the variable's value is instead stored in a hardware register because while the value of the variable is needed, an actual memory location is not.

A third case is when the variable is set with some value that does not change so the compiler eliminates the variable and uses the constant value in the places where the variable would have appeared.

Memory padding by the C compiler

The C compiler standard also allows for the use of memory padding for variables that are made up of other variables, the struct. A struct defines a new type of variable that is composed of other variable types. An example:

struct {
    int   iVal;
    char  chArray[15];
    long  lVal;
    char  chArray2[5];
} MyStruct;

On an Intel x86 32 bit machine the C compiler may allocate a memory area for a variable of this type that would be the size of an int, 32 bits, fifteen times the size of a char, 15 * 8 bits, the size of a long, 32 bits, and 5 times the size of a char, 5 * 8 bits. Or the C compiler may introduce padding in order to align the long variable lVal on a 32 bit or double word memory boundary. So instead of the size of the struct being 32 bits plus 15 * 8 bits plus 32 bits it is instead 32 bits plus 16 * 8 bits (padding introduced to align the beginning of the next variable on a 32 bit memory boundary) plus 32 bits plus 5 * 8 bits.

This memory boundary requirement will vary depending on the underlying machine hardware characteristics. Some hardware designs are more efficient at accessing memory when a memory address is on a particular memory size alignment. For other hardware designs it may not matter.

So the C compiler may see the above struct (again this depends on the hardware design and the C compiler as the C Standard does not have a specific requirement for memory alignment) as really looking like the following:

struct {
    int   iVal;
    char  chArray[15];
    char  chPadding[1];   // invisible padding added by compiler for memory alignment.
    long  lVal;
    char  chArray2[5];
} MyStruct;

If you have an array of this MyStruct variable type you might expect that the memory allocated for the array would be the number of elements of the array times the size of the individual array element.

However the C standard says that a struct must begin on a memory boundary that aligns with the most restrictive type's needs. So in this case a long needs or requires a double word alignment so each of the elements of the array must start on a double word alignment.

The result of this requirement is that padding is added to the end of each array element when the size of the array is calculated by the compiler.

So now the struct looks to the compiler like the following:

struct {
    int   iVal;
    char  chArray[15];
    char  chPadding[1];   // invisible padding added for memory alignment.
    long  lVal;
    char  chArray2[5];
    char  chPadding2[3];   // invisible padding added for memory alignment for array.
} MyStruct;
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.