Hard Light Productions Forums
Off-Topic Discussion => Programming => Topic started by: ethaninja on October 17, 2010, 12:53:58 am
-
Can anybody explain to me what pointers do in layman terms? I'm on this tutorial but he sort of Wikipedialized it so it is very hard to read without stressing for a brain aneurysm.
So if anybody knows how to dumb it done for those that are a little slower on the learning curves I would greatly appreciate it ;)
-
A pointer just points to someplace in memory. Instead of storing the actual data it just tells you where it's located, useful for strings and dynamic stuff.
-
What Scooby_Doo said. A pointer is the address of a specific location in memory; you can use it to put a piece of data there and then find it later. The actual value of a pointer is just an address value, not the data itself. There's a related term called "dereferencing," which basically means retrieving the data that a pointer is pointing to.
-
What Scooby_Doo said. A pointer is the address of a specific location in memory; you can use it to put a piece of data there and then find it later. The actual value of a pointer is just an address value, not the data itself. There's a related term called "dereferencing," which basically means retrieving the data that a pointer is pointing to.
Ah ok. So it's not actual data, but a location of where to find it sort of thing?
-
Correct.
Very rough example:
int value = 1 ;
int *pointerValue = &value ; // pointerValue now contains the address of where value is stored at.
print pointerValue ; // some goobly gook value
print *pointerValue ; // 1
value = 2 ;
print *pointerValue ; // 2
*pointerValue = 3 ; // Note i'm dereferencing the pointer (i.e. i'm assigning 3 to into the memory address pointerValue is keeping)
print value ; // 3
-
Watch the video: http://www.youtube.com/watch?v=i49_SNt4yfk&feature=related
-
Holy crap, Scooby Doo, you just cleared up the one thing in C++ that I didn't ever, ever, EVER understand.
-
Watch the video: http://www.youtube.com/watch?v=i49_SNt4yfk&feature=related
Haha that video is SWEET. Wish all tutorials were like that :P
Cheers for the reference
-
another way to think of pointers is as variable variables.
-
Also pointers are far faster to send as parameters for a function. Instead of copying all the data to the function call stack it just copies the pointer's value ( just a memory address).
Also use references when possible, they're safer than pointers.
-
You don't have to do anything special for that though. Big things like arrays get passed to functions as a reference automatically. ints don't need to be because they're the same size as pointers.
-
It seems a bit limiting to never use classes.
-
Pointers are necessary if you want a function to manipulate data of another function.
-
It seems a bit limiting to never use classes.
why would you never use classes?
-
You don't have to do anything special for that though. Big things like arrays get passed to functions as a reference automatically. ints don't need to be because they're the same size as pointers.
When did this change?
int MyHugeArray[MAXINT] ;
somefunction (MyHugeArray) ; // makes a copy of MyHugeArray
-
why would you never use classes?
The post above implied only using things that are small enough to pass by value or arrays which pass by reference implictly.
When did this change?
int MyHugeArray[MAXINT] ;
somefunction (MyHugeArray) ; // makes a copy of MyHugeArray
Never?
Arrays usually act like pointers.
-
You don't have to do anything special for that though. Big things like arrays get passed to functions as a reference automatically. ints don't need to be because they're the same size as pointers.
When did this change?
int MyHugeArray[MAXINT] ;
somefunction (MyHugeArray) ; // makes a copy of MyHugeArray
My understanding was that since arrays are pointers in c/c++, this is passing just the address of the start of the array, so this is effectively passing the array by reference. If, however, the array was wrapped in a class or struct, the whole thing would get copied.
-
Yeah, I know that passing an array into a function passes its address, so that the function manipulates the original array. You'd have to explicitly make a copy of the array if you didn't want the original to be changed.
-
why would you never use classes?
The post above implied only using things that are small enough to pass by value or arrays which pass by reference implictly.
When did this change?
int MyHugeArray[MAXINT] ;
somefunction (MyHugeArray) ; // makes a copy of MyHugeArray
I thought Arrays were used if you wanted to clone something?
Never?
Arrays usually act like pointers.
-
Also use references when possible, they're safer than pointers.
This isn't entirely valid. If you're using smart pointers, you may be passing the smart pointer class, but the pointer is actually NULL!
(I know, minor, subtle point, but it's caught people before - not only that, sometimes a pointer makes more sense!)
-
Also use references when possible, they're safer than pointers.
This isn't entirely valid. If you're using smart pointers, you may be passing the smart pointer class, but the pointer is actually NULL!
(I know, minor, subtle point, but it's caught people before - not only that, sometimes a pointer makes more sense!)
Then your not actually dealing with the pointer, but the class instead :P
And yes you can get a null pointer issue with references but it's something that doesn't come very easily, as compared to regular pointers.
-
Just pointing out :) I have heard people say that using references means that they're safe!
-
Well it's a lot harder to do stupid mistakes (forgot to give it an address...etc) with references than with pointers. Also references don't need to allocated and deallocated since the data they're pointing to is usually on the stack (unless you reference to a pointer [can you even do that?])
-
i still get hung up on pointers. seems every time i need to code something, like make a function return more than one value, and i know the only way to do that is with a couple of pointers in the arguments. but i still have to google it to figure out what symbols the dereference and address-of operators use. i always get those mixed up.:D
i know one thing that always confused me is that i figured the compiler reduced all variable names to an address anyway, so why would you use pointers over your typical named variables. of course its much easier to move around a memory address than it is to move around a several-byte struct. and or course c programmers use pointers regularly to make functions better at talking to whats calling them. but you really dont start appreciating pointers until you start getting into some more complicated areas of programming (such as object oriented code).
-
You don't actually need pointers for OO. Java doesn't have them, for instance. That's cause in Java you always pass by reference anyway for anything except primitives (integers, floating points, etc). You can do much the same in C and just use references instead of pointers if you want.
-
You don't actually need pointers for OO. Java doesn't have them, for instance. That's cause in Java you always pass by reference anyway for anything except primitives (integers, floating points, etc). You can do much the same in C and just use references instead of pointers if you want.
Technically, a reference is a kind of pointer. :)
EDIT: Apparently, it's actually the other way around. A pointer is a kind of reference variable:
The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations.