When we talk about C and C++ and we talk about variables, we often refer to variable’s as names/symbols that ‘refer’ to things. We say things like that because we like to abstract ie. do represent complex things as simpler, and faster-to-understand. Its not accurate but its faster.
I’ve been reading “Game coding complete” today and this became evident.
People are generally very messy when it comes to using word terms that represent anything in languages. They often don’t articulate what they mean, and usually expect you to guess the same meaning they are assuming or figure it out. For instance.
“A variable refers to a thing” is messy because to truly represent what a variable is in C++ you would say that a variable is a name that stores an address in memory.That's really what a variable is.
The problem is that doing that all the time, makes things very verbose and time consuming especially when you have lots to say about something. We are lazy. So you use messy words that are faster and generally people figure out. So anyway, by point in this article is to say that because we learn from messy teachers, we dont know that all variables in C/C++ store addresses – not just pointers!
In C/C++ you have the following notations for variables:
[Type] [Variable-name] or [Type] [*] [Variable-name]
such as
char name;
char * name;
These are variable declarations.
Both store memory addresses: Some people don’t know that because they’ve learned by being taught by messy words which suggest that one of these variables store an address. That’s the consequence. Trust me,they both store addresses.
So mostly they look and do the same thing.
One is called a variable and the other is called a pointer variable. So how do they differ? And I’ll tell you how they differ at the implementation level.
char name; // variable stores a memory address say 0002, where say the content “Stuart” is stored or will be stored.
char* pname = &name; // pointer stores a memory address also of 0002, also thus where the content “Stuart” is stored.
The main difference between the two variables is what happens when the variables are read/accessed.
The * in the variable declaration denotes that the variable has access to the address its storing *directly*. You can think of the * asterisk as saying this variable has full access to its stored address(0002)
A variable without the asterisk does not have full access to its address, instead when you access it, it bypasses access to the address(which it still stores but just doesn;t have direct access so) and instead refers to the raw data/content at that memory location – not the memory location itself, not the number representing the memory address 0002.
So a non-pointer will always manipulate the data at the address location that it stores, the pointer will always manipulate the address itself that it stores. Done.
So in summary:
All variables store memory addresses whether they are pointers or not. Pointer variables, have the special ability (denoted by * during declaration) of accessing that memory address number. Normal variables don’t, and instead directly go to that address and manipulate the content at that address..not the address number itself.
Extra information.
Memory addresses can be addresses on the stack or on the heap. variables and pointers can store either. Depends on how they address is obtained:
int variable1(1); // stores an address on the stack and when you access that variable, you are manipulating the content of a int’s data at that location( in this case 1).
int *variable2(new int(2)); // stores a heap address also on the stack, but variable2 has direct access to that stack address…which happens to additionally store new address of a heap location returned by new()
That's really how it works. but to be more accurate each variable itself is a memory location on the stack, that just stores other memory locations on the stack. And depending on how the variable was declared will determine how the content of that memory location is read/accessed.
[some stack address] holds variable1’s content which always another stack address (who’s when accessed, the content 1 is directly accessed/amnipulated)
[some stack address] holds variable2’s content which in this case is a heap address who’s content when accesses, refers directly to that heap address.
Simple.