Call computeSphere(r, &a, &v);
& will pass the address of the variable and the value can be changed in the function.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
That solved the problem, It turned out I needed to use pointers to solve the question
Preferably references. Pointers are a waste of processor when you can use references instead.
Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
Preferably references. Pointers are a waste of processor when you can use references instead.
Serious question: What's the difference...
I thoughreference was just the C++ term for pointer, like folder is the Winblows term for directory.
And if they are different, how are pointers a waste of processor?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
A pointer is an object. It's usually the same size an an int, it stores a single numerical value (i.e. the memory address to which it "points"), and has the special abilities of "being dereferenced" to get the object it points at, and pointer arithmetic when adding or subtracting to it.
int a;
int* b;
In this code, I made two objects. One, an int, nameda, which probably takes up 4 bytes; and another object, a pointer (to an int), named b, which probably takes up another 4 bytes (maybe 8, depends on your system etc).
A reference is essentially another name for the exact same object;
int a;
int& b;
In this code, I created only one int. Just one.b is another name for the int known as a. Thus, the reference does not take up any more memory; it's not an object. It's just another name for some object (note that how a compiler makes references work is up to the compiler - whilst smart compilers don't waste time and memory, it's not mandated, and behind the scenes I think there's nothing stopping them being bloody silly and doing something expensive; I haven't read the C++ 11 definition yet, so if that has now changed, feel free to tell me!)
When I pass a pointer into a function, I am passing-by-value. The pointer is copied and passed into the function. This takes up time, to make the copy, and memory, to hold onto it. If all I want to do is make sure that the function can affect the original object in the calling code, it seems a waste to make pointers and copy them around, when I could pass in a reference.
Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
For those just joining in, here's passing-by-reference in play.
void makeAnIntBigger(int& input)
{
input++;
}
int a = 7;
makeAnIntBigger(a);
// now, a has the value 8, and I didn't have to make any pointers, which costs time and memory
When I pass-by-reference, no extra variables are made and copied; the function gets the actual object.
Here's more reference fun:
#include <iostream>
int main()
{
int a = 7;
int& b = a;
int& c = b;
b++;
c++;
std::cout << a << " " << b << " " << c;
return 0;
}
Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
Sounds to me like a reference is a pointer with hidden code to make it look like a variable. The function has to get either
1) the value of the variable, which is local or
2) the address of the variable, which is a reference.
No need to continue this here. I'll look up the inner workings -- unless Narue chimes in. I like the way she explains things...
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Sounds to me like a reference is a pointer with hidden code to make it look like a variable.
Since a pointer takes up memory, and can be dereferenced, and can have pointer arithmetic applied to it, and a reference does none of those things, they're clearly not at all the same and to think of a reference as just a pointer that's permanently dereferenced is missing the point.
The function has to get
1) the value of the variable, which is local or
2) the address of the variable, which is a reference.
A reference is not an address. A reference is the object. With references, the function can now also get
3) The actual object. Not a copy of it, not a pointer whose value is the address of it; just the actual, original object.
Really, references are not just some kind of permanently dereferenced pointer. They're worth reading up on. In the nicest possible way, to think of a reference as a kind of permanently dereferenced pointer is (a very common way) to miss the point.
But yes, this thread must die :)
Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117