PDA

View Full Version : Pointers question: why use them instead of Global Vars?


kvernon
03-07-2006, 03:22 PM
I actually (finally) understand how pointers benifit C and Cpp, but where I'm lost now is, why use them if I can global variables? The is the only part I'm questioning on the pointers concept. Could someone enlighten me on something that I'm missing?

Thanks,

Kelly

Milho
03-07-2006, 03:38 PM
There are many benefits with pointers. Especially that you can manipulate the memory directly. This comes in very handy when creating arrays which size you don't know at the beginning.

But regarding your question the benefits of pointers over global vars is that with global vars you can't really tell where they have been changed, while pointers can have same benifits of existing for the whole application but you can tell which functions or classes know about it.
It's easier to debug and read. That's my opinion and experience but I'm looking forward to other statements.

Robert Bateman
03-07-2006, 03:39 PM
global variables are bad. very bad. They firmly root your code to the ground, and make it very difficult to re-use sections of code.

pointers have basically 3 uses.

1. avoiding copying of large data structures passed into functions
2. allow the editing of external data within a function
3. allow for memory allocation.


typedef struct {
float loadsOfData[100000];
} VeryBigThing;

/* this will copy all of the data in the struct */
void someFunc(VeryBigThing data)
{
/* any changes we make to data will change the copy */
}

/* by using a pointer, we go to the data directly, and have not copied tonnes of data */
void someFunc(VeryBigThing* data)
{
/* any changes we make to data will change the original */
}


and they allow for dynamic memory allocation of arrays....


int *pData = 0;
/* allocate an array of 1000 ints */
pData = (int*) malloc(1000 * sizeof(int) );

int i=0;
for(i=0;i!=1000;++i) {
pData[i] = i;
}

/* now get rid of it... */
free( pData );


if you manage to write a program using no global variables, you'll find that your code is actually very flexible and a lot easier to follow the program flow.

Milho
03-07-2006, 04:54 PM
:bowdown: (no comment)

mummey
03-07-2006, 05:24 PM
I actually (finally) understand how pointers benifit C and Cpp, but where I'm lost now is, why use them if I can global variables? The is the only part I'm questioning on the pointers concept. Could someone enlighten me on something that I'm missing?

Thanks,

Kelly

kvernon: I could give an explanation, but I have noticed that the best method to learn about pointers is to "see for yourself."

Look for information on Linked Lists. Also look into other data structures such as trees and hash tables. It will become ALOT clearer once you have done this.

kvernon
03-07-2006, 09:36 PM
Okie dokey, I am starting to grasp a little better about why you don't want it [having global vars instead of pointers]. I suppose it's all about how flexible you want to be with you application. It'll still take some sinking in though, but maybe that will happen later on in a future lesson. I understand the memory allocation, but at the same time I don't. Since my background has been simpler languages (get ready to laugh :) ) javascript, actionscript 2.0 and asp vbscript, I haven't really needed to worry on this front. But I'll get it!

Thanks for the help. I'm still sitting back pondering all your comments more. I just have to adjust my thinking. It's like when you learned how to ride a bicycle without training wheels for the first time. Hmmm, maybe that's a little too basic, but you all get the idea. Well my brain is crapped out at the moment.

I'll do some studying later on tonight and see if things gel a little bit more.

Thanks everyone,

Kelly

Xaust
03-18-2006, 05:16 AM
You will have to use experience and judgement to answer this question. It's also a matter of taste and preference.

I normally use strictly global variables in very small applications. An example might be a test application, where I am trying something out.

I almost never consider globals and pointers as interchangeable. If it's a value which I wish to modify remotely, I would first consider using a reference over a pointer. If its a value such as an int, (float, char, bool,) I would also consider passing it as a value alone; especially if I don't want the original value modified.

Now you may have an instance where you would use a pointer to a global. Suppose you have two variables meant to track similar (or interchangeable) data.

--



As an example, suppose you want to track two counters.int nCountA = 0;

int nCountB = 0;

So you want to modify these or do whatever to them, but they both are manipulated in the same way. You could write a function;void ChangeCount( int *pnCount ){

*pnCount ++; // remember to dereference the pointer!
if(*pnCount == 100) *pnCount = 0;
}

You would pass a pointer to your target counter;ChangeCount( &nCountA ); // modify counter A


As I said before, you could use references instead, but supposing it gets more complex this could be better.




Suppose you want to count on two hands. In other words, you want to alternate counters. You could do this;int nCountA = 0;

int nCountB = 0;
int *pnCountX = &nCountA;

You would keep the ChangeCount() function the same, but you could call it like this;ChangeCount( pnCountX );


--


Sorry for such a long post. I hope this clears up a few things, and perhaps gives a few ideas.

Cronholio
03-20-2006, 08:41 AM
If you are questioning why use pointers rather than global variables, then you don't really understand what a pointer is, I'm afraid to say. They are nothing alike. A global variable is just like any other variable, it contains a value but it's scope is not limited (unless you use an access modifier). A pointer contains a memory address, not a variable. You can access the value at the address through the pointer. In addition to modifying out of scope variables at specific addresses from within functions, probably one of the best uses for a pointer is when working with arrays, classes and structs. Generally pointer arithmetic can be used to access and sort array elements much faster than indexing the array, and when dealing with multiple structs and classes of the same type, it's much easier to access like members of those classes and structs through the same pointer interface. Then there are cases where there is no substitute for the use of pointers, like API functions, linked lists and filehandling. You are going to find that when you programs start getting more complex and you are dealing with creating windows and manipulating files that many of the associated functions return pointers of some sort or expect pointers as arguments.

And there's nothing wrong with using global variables or global constants if the situation calls for it. Just don't go overboard.

CGTalk Moderation
03-20-2006, 08:41 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.