Hello everyone!

Yesterday I asked if anyone had any thoughts about a tutorial that they would like me to write. Many of the requested tutorials dealt with C/C++ and uses thereof. Well, I refuse to go into the very basics of any programming language, such as what an if statement is; there are already plenty of tutorials and webpages devoted to such basic questions. Instead, I figured I'd give a bit of an explanation of one of the most confusing concepts in C and C++ programming.

For those that don't know, a pointer is a variable that holds a memory address. Now, what makes these exceptionally tricky to most programmers who haven't worked with low level languages is that other languages don't let the programmer deal with memory locations directly, like C or C++ does. If you haven't had the chance to work with a low-level language before, you may be asking the same questions I did when I first started working with pointers in my Computer Science classes, and that question is: "Why are they even necessary? Why can't I just use normal variables?" The answer to that question lies in the data that is held by a pointer and a variable (such as an int or a char) respectively. A variable holds the actual contents (such as a number, in the case of an int, or an ASCII number in the case of a char variable), while a pointer merely holds the memory location of whatever it is pointing to. So, if there was a character pointer, then it would hold a memory location where that character is stored. When a function is called, and the variables are passed to that function, it takes CPU time to "transfer" that data to the new instance of the function, and in certain cases this can cause many instances of the data as well. This may seem trivial when you consider such things as ints or characters, but what if the variable you had was actually for a data structure that held 10 MBs of data?

Let us do just that. Let us consider a data structure which holds approximately 10 MB worth of data, and we have a variable which is an instance of that data structure. Let us also consider that there is a pointer variable which points to the same 10 MB worth of data in that instance of the data structure. So, which would be faster to pass to the new function, 10 MB worth of data or a memory address that is 32 BITS long? Hopefully, you begin to see the usefulness of pointers, not to mention the fact that you can dynamically allocate memory whenever you need it.

So, that doesn't seem so hard now does it, and it helps speed up the program, so why do people have so much trouble with it? Well, allocating and deleting the memory have all of a sudden become your job to do. What does this mean exactly? Well, let me explain:

Your OS (no matter what it is), uses something called a Heap. This Heap holds all the memory locations that your computer has for you to use. So, if you want to make a pointer variable, what you need to do is allocate the memory needed for whatever type of data you are pointing to. The proper syntax in C++ would be:

" int * f00;
f00=new int;
"

That would create a pointer variable named f00 which would point to an integer. Then the second line gets the correct amount of memory from the Heap to store that integer in.

Now that you know about the allocation of memory, you should know about the most important part of pointers, and that is memory deallocation. Why is this so important? Because if you never deallocate your memory, it will never be put back into the Heap for other programs to use; all of a sudden you have lost that 8 megabytes of memory because your program never put it back in the Heap (think Internet Explorer ). Deallocation, however, is a little more tricky than allocation. First, you should know that functions will automatically call the destructor (if you know about classes, then you know what a destructor is, if not you should do a search on google for a tutorial on C++ class structure). The destructor calls the "delete" operation on the pointer variables, like:

" delete f00;
"
You should also know, that if you ever create a class that uses pointers as members, you should make sure to include the proper destructor which includes the delete syntax for your pointers. The big problems come up when you try to delete memory which has already been deleted, or when you just don't delete the memory at all.

Hopefully this has helped some people understand the concept of pointers a bit, and maybe peaked some interest in lower leverl programming. What I have portrayed above is certainly not a "How to" on using pointers, because I don't feel like going through all the syntax to be quite honest. All the syntax for both C and C++ (and you should be aware that allocation and deallocation of memory in C is MUCH different than C++) can be found readily in any C/C++ book in your local library. I also hope that everyone appreciates how hard it is to write tutorials on low level programming, and understands that writing a tutorial on "How to write a packet sniffer," or other such projects would be fairly hard to do, and would more likely than not assume a much more advanced knowledge of the C/C++ programming language than most people on this forum have.

Regards,
Wizeman