![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
Pointers! Why?
I have just about grasped the idea of pointers, but i am still confused as to why you would need them.
This code is taken from C++ Without Fear by Brian Overland Consider this: #include <iostream>
using namespace std;
void double_it(int *p);
int main()
{
int a = 5, b = 6;
cout << value of 'a' before doubling it: " << a << endl;
cout << value of 'b' before doubling it: " << b << endl;
double_it(&a);
double_it(&b);
cout << value of 'a' after doubling it: " << a << endl;
cout << value of 'b' after doubling it: " << b << endl;
return 0;
}
void double_it(int *p)
{
*p = *p * 2;
}How is this anymore useful/effective/amicable than the following? #include <iostream>
using namespace std;
int double_it(int p);
int main()
{
int a = 5, b = 6;
cout << value of 'a' before doubling it: " << a << endl;
cout << value of 'b' before doubling it: " << b << endl;
a = double_it(a);
b = double_it(b);
cout << value of 'a' after doubling it: " << a << endl;
cout << value of 'b' after doubling it: " << b << endl;
return 0;
}
int double_it(int p)
{
p = p * 2;
return p;
}
__________________
www.heldtogether.co.uk |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3
![]() |
The differences, the one with pointer allow you to modified the variables passed to the function. they are passed by ref meaning when the pointer variable is changed. the variables passed to the function is also changed. the other code you posted, simply pass by val, meaning it will be only be changed when you use '=' to the function(var). The use of pointer in my opinion makes it easier to modify a variable by using the address. I hope it is the right answer.
|
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
The first one modifies the variables directly, the second makes a copy of the variable passed. This might not be a big deal in the case above but when working with huge objects, making a copy will make your program memory inefficient.
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
I understand what it does, but why would you want to use pointers? As far as i can see it doesn't make it any easier or anything, just a different way of doing things...
__________________
www.heldtogether.co.uk |
|
|
|
|
|
#5 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,007
Rep Power: 5
![]() |
There are a lot of reasons. Bear in mind that in C++ (not C), you can use reference types as an alternative to passing pointers. That said, the two are the same principle; using reference variables in C++ is essentially a shortcut for passing pointers, letting the compiler worry about generating the appropriate indirection, rather than having to do it by hand.
A few examples of where you might want to pass a pointer (or reference) include: What you are passing is large, such as a struct or class. Data in C/C++ is passed to functions on the stack (barring certain compiler-specific extensions), and this is not an efficient way to pass large items. It allows you to modify the original variable, rather than a local copy. You point out in your example that you could just return a value, and while this is true in your example, in many, it is not. Imagine you wished to modify several values; assigning through return only lets you modify one. If what you are dealing with is an allocated block of memory (such as an array), it makes sense to pass a pointer to that block (or a subsection of it). In fact, short of passing every single memory location as a separate parameter, passing a pointer is the only way. If you have a function that swaps two items, and you want them to actually be swapped in the caller, not just the callee, using pointers is the way to do this. This approach is used heavily in sorting. In fact, often you might swap pointers to the data objects themselves, meaning you're not merely dealing with pointers, but instead pointers to pointers. This allows you to do a sort or similar operation as efficiently as possible (assumign your algorithm is likewise efficient), because it's much less work to swap a pair of pointers (generally four bytes on modern systems) than objects which may be dozens or hundreds of bytes- maybe even more.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
AH ha i see! Thanks lectricpharaoh, that's cleared that up. I can definately see where you're coming from.
Cheers!
__________________
www.heldtogether.co.uk |
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Dec 2005
Posts: 1
Rep Power: 0
![]() |
Quote:
Let us say we have an array of numbers for examples sake we have an array of ten thousand numbers. Now if we are using an array and we want to delete the first element we would have to shift the array 9999 elements. This would require a relatively long time since we have a lot of operations to perform. Are you following this logic to date? If we are using pointers we can create a type of list called a linked list. A linked list is a list of elements that uses pointers to reference the next element. For example one element of the array would contain a value and a pointer to the next element. Now back to the case of the ten thousand numbers. If we are using a linked list and we want to delete the first element we simply change where the pointer points and voila we are now done. The difference in efficiency is staggering is it not? One operation versus 9999 operations for the array. If your still confused look up a tutorial that explains linked lists in further detail. |
|
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
Yeah i kinda get it... when i've thought about for a while i'll get it solidly
![]()
__________________
www.heldtogether.co.uk |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Have you ever tried to use "malloc" without a pointer?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Programmer
|
Pretty much the only time pointers are used in my programs, as far as I can think of, is in constructors and destructors for classes.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|