Outside of passing objects by reference, why would you want to access memory directly by using pointers?
Outside of passing objects by reference, why would you want to access memory directly by using pointers?
There are two aspects to using pointers:Outside of passing objects by reference, why would you want to access memory directly by using pointers?
1) Efficiency.
Whenever the data you are passing to a function is not a simple data type like int, double, etc. using a pointer is more efficient. Functions have to make a copy of whatever arguments are passed to them. When you pass a pointer, you are passing an address. An address is essentially an integer, and making a copy of an integer doesn't take up much room in memory. On the other hand, if you pass an object to a function, the function has to copy the whole object, and an object can have an enormous amount of data inside it, which takes up more room in memory.
2) Being able to change the original object you pass to a function.
If you pass an object to a function, the function makes a copy of the object for the function. If the function then makes changes to the object, the changes are made to the copy, and when the function ends, the copy is destroyed leaving the original object unchanged. Instead, if you pass a pointer to the function, the function copies the pointer. But, a copy of a pointer points to the same place in memory as the original pointer, so you can use the copy of the pointer to access the original object and change it. Like before, when the function ends, the copy of the pointer is destroyed, however the original object remains permanently changed. The original object remains permanently changed because you made changes directly to the original object--not a copy.
Last edited by 7stud; 05-09-2005 at 08:21 AM.
Oh, good call. Forgot my Java background.