I don't know when to use them. :X I know what they are and everything, but I don't know exactly WHEN to use them, when to use it with a variable/function. etc
Printable View
I don't know when to use them. :X I know what they are and everything, but I don't know exactly WHEN to use them, when to use it with a variable/function. etc
First, you have read the cprogramming tutorial on pointers, right?
Also, you have used say, std::vector before, correct? Have you considered how std::vector might be implemented?
Pointers, yes someone told me about vectors being generalized as pointers. :D
btw
So is this a reason people use pointers?Quote:
If you happen to have a huge piece of data that you want to pass into a function, it's a lot easier to pass its location to the function than to copy every element of the data!
Sorry if I made a few threads back about pointers, it's just a confusing topic at some parts. :<
Yes, though in C++ we would often pass a reference instead of a pointer in such a case.Quote:
So is this a reason people use pointers?
See references as a simplified syntax for pointers.
The only differences are
- A reference is always bound to a variable - it cannot be NULL.
- A reference cannot be rebound. Once it's bound, it can never be changed.
- References do not require * to dereference.
Basically, if a function requires something by reference, it means that argument isn't optional, as opposed to pointers where you can always pass NULL.
Pretty much, yes. A reference must always be bound to something, so just like const, it must be initialized.
So lets, you get a massive amount of data from a file, would you put that into pointer? Or would the file system do that for you and you just have to reference it????
I suggest thinking of references as aliases instead. A reference is another name for an existing object. Consequently, it must refer to an existing object, unlike a pointer. Pointer syntax does not come into the picture, simply because a reference is not a pointer.Quote:
See references as a simplified syntax for pointers.
You cannot put anything other than an address or zero into a pointer. You could put the data into memory, and then use pointers to access the memory.Quote:
So lets, you get a massive amount of data from a file, would you put that into pointer?
It really depends on how where/how you get/store the data.
Is it a typical raw buffer? Then a pointer might be a good choice so you can use it as an array.
If you store it in a vector or stuff, then it's better to pass via reference.
Ah, that's another good one. An alias it is!
No, references can be used on any type. Just append & after it.
BYTE&
int&
MyClass&
std::vector&
etc
No. In this context, when I say object, I mean "a contiguous region of memory holding a value of some type" or "a named or unnamed variable of some type".Quote:
Are references strict to classes/structs?
Another example is, what if you had a game physics engine, and you were trying to define skeleton bone structures ( :P ) for ragdolls, would you store any of that into a pointer?
PHP references are like C++ references. PHP5 objects are like Java objects, which behave like pointers without the pointer syntax, e.g., when you pass a PHP5 object as an argument, it is passed as if you are passing a pointer to the object. PHP4 objects are like C++ objects in that they are passed by value by default.Quote:
Sorry, I come from PHP where objects are just variables to classes.
Possibly, but you may also find that there are better ways, such as using a std::vector<bone> that contains the bones of that ragdoll. I presume, in this instance, a bone is defined as two end-points in x,y,z [,w] format, and the "meat/cloth" around the bone is built up by the code.
The biggest point (no pun intended) about pointers is that they can change what the refer to. Yes, you can use pointers to handle dynamic content [that is, hold a variable amount of data], but the generally accepted statement is that "if you can do it with a vector, you should" - and if you think you can't, you'd better have a solid motivation for that.
--
Mats
No it isn't. A vector is a storage class (container class) that allows you to add, remove, store and retrieve data. A pointer is a variable holding an address to something in memory. Whilst that can also allow you to add, remove, store and retrieve data, all of those mechanisms would have to be implemented by you.
--
Mats
A generalization of a pointer is an iterator.
A vector is not a pointer and I would keep them separate.
A vector is an array, if anything.
And an array, though similar to a pointer is not a pointer.
References "refer to objects", just like PHP references.
Pointers just store numeric memory addresses (which can even be 0, aka NULL), and you must use the dereference operator (*) and the address-of operator (&) with pointers, but not with references (it's done implicitly to make the syntax nicer).
In terms of usage, pointers are really only used in C++ for managing memory. When you want a memory address, consider passing a pointer, when you want an actual object (not just a copy, either), consider passing by reference.
> Pointers just store numeric memory addresses (which can even be 0, aka NULL)
Pointers are not numeric, but you can think of them that way, since they are usually mapped to a numeric addressing method.
Or in C++ terms: consider passing by reference if you can, and if not, pass by pointer.
BUMP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Could I just make all the variables/functions, pointers? :P
I suppose you could, but that would needlessly increase the complexity of your program with respect to programming and maintenance. Of course, functions are functions, except when they are converted to function pointers.Quote:
Could I just make all the variables/functions, pointers?
One of my biggest problems, which probably sounds kinda noobish, even though I understand the concept of WHY it's memory from other programs. :P But why memory?
Why do pointers use memory?
:S
By the way, are classes/structs pointers? cause I remember getting a compiler error that showed it was default by pointer.
What do you mean by "use"? Pointers "use" memory in the sense that variables use memory. Variables use memory because they store values. Values are stored in memory by design.Quote:
Why do pointers use memory?
No, though you can create pointers to class/struct types.Quote:
By the way, are classes/structs pointers?
Any variable, including a pointer, has a given size. A pointer may point to a region of memory only limited by what is available to the program.Quote:
So basically, non-pointer variables are limited by memory, and pointers are free to use as much memory? so to speak?
Make a good quote to thank someone ;)
Like
"Thanks laserlight! How the hell do you know everything?!"
:D