Just started to learn C++ today and I've hit a spot of confusion regarding the '*' pointer.
I don't understand how it's used. All it seems to do is reverse the '&' pointer (ie. int *var = &var2 --> value of whatever var2 points to rather than memory reference).
Is that all it does or am I missing something?
This is a pointer of type int pointing to the address of var2:
int *var = &var2;
var contains the address of the var2 variable.
Alright. Thanks.
Pointers and addresses can get very confusing, especially because the same symbols are used in different contexts.
Building on what [b]florian[/b] said:
int *var = &var2;
In this example, * is being used to indicate the [i]type[/i] of var, specifically that it is a pointer to int (int*).
& is being used as an "address-of" operator or "reference" operator, which is an operator that gives the address of a variable, in this case var2.
[b]now, the interesting thing[/b] is that * can also be used [i]as an operator[/i], in which case it is called the "indirection" operator or "dereference" operator, which indeed does the opposite of the reference operator, which is to fetch whatever is at the location pointed to by the address stored in the variable.
let's modify the example a bit:
int var = 2; // variable 'var' of type int is assigned the number 2
int* ptr = &var; // variable 'ptr' of type int-pointer is assigned the address of var
int var2 = *ptr; // variable 'var2' of type int is assigned the value at the location stored in ptr, which holds the number 2
They can also be used when saving memory and time, for example if you needed an array of 500,000 vector objects (for example to store a scene) you'd probably want to use pointers so that you wouldn't end up wasting time + memory allocating over 6MB of memory. Instead you'd hold an array of 500,000 pointers and then when you'd only initialise the vectors that you use, when you use them
std::vector/std::deque/std::list much?
[QUOTE=Dotmister;23235995]They can also be used when saving memory and time, for example if you needed an array of 500,000 vector objects (for example to store a scene) you'd probably want to use pointers so that you wouldn't end up wasting time + memory allocating over 6MB of memory. Instead you'd hold an array of 500,000 pointers and then when you'd only initialise the vectors that you use, when you use them[/QUOTE]
I disagree, the fewer memory allocations the better as they usually take a pretty long time.
You don't need to use pointers in C++.
Prove me wrong.
yeah, [i]you[/i] don't.
[QUOTE=efeX;23237328]You don't need to use pointers in C++.
Prove me wrong.[/QUOTE]
You can though. It's actually useful in places you want a reference, but it's okay to not exist (NULL). And a reference must be defined upon creation and cannot be re-assigned.
Additionally, some APIs, especially C APIs, have some functions returning pointers.
I still fail to understand this. Surely the variable names themselves are pointers to memory locations?
I've always assumed that either a table is created at runtime, or perhaps more likely compilation, dictating that 'a' = (memory location) and etc.
You don't need a compiler, or syntactical sugar to program. Prove me wrong. [/being a dick]
[QUOTE=Jawalt;23242515]You don't need a compiler, or syntactical sugar to program. Prove me wrong. [/being a dick][/QUOTE]
Shrug. If you consider his comment 'being a dick' then you must not venture out of the Programming board much. I'm actually surprised I haven't been flamed by a hundred cunts for posting here. :V
[QUOTE=Killalot;23242473]I still fail to understand this. Surely the variable names themselves are pointers to memory locations?
I've always assumed that either a table is created at runtime, or perhaps more likely compilation, dictating that 'a' = (memory location) and etc.[/QUOTE]
Variable names aren't really "pointers" to memory locations, not in the sense of what the word "pointer" means in C++, but yes, the compiler does decide how the variables will be arranged in memory. This is done at compile time; each variable you define has a predetermined location that doesn't change.
Pointers, in contrast, hold memory addresses that can be determined [i]at runtime[/i].
Pointers are used with dynamic memory allocation. Suppose you want your program to ask the user for the name of a file, then read the file's contents into memory. Since you don't know in advance how big the chosen file will be, you can't declare an array of the necessary size in your program's source code. So instead you check the file's size at runtime and ask the operating system to allocate that much memory, using the new[] operator. What you get back is a pointer to the allocated memory, which you store in a pointer variable.
There's a distinction here: the pointer variable is an ordinary named variable whose location is chosen by the compiler, but the [i]contents[/i] of the pointer variable represent something created at runtime, that the compiler didn't know anything about. You've essentially created a new variable at runtime, and its address, stored in a pointer variable, is the "name" by which you access it.
Many of the container classes in the C++ standard library, such as std::vector and std::list, use dynamic allocation and hold pointers internally.
I can understand that a lot better. So basically it's a method of storing references to memory at runtime and is useless for static code (where array sizes are statically defined and etc) but useful for dynamic code where the input is unpredictable?
No...
Going off of this:
[QUOTE=Killalot;23242473]I still fail to understand this. Surely the variable names themselves are pointers to memory locations?
I've always assumed that either a table is created at runtime, or perhaps more likely compilation, dictating that 'a' = (memory location) and etc.[/QUOTE]
I think you are confused about the difference between a pointer declaration and the dereference operator, as was discussed by an earlier poster.
They are NOT the same thing AT ALL. The symbol is read in a completely different way by the compiler in different contexts. Saying
int *foo;
is not dereferencing the name, it is creating a pointer.
int bar= *foo;
is completely different; it is finding the stuff stored at the address held in foo, and putting it in bar.
Pointers just point to adresses in memory; if I'm not mistaken, the dynamic part comes in when you allocate memory on the heap using the "new" operator.
[editline]09:25AM[/editline]
Also, static/dynamic memory does not refer to if your program has input, it is just different ways to store memory. I'd explain it, but to be honest, my use of it works for me, but I probably wouldn't be able to explain when to use dynamic and when to use static very well. You'd be best reading an article or ask someone who knows this stuff really well.
[QUOTE=ryandaniels;23253338]
int *foo;
is not dereferencing the name, it is creating a pointer.
[/QUOTE]
A variable declaration and an expression are two different things, but the syntax isn't as inconsistent as you might think. You can think of it as declaring that *foo is an int (which implies that foo is a pointer to an int).
[QUOTE=efeX;23237328]You don't need to use pointers in C++.
Prove me wrong.[/QUOTE]
The STL library uses little more than a pointer as its iterator IIRC.
At least, without pointers the STL library wouldn't really do much. :)
It all depends on how strictly you're defining "need".
I mean doing things like. int *a = &b; no one does that shit. So why teach people that way.
uh, people do that shit.
When you have a block of memory and you want to access parts of it, such as getting pixel data from a bitmap.
[QUOTE=efeX;23265122]I mean doing things like. int *a = &b; no one does that shit. So why teach people that way.[/QUOTE]
Actually, people do that shit, just not as much as they used to do 'coz we've got references in C++.
So it's more like int & a = b; nowadays.
That's like saying 'a' is another name for 'b' isn't it?
[QUOTE=nos217;23281723]That's like saying 'a' is another name for 'b' isn't it?[/QUOTE]
It is, all operations on 'b' are performed on 'a' with the same syntax, including assignment and address-of (you can't have pointers to references).
I've preached about references for a long time now, but that's a surprisingly simple way of putting it. Thanks.
References are nicer when you want to work with the object rather than its address. When I have a pointer (or something pointer-like, such as an auto_ptr or iterator), I'll often do
[code]
Foo &foo = *pFoo; // Bind a reference to the pointed-to object
foo.doStuff();
doMoreStuff(foo);
// etc.
[/code]
because it looks nicer than
[code]
pFoo->doStuff();
doMoreStuff(*pFoo);
// etc.
[/code]
[QUOTE=Wyzard;23282199]References are nicer when you want to work with the object rather than its address. When I have a pointer (or something pointer-like, such as an auto_ptr or iterator), I'll often do
[code]
Foo &foo = *pFoo; // Bind a reference to the pointed-to object
foo.doStuff();
doMoreStuff(foo);
// etc.
[/code]
because it looks nicer than
[code]
pFoo->doStuff();
doMoreStuff(*pFoo);
// etc.
[/code][/QUOTE]
It's also more versatile, as you could change the code to using value types without changing much of the code. Isn't really much of a deal though, because there should be a good reason that pFoo exists as a pointer to begin with (like iterating over an array of pointers, etc).
You MUST use a pointer when you want to modify a variable passed to another function by main() since this value is actually called a pass by reference and is a copy of the original variable.
That's true in C, but C++ has references, which are better-suited to pass-by-reference (go figure) than pointers.
Thank you very much for this enlightenment,now where can i find some information on how to use a reference?
Google search. Basically, references let you pass a non-pointer into a C++ function and let the function modify its value. It's really just a shortcut of pointers.
Sorry, you need to Log In to post a reply to this thread.