Pointers Help

CNerd2025

Solid State Member
Messages
7
I am very new to C/C++, and for that matter, to this board as well.

I understand most of the concepts of each, and the difference between them. However, I am having trouble with pointers. I do not understand what they do, and I don't understand what the heap andstack are. I've seen different definitions of each, and I'd like an actuall programmer to set me straight. I've read in various books/tutorials that once you understand pointers you've pretty much mastered the language.

I'd be very grateful for any help. Thanks!

--Drew
 
I'll try my best to explain......

Hi Drew,

I'm not really an experienced programmer, but I understand how pointers work and how pointers make C/C++ a powerful language :D, and I'll try my best to explain them!

By definition, pointers are variables which store the *memory addresses* of other variables. Why "once you understand pointers you've pretty much mastered the language?" It is because pointers are unique and powerful tool which gives the programmers much control in a convenient way (and that's why I chose C++ instead of Java :D). For example, you use pointers to create a dynamic arrays (since you cannot use a variable to declare an array, e.g. int anArray = int[anotherVar];) which size can be declared with variables. In short, pointers make a program more sufficient by allowing the programmers to handle the addresses of a variable instead of the actual objects (which may be huge, wasting computer resources). Like you can pass the addresses to other function, then the code inside the function can call the actual objects using the address (unlike the actual object, which takes only few bytes or less).

For more advance usage of pointers, you can make a binary tree (a heap is a binary tree except heap is a *complete* binary tree). A binary tree (in case you don't know it) is like a sorted structure of data, which is a handy tool to use and data can be found very fast too! You have a root on the top, and then value less than the root go to the left and value greater or equal than go to the right (if i'm wong someone please tell me). Here is why pointers are needed: a node, which makes up binary tree, contains a value (the data) and two pointers, one points to the left and the other points to the right. If you don't understand, see this animated binary tree: http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html (each circle is a node). Basically, pointers enable a node to point to another node because the pointers store addresses, in this case, another node's address. This allows the parent node (or supernode) of the child node be able to locate the child node. Without pointers you can never do this (someone please tell me if there is anyway to do this). Once again, heap is a complete binary tree and has it own order. It is a broad topic, so I cannot explain it here, but check this PDF about heap: http://www.scs.carleton.ca/~nussbaum/courses/cs/95.384/heaps/Heap_Data_Strucure.pdf.

Stack, like heap and binary tree, is another advance method to code. Stack can be coded using array (but vertically). You might have heard this: stack is in LIFO order, that is, Last In First Out. This basically means that a new data is inserted on TOP of the rest, and when you ask the stack to retrieve the data, the TOP data is returned. In short, stack does NOT allow random access; opposite to arrays, any values in an array can be retrieved using anArray[indexNum]. On the other hand, ONLY the value on the TOP can be retrieved. Opposite to Queue, in FIFO (First In First Out) order, queue only allows the value on the BOTTOM to be retrieved; so, if you understand queue, you understand stack.

Stack, queue, binary tree and heap are ALL advance, better way to program. Therefore, unlike array, you cannot declare a stack by using aStack = (stack)stack[5]; or something like that. You have to implement them yourselves as a programmer(or find code already made by others). In conclusion, pointers make these advance way of programming possible. (I think Java have stack and queue too, but it doesn't support pointers; the Java makers implement them already, which makes programming no fun *annoyed*) And this is why I love C/C++.

Sorry for my extremely long reply. Hope this lengthy reply help you in any way. And if you're interested in C++, go to http://www.cpp-home.com, which has a lot of resources on the language :D)

(To other programmers: If anything here is wrong, please inform me. Thanks :D)

Have a nice one. And Happy Programming
 
Re: I'll try my best to explain......

Thank you so much for your help. I must admit that most of your reply was new info, but I learned from all. I must also admit that I do not understand what a dynamic array is. Due to your excellent links and descriptions, I understand now what heap and stack are, as well as queue and binary trees, to which I was not familiar. I must also admit that I have no idea what a "dynamic array" is. From its name I can derive the fact that it is not static, and therefore changes, but how is one declared with pointers. If you have any web resources to which you can give me access, please let me know.

By the way, in my previous searches to find information on heaps and stacks, there seemed to be something to do with memory allocation. I seem to remember that stack is memory allocated at compiletime, while heap is memory allocated at runtime. Are there possibly two definitions of heap?

Thanks again for all of your help!

--Drew
 
Dynamic Arrays and Memory Allocation

You're welcome! I've been in Toronto these three days for vacation and couldn't reply =D.


Sorry that I didn't explain what dynamic arrays are. If you are saying they change in form, then I have to say you guessed it wrong =D. Why people call them "dynamic" is simply because they use dynamic memory. In case you don't know what dynamic memory is, there are three types of memory: static memory, automatic memory, and dynamic memory. Static memory is used when you declare global and static variables (that is, they are "static" and not going to be destoyed until the end of the program). Automatic memory is used when you declare local variables and parameter variables; the program create and destroy them as needed (e.g. they are "local" and created at the beginning and destroyed at the end of a function), they are created automatically created and destroyed, and that's why they are named so. Static and automatic memory are both created by the program. You type int anInt = 5; and the memory is allocated for you. And the program has the control of them, you have no say! You, however, have power to allocated memory yourself (power of C/C++!). Probably you knew this already, but you create a dynamic memory slot by typing (for example) "int* a = new int;" (C++ style) or "int* a = (int*)malloc(sizeof(int));" (C style). Now, you have a pointer "a" (hope you're familiar with pointer by now) and this pointer points to this particular memory address. Maybe it looks the same, but here are the main differences: you can delete them (and have to manually) by typing "delete a;" and they exist as long as you don't delete them. This is a problem sometimes because if you have a HUGE array of dynamic memory variables and you forgot to delete, they will exist even after the program ends, which is commonly known as memory leak. The memory is still allocated and the computer can never release them and use them for other programs. This will sometimes significantly reduce computer's performance. But it can be fixed by restarting the computer. Sorry for these irrelevants...... let get back to dynamic arrays. So, you can create an array of dynamic memory variables. They are used extensively in C (and sometimes C++ too). Notice that you cannot create an array by doing this:
int x = 10;
char string[x];
this is because the compiler needs to know the size in compile-time. When you use dynamic memory, since you have the control so the compiler does not care about it, you can do this:
int x = 10;
char* string = new char[x];
and you can delete the whole string anytime you want by doing this:
delete[] x;
Therefore, for this reason, most people with enough programming experience would prefer the old C-style dynamic arrays over the new static or automatic arrays due to the flexibility they offer. I tried to find a good source on dynamic arrays but couldn't find any explainatory one in C/C++. sorry~ :(


For the second question you are asking, I'm not sure what you meant "stack is memory allocated at compiletime, while heap is memory allocated at runtime." Because stack and heap are memory structures (that is, they are virtual representation of a set of data) but not memory types. Probably you got something mixed up. Due to computer scientists want to maximize the memory size (to increase computer resources and performances), they design the computers to keep Activication Records (AR) so that the computer will keep a record for every function called or every variable created, etc. Then, when these functions and variables are no longer needed, the computers, having these ARs, know where in the memory did these functions and variables use and be able to deallocate them for other programs' use. For static memory (as a reminder, global and static variables), the machine will use stack to keep track of all these variables. And the compiler, at compile-time, will sort of "prepare" a stack of static variables so that the machine, at runtime, can set up the stack. Probably this is why you remember stack is memory allocated at compiletime. The machine, when dealing with dynamic memory, have to use a heap structure of ARs because this type of memory slots can be destroyed anytime the programmer wants. Unlike stack, which only allows LIFO (Last-In-First-Out) order, heap allows the machine to access a particular ARs in the heap structure and erase the records with ease without affecting the rest of the structure. And the machine can only create the heap structure of ARs during runtime because this type of memory is "dynamic". And maybe this is why you associate heap with memory during runtime. By the way, this heap-, stack-based allocation and ARs are really low-level and hard-to-understand topics. If you are really interested, they are often tought in some advance computer classes in college, or you can even buy or borrow a college computer textbook to take a look into it.

Hope this helps. Have a nice one. Happy Programming again.
 
Back
Top Bottom