[Dev-C++] What's the difference? Pointers and regular variables
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Samuel K. <ib...@ly...> - 2003-03-04 01:58:14
|
Pointers, as you have already stated, hold the address in memory of other variable(s). So what is the difference and when and when you should not use them? Well the difference is the fact that pointers hold an address, but I don't think that is exactly what you wanted, so I'm going to explain when you should use them.
Dynamic Allocation: That is a fancy two dollar word that simply means you can ask the system for more memory during runtime. For example:
int count;
int *dynamic;
cout << "\nPlease enter a number: ";
cin >> count;
if(count > 0)
dynamic = new int[count]; //asking the system for memory equal
//to size of int times count (an array
else //of int in other words)
cout << "\nBad value." << endl;
delete dynamic; //returning the memory to system when
//done with it.
You can also use pointers to save both memory and runtime. For example, when you pass an object whether it be a class, struct, or array, it uses up much more memory if the computer has to make a copy of that array, class, struct everytime the function is called. (Note: arrays are automatically called by reference which is similar to pointers. It also uses up the runtime required to make a copy. It might take a little time to copy a billion objects for every function you call. The third reason you might want to use a pointer is if you want to change the parameter to a function. For instance if you want to raise the salary of every worker in class allTheCompanysWorkers you will HAVE to pass it by either reference or pointer so you can change them values otherwise you would have to change them where ever you created them. That can make things more complicated than necassary. There is also one other reason to use pointers some functions in standard C and C++ might require a pointer
to a char array for instance or might return a pointer. In a situation like that you would also have to use pointer.
So when should I use regular variables, references, or pointers?
Simple you want to use regular variables in all regular conditions. The only times you don't want to use regular variables is in one of the four situations listed above. Otherwise you want to try to use call by reference if it is possible. The reason references are simpler is because they are just another name for the variable they refer to. They aren't anything new really. The reason you want to use reference if it is possible is because they are simpler than pointers. You can use reference variables if for instance you want to pass a large parameter but don't want the cost of copying everything in the object that you are passing. The only times you want to use pointers is for dynamic allocation and for type compatibility when calling a function someone else has written. I know that is a lot to understand right now, but keep programming and you will start picking up a feel for when you should use one over another.
_____________________________________________________________
Get 25MB, POP3, Spam Filtering with LYCOS MAIL PLUS for $19.95/year.
http://login.mail.lycos.com/brandPage.shtml?pageId=plus&ref=lmtplus
|