The many faces of a variableFiled Under: Articles, Weekly Tuesday Dose of goodness
Hi all,
This week, we’ll revert back to the long line of topics I’m going to share. Today, we’ll talk about the many faces of a variable.
For most people, when they were first introduced to programming, they’re also introduced to variables as well. So why are there many faces in a variable?
Let’s have a look.
Variables
First of all, what are they? Variables provide a means of storing a state or value so that they can be used at a later stage. Various programming languages have different mechanisms, semantics and syntax of representing a variable. The earliest representations might probably be registers. (Don’t flame me if I’m wrong though…)
Different variations of a variable
Human intelligence naturally and eventually help provide new means of manipulating variables differently. Here’re a few examples:
1) Arrays
2) Pointers
3) Aggregations
Each of these 3 types individual branched out to several different descendants.
Arrays came about so that multiple variables can be stored in the memory allocated in a contiguous manner.
Soon it became obvious that arrays are “evil” as indicated by some. Data structures became THE Way to store variables instead. Eventually we have data structures like vectors, linked list, binary trees and so on.
Pointers were already around a long time ago. It’s more commonly known as direct memory address. In time, better forms of pointers or referential-integrity-like mechanisms emerged. In C++, references are usually used if possible compared to pointers. Just a little note on references; they behave like pointers but are definitely not a derivative of pointers.
You can run a test with a simple program using sizeof(). A reference is an alias, or acting on behalf of your normal variable; it cannot be deleted directly or be assigned to another referent.
In recent years, more smart pointer containers became available to help address the inherent problems of pointers. We’ll discuss this topic in a separate post.
Aggregations of variables are widely used to create a connection between seemingly technical representation of bits and a real life object. Hence, in C, structures were available for C programmers to aggregate multiple variables into a single unit of logical storage structure.
Later in C++ and Java, classes were introduced to provide a closer representation and connection with real life objects. Soon other mechanism such as encapsulation, polymorphism, abstraction and so on come about.
Coming back to the topic of variables themselves, we must first acknowledge that a variable has more than just the value it represents. Let’s look as straight as possible.
One last thing, all variables involve storing themselves into only 1 type of memory (in major programming languages). Yes, just when you think that a pointer is a heap variable, that’s wrong. Let me explain myself in a while. We’ll need to briefly discuss about the different types of memory used by a program.
1) Stack
2) Heap
3) Static
4) Code
These 4 types of memory are just some of the more generally known ones. It’s also named this way so that people can understand them easily. All variables, when declared, are all stored in the stack memory.
The only exception is with static, class and structure attributes.
Enough of talk, let’s have a look at the code snippets below. (Note, all these snippets are based on C or C++ and may not apply for Java or C#)
In this snippet:
int a = 0;
- Variable ‘a’ is stored in the stack
- Assigned to a value 0
Next, look at this:
int a = 0;
int* b = &a;
- Same thing for variable ‘a’
- A pointer (take NOTE) is declared and initialized with the address of variable ‘a’.
- Ask yourself, are pointers purely heap-based if it’s now pointing to a memory location in the stack?
- Pointer ‘b’ is stored in the stack
- The contents of pointer ‘b’ is the memory address (in the stack) of variable ‘a’
Next,
int* c = new int(100);
- Pointer ‘c’ is declared and initialized to a HEAP memory address. That’s it.
- Pointer ‘c’ is a stack variable.
- As the new integer is created in the heap, the value of 100 is initialized in the memory as well
- Pointer ‘c’ does NOT contain the content: 100
- This is alike giving someone a business name card with an address pointing to the location of your office
- Pointer ‘c’ leaks a single integer (4 bytes in 32-bit systems) if it’s not deleted.
Lastly,
cSmartPtr< int > intPtr = new int(100);
intPtr = NULL;
- intPtr is a smart pointer container; it’s an aggregated template object stored in the stack
- intPtr hold a reference count for this particular pointer, which is now at 1
- Once intPtr is set to NULL, reference count become 0 and a deletion takes place
- Why does it happen? That’s because of 2 things:
1) operator = has been overriden
2) A parameterized constructor taking in a T* pointer is created (since in template form, cSmartPtr is seen as cSmartPtr<T>)
In Conclusion
A variable can have many faces depending on what they represent.
A normal variable can have at least 2 faces. Why? That’s because,
other than its obvious value, the variable also has an address allocated on the stack.
A pointer variable can have at least 3 faces. Why? That’s because, other than its obvious memory address value it carries, the pointer
itself also has a memory address on the stack. Lastly, since a pointer can be dereferenced, it opens up a pathway to access the memory
contents in the heap.
A reference is not a variable. It’s really just an alias representing your normal variable (or pointers if need be). It does not have its own memory allocated to it and it doesn’t contain any values. It doesn’t pointer to its referent because of a memory address.
All temporary variables are ALWAYS located on the stack. Like it or not. This includes arrays, pointers and normal variables.
All static variables are located on the static memory. This is also known as the private heap.
As stack memory is very limited, a poorly design program can easily lead to stack overflow EVEN when there’re no recursive calls.
I seriously hope that this article can help clean up the air a little.
Signing off,
Jeremy
- Permalink
- Admin
- 30 Jul 2009 4:01 PM
- Comments (2)
July 31st, 2009 at 9:52 am
do you have an article that explains stack, heap and code? can’t seem to find it
July 31st, 2009 at 1:41 pm
dun think it’s up yet…
it’s actually one of the syllabus in my C++ Intermediate level training course.
Which is probably y I hav reservations putting them up….
Mabe an abstract one next week?