davidktw
Arch-Supremacy Member
- Joined
- Apr 15, 2010
- Messages
- 13,550
- Reaction score
- 1,302
The post below is in response to a EDMW spoof found at http://forums.hardwarezone.com.sg/e...mmers-who-dont-know-what-pointer-4880455.html
However I would rather newbie programmers who visit this forum to acquire better understanding about such things instead of the fast flowing EDMW which will quickly bury these information.
It is by no means a complete commentary on pointers. Interested parties should go and read up the relevant topics on your own. However what I have shared is the mentality on why a computer scientist should be proficient in the pointer theory, so that it can kick start more in-depth knowledge that will benefit him/her on long run in the computing career.
==========================================================
In any case, since there seems to be so much opinions about pointers, then I will give mine here - If you don't know pointers, your computing knowledge is very much limited. Does it means you can't do great things ? Nope, you still can produce good works, since there are a lot of areas of works that does not require good understanding of computer memory models.
First I saw a closest explanation about pointer, while correct, is not really explaining about pointers. There is a mixture of another topic known as memory models. Pointer is a high level abstraction from memory models, but does in some way related. Still the explanation shouldn't involve it. I will get to the part shortly.
First thing first. Pointer is nothing more than a variable that stores memory address. Here we don't go into what is exactly stored, but rather we assume it is a memory address to somewhere. In more concise, it is just an integer.
When we discuss about pointers, we often need to take into consideration another programming language concept known as Type. Without involving the typing system, pointer will be less powerful. Some have posted the difficulty of dealing with pointers having that it results in memory leakage that causes crashes or memory segmentation fault or bus faults is an incorrect perception. Pointers can co-exist with automatic memory management subsystem to garbage collect unreachable memory blocks. However, software developer must learn how to play nice with the AMM subsystem since these systems are normally not part of the programming language semantics and hence some rules have to be obeyed.
I will also expand the topic into references, object oriented view of the issue with relevant to pointers.
Lets start with something simple
2 variables are created, a primitive integer and the other the pointer to an integer. The memory address of integer variable "n" is assigned to variable "p". Understanding computer memory model means we know the compiler will produce codes that allocate a block of memory with the placeholder called "n". The 2's complement binary representation of the integral value "10" bin=(1010) will be stored at the memory location allocated for variable "n". Since "n" is an integer, on 32/64bits memory width system, it normally take the form of 4 bytes.
When the following code "int *p = &n;" is run. What it does is to assign the memory address of variable "n"(&n) to another memory allocation created for variable "p". Depending on which kind of memory system, the pointer variable "p" will be either 16/32/64bits in storage size. However still explaining pointers normally don't need to involved it yet.
Assuming variable "n" is created at memory location 12345678 during runtime, then we can safely assume the value of variable "p" will be 12345678 too. This is still a high level explanation of Pointer. When one run codes like
'printf("%d", n);', the system is always dealing with memory address. The system don't care what "n" is. It's like a printed label that you pasted onto pigeon holes that you name the hole, but ultimately you can always count the pigeon holes #1, #2, #3, ...
A name makes it easier for human to refer to. That means simple instructions like 'printf("%d", n);' basically instruct the system to assign the memory address of variable "n" to a register, invoke the "printf" routine to print the contents found at memory location of variable "n". This is a naive way to looking at what is going on.
Variable "p" meanwhile stores 12345678, which is not very useful on it own. However when couple with the TYPE of the variable "p", the compiler understands that the value is a memory address at 12345678 and what this memory location store is an integer of 4 bytes. That means the data is at memory locations 12345678, 12345679, 12345680 and 12345681 assuming each memory address is 1 byte. Up to there, I think it's pretty clear why earlier I mentioned the typing system has to be discussed with pointer, because they are closely related.
From the programming language standpoint, despite that memory address is what system works with, the label is often the context for programmers to use. Hence if we want to refer to memory address of variable "n", we use "&n" to reference the label to a memory location. Since this value is assigned to variable "p", hence to get the variable "n", we dereference the memory location using *p;
Hence here we observe type of "*p"(int) is equivalent to type of variable "n"(int). Likewise &n(int*) is the same type as p(int*).
As we refer to pointers, there is another type of referencing technique known as References. Different programming languages have slightly different semantics to references, but ultimately it is derived from pointers. In C++, reference must refer to an entity. Unlike pointers, it is not allowed for reassignment. However in Java, there is also reference. The semantics in Java is different in that it functions similarly to pointers except that there is no pointer arithmetic, and you can't explicitly use it to reference any memory location. It must refer to either NULL or an existing entity with known memory addresses.
The semantics enforced by Java is stronger and more strict in order to facilitate the proper operation of the Garbage collector. In terms of programming features, references are weaker than pointers, but they do helps to provide safer programming techniques with the help of the automatic memory management subsystem known as the garbage collector.
In C++ references exist in the following form
May it be references or pointers, the concept exist in all programming languages and influence the perception of data structures.
Analogous to the classic wave theory and quantum mechanics of light in the physics. Pointers or References and Objects are interchangeable.
Using the Java example below
We can tell this is a data structure of a Linked List. How would you have visualise it in both different approaches
The top shows the pointers/references concept, the one below is OOP approach. However you will find lecturers don't describe these 2 forms to you even though it is a linked list. The top one is even often the case no matter it is OOP or normal classic references and pointers.
The whole concept on how you visual thing comes from the knowledge of understanding what really is pointers, how does it related to references and how it is a linkage concept.
It is already a very long post and yet I must say it merely scratches the surface. If we dwell deeper, we can discuss about endianness of the system and how it affects pointers, the way to calculate the size of structures with respect to pointers and alignment.
We can also further look at real memory model, protected memory model and discuss why pointers concept are not directly affected by still in ways relevant. How in real memory model that segment selectors are used, how in the 32bits protected memory model that they are described with paging tables and so forth.
My point is it has to start from pointers because it built up the basic understanding about memory and hence it will broaden your knowledge on computing. That's why it comes back to my earlier saying - "If you don't know pointers, your computing knowledge is very much limited"
It is the touching stone to kick start deep understanding of memory and how they are managed. I'm afraid just proficient with high level programming language wouldn't get you very far. When performance tuning is required, there is always a dark area which you will not understand and hence prevented you from truly appreciating computer science.
However I would rather newbie programmers who visit this forum to acquire better understanding about such things instead of the fast flowing EDMW which will quickly bury these information.
It is by no means a complete commentary on pointers. Interested parties should go and read up the relevant topics on your own. However what I have shared is the mentality on why a computer scientist should be proficient in the pointer theory, so that it can kick start more in-depth knowledge that will benefit him/her on long run in the computing career.
==========================================================
In any case, since there seems to be so much opinions about pointers, then I will give mine here - If you don't know pointers, your computing knowledge is very much limited. Does it means you can't do great things ? Nope, you still can produce good works, since there are a lot of areas of works that does not require good understanding of computer memory models.
First I saw a closest explanation about pointer, while correct, is not really explaining about pointers. There is a mixture of another topic known as memory models. Pointer is a high level abstraction from memory models, but does in some way related. Still the explanation shouldn't involve it. I will get to the part shortly.
First thing first. Pointer is nothing more than a variable that stores memory address. Here we don't go into what is exactly stored, but rather we assume it is a memory address to somewhere. In more concise, it is just an integer.
When we discuss about pointers, we often need to take into consideration another programming language concept known as Type. Without involving the typing system, pointer will be less powerful. Some have posted the difficulty of dealing with pointers having that it results in memory leakage that causes crashes or memory segmentation fault or bus faults is an incorrect perception. Pointers can co-exist with automatic memory management subsystem to garbage collect unreachable memory blocks. However, software developer must learn how to play nice with the AMM subsystem since these systems are normally not part of the programming language semantics and hence some rules have to be obeyed.
I will also expand the topic into references, object oriented view of the issue with relevant to pointers.
Lets start with something simple
Code:
int n = 10;
int *p = &n;
2 variables are created, a primitive integer and the other the pointer to an integer. The memory address of integer variable "n" is assigned to variable "p". Understanding computer memory model means we know the compiler will produce codes that allocate a block of memory with the placeholder called "n". The 2's complement binary representation of the integral value "10" bin=(1010) will be stored at the memory location allocated for variable "n". Since "n" is an integer, on 32/64bits memory width system, it normally take the form of 4 bytes.
When the following code "int *p = &n;" is run. What it does is to assign the memory address of variable "n"(&n) to another memory allocation created for variable "p". Depending on which kind of memory system, the pointer variable "p" will be either 16/32/64bits in storage size. However still explaining pointers normally don't need to involved it yet.
Assuming variable "n" is created at memory location 12345678 during runtime, then we can safely assume the value of variable "p" will be 12345678 too. This is still a high level explanation of Pointer. When one run codes like
'printf("%d", n);', the system is always dealing with memory address. The system don't care what "n" is. It's like a printed label that you pasted onto pigeon holes that you name the hole, but ultimately you can always count the pigeon holes #1, #2, #3, ...
A name makes it easier for human to refer to. That means simple instructions like 'printf("%d", n);' basically instruct the system to assign the memory address of variable "n" to a register, invoke the "printf" routine to print the contents found at memory location of variable "n". This is a naive way to looking at what is going on.
Variable "p" meanwhile stores 12345678, which is not very useful on it own. However when couple with the TYPE of the variable "p", the compiler understands that the value is a memory address at 12345678 and what this memory location store is an integer of 4 bytes. That means the data is at memory locations 12345678, 12345679, 12345680 and 12345681 assuming each memory address is 1 byte. Up to there, I think it's pretty clear why earlier I mentioned the typing system has to be discussed with pointer, because they are closely related.
From the programming language standpoint, despite that memory address is what system works with, the label is often the context for programmers to use. Hence if we want to refer to memory address of variable "n", we use "&n" to reference the label to a memory location. Since this value is assigned to variable "p", hence to get the variable "n", we dereference the memory location using *p;
Hence here we observe type of "*p"(int) is equivalent to type of variable "n"(int). Likewise &n(int*) is the same type as p(int*).
As we refer to pointers, there is another type of referencing technique known as References. Different programming languages have slightly different semantics to references, but ultimately it is derived from pointers. In C++, reference must refer to an entity. Unlike pointers, it is not allowed for reassignment. However in Java, there is also reference. The semantics in Java is different in that it functions similarly to pointers except that there is no pointer arithmetic, and you can't explicitly use it to reference any memory location. It must refer to either NULL or an existing entity with known memory addresses.
The semantics enforced by Java is stronger and more strict in order to facilitate the proper operation of the Garbage collector. In terms of programming features, references are weaker than pointers, but they do helps to provide safer programming techniques with the help of the automatic memory management subsystem known as the garbage collector.
In C++ references exist in the following form
Code:
int n = 10;
int &q = n;
May it be references or pointers, the concept exist in all programming languages and influence the perception of data structures.
Analogous to the classic wave theory and quantum mechanics of light in the physics. Pointers or References and Objects are interchangeable.
Using the Java example below
Code:
class Node {
int data;
Node next;
}
Node root = new Node();
We can tell this is a data structure of a Linked List. How would you have visualise it in both different approaches
The top shows the pointers/references concept, the one below is OOP approach. However you will find lecturers don't describe these 2 forms to you even though it is a linked list. The top one is even often the case no matter it is OOP or normal classic references and pointers.
The whole concept on how you visual thing comes from the knowledge of understanding what really is pointers, how does it related to references and how it is a linkage concept.
It is already a very long post and yet I must say it merely scratches the surface. If we dwell deeper, we can discuss about endianness of the system and how it affects pointers, the way to calculate the size of structures with respect to pointers and alignment.
We can also further look at real memory model, protected memory model and discuss why pointers concept are not directly affected by still in ways relevant. How in real memory model that segment selectors are used, how in the 32bits protected memory model that they are described with paging tables and so forth.
My point is it has to start from pointers because it built up the basic understanding about memory and hence it will broaden your knowledge on computing. That's why it comes back to my earlier saying - "If you don't know pointers, your computing knowledge is very much limited"
It is the touching stone to kick start deep understanding of memory and how they are managed. I'm afraid just proficient with high level programming language wouldn't get you very far. When performance tuning is required, there is always a dark area which you will not understand and hence prevented you from truly appreciating computer science.

