pointers

pointers

sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
What's the purpose of pointers? Everyone uses them. Don't they refrence things?
 
 
2011 Jan 31 at 18:01 PST
SuperJer
Websiteman

2005 Mar 20 • 5579
A pointer a variable which contains an address.

For example let's say I have a byte-sized variable called pissant and it contains the value 0x42. The value 0x42 is stored at an actual position in memory, let's say 0xDEADBEEF.

So your memory looks kind of like this:

code

Address--- Value
0xDEADBEE8 ?
0xDEADBEE9 ?
0xDEADBEEA ?
0xDEADBEEB ?
0xDEADBEEC ?
0xDEADBEED ?
0xDEADBEEE ?
0xDEADBEEF 0x42
0xDEADBEF0 ?
0xDEADBEF1 ?
0xDEADBEF2 ?


I put ?s in because like who the fuck knows what is in those other positions.

Now let's say you want to have a pointer to your pissant variable. Let's call it pointy. OK. It also has to have a position in memory. Let's store it at 0xDEADBEE8.

This is a 32-bit machine (I've decided) and therefore your pointer is got to have 4 bytes.

OK so like check out your memory now, kids:

code

Address--- Value
0xDEADBEE8 0xDE
0xDEADBEE9 0xAD
0xDEADBEEA 0xBE
0xDEADBEEB 0xEF
0xDEADBEEC ?
0xDEADBEED ?
0xDEADBEEE ?
0xDEADBEEF 0x42
0xDEADBEF0 ?
0xDEADBEF1 ?
0xDEADBEF2 ?


You see that?!?!

The four bytes starting at 0xDEADBEE8 (which is the variable pointy) is a pointer to the position 0xDEADBEEF which just happens to be where pissant lives.

You got that?!!!

A pointer is a variable that contains the address of something... else.
 
 
2011 Jan 31 at 22:11 PST — Ed. 2011 Jan 31 at 22:14 PST
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5485
57,583 ₧
Pointers are fun. See how quickly you can SegFault by iterating a pointer and writing over each memory address!
 
 
2011 Feb 1 at 11:53 PST
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
Sorry I should have been more specific. I know what a pointer is. I jus don't know what the point is for using them.
So when do you use a pointer? And why?
 
 
2011 Feb 1 at 18:50 PST
Killer-Duck
Homicidal Anatidae

2008 Mar 5 • 1169
633 ₧
Quote:

Pointers to data significantly improve performance for repetitive operations such as traversing strings, lookup tables, control tables and tree structures. In particular, it is often much cheaper in time and space to copy and dereference pointers than it is to copy and access the data to which the pointers point.

Pointers are also used to hold the addresses of entry points for called subroutines in procedural programming and for run-time linking to dynamic link libraries (DLLs). In Object-oriented programming, pointers to functions are used for binding methods, often using what are called virtual method tables.


See more at: http://en.wikipedia.org/wiki/Pointer_%28computing%29
QUACK! QUACK!
 
 
2011 Feb 2 at 00:07 PST
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
Can I have a real life example? I still can't get the concept.
 
 
2011 Feb 2 at 19:22 PST
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5485
57,583 ₧
Well, when you do myClass thingy = new myClass(), in C++ (as an example) you might do so with a pointer to thingy, rather than thingy itself. myClass might be really heavy on memory so it is better to move a pointer around and leave the class on the heap than it is to "directly" use the class.

These are the kind of issues that don't come up in managed languages, like Java and large wads of .NET.
 
 
2011 Feb 3 at 03:49 PST
SRAW
Rocket Man

2007 Nov 6 • 2525
601 ₧
That was indeed a very clear explanation dr...
Free Steam Games
 
 
2011 Feb 3 at 05:07 PST
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
So the pointer acts jus like the class?
 
 
2011 Feb 3 at 10:07 PST
buq25

2008 Jul 5 • 572
295 ₧
sprinkles said:
So the pointer acts jus like the class?

But less resource costing?
Today's post brought to you by the letter: "heck".
 
 
2011 Feb 3 at 10:12 PST
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5485
57,583 ₧
In some sense. The pointer doesn't act like the class, it points to it. Because, you know, it's a pointer.
 
 
2011 Feb 3 at 10:21 PST
SuperJer
Websiteman

2005 Mar 20 • 5579
You don't need pointers. They are just one way of doing things.

In low level languages, and at the hardware level, pointers are everywhere, though. If you dig down enough, everything on a computer is about memory addresses.

Example use of a pointer:

Let's say you want to efficiently store the current state of the keyboard, and the previous state of the keyboard. In a game or something.

Let's say there's an API call GetKeyboardState( int array[255] ) to which you pass an array, and it fills it with a 0 or 1 at each position depending on whether each key is currently pressed down.

Let's make 2 arrays to hold the current and previous states:

code
int kb_state_0[255];
int kb_state_1[255];


Now let's make 2 pointers:

code
int * current_keyboard_state = kb_state_0;
int * previous_keyboard_state = kb_state_1;


Now everytime we read a new keyboard state, swap where the pointers are pointing, and read in the new data.

code
...
int * temp = previous_keyboard_state;
previous_keyboard_state = current_keyboard_state;
current_keyboard_state = temp;
GetKeyboardState( current_keyboard_state );
...


This is somewhat efficient because you don't actually have to move the whole 255-element arrays at any time. You just switch the pointers.

Hope I didn't screw that up...


 
 
2011 Feb 6 at 21:47 PST
adhesive

Find the Hole Participation Medal
Find the Hole II 1st Place Medal
2007 Sep 13 • 79
1,301 ₧
Ever send an array to another function?
code
int x[] = {1,2,3,4,5}
work(x);

void work(int numbers[]){
code...
}

Surprise, You secretly used pointers!
You actually sent &x[0] to work and the work function views int numbers[] as int* numbers. This way you can just send 4bytes (address of x[0]) to work instead of the whole array (4bytes * array length).
If you didn't use pointers you would have to send all elements to the work function, make a new array there, do the calculations, return all the modified elements to the function caller, and finally repopulate the original array. Try making a recursive program without pointers.


SRAW said:
hey a rare cameo by adhesive
 
 
2011 Feb 11 at 10:53 PST
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
I'm not sure I understand what you mean by move.


??
 
 
2012 Nov 15 at 22:41 PST
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5485
57,583 ₧
 
 
2012 Nov 16 at 08:48 PST
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
 
 
2012 Nov 17 at 23:50 PST
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
AHAHAHAHAHAHAHAHAHAHAHAHA
 
 
2012 Nov 18 at 09:21 PST
SRAW
Rocket Man

2007 Nov 6 • 2525
601 ₧
SPRINKLES GOT PWNED
Free Steam Games
 
 
2012 Nov 18 at 10:28 PST
Page [1]