Homepage   Search   Register   Log in SUPERJER FORAMS

pointers

pointers

Programming Help

Pages: [1]
sprinkles

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

2005 Mar 20 • 3883
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.
    (Edited 2011 Jan 31)     2011 Jan 31
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5404
57,583 ₧
Pointers are fun. See how quickly you can SegFault by iterating a pointer and writing over each memory address!
I give a little into the moment like I'm standing at the edge (I know)
That no-one's gonna turn me 'round
Just one more step, I could let go
    2011 Feb 1
sprinkles

Chrome Whore
2009 Sep 6 • 2545
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
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
sprinkles

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

Find the Hole II Participation Medal
2007 Oct 19 • 5404
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.
I give a little into the moment like I'm standing at the edge (I know)
That no-one's gonna turn me 'round
Just one more step, I could let go
    2011 Feb 3
SRAW
Rocket Man

2007 Nov 6 • 2376
601 ₧
That was indeed a very clear explanation dr...
Free Steam Games
    2011 Feb 3
sprinkles

Chrome Whore
2009 Sep 6 • 2545
10 ₧
So the pointer acts jus like the class?
    2011 Feb 3
buq25

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

But less resource costing?
Roses are red,
Violets are blue.
Shouldn't violets be violet,
and roses be rose?
    2011 Feb 3
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5404
57,583 ₧
In some sense. The pointer doesn't act like the class, it points to it. Because, you know, it's a pointer.
I give a little into the moment like I'm standing at the edge (I know)
That no-one's gonna turn me 'round
Just one more step, I could let go
    2011 Feb 3
SuperJer
Websiteman

2005 Mar 20 • 3883
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
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
sprinkles

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


??
    Nov 15
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5404
57,583 ₧
    Nov 16
sprinkles

Chrome Whore
2009 Sep 6 • 2545
10 ₧
    Nov 17
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2026
AHAHAHAHAHAHAHAHAHAHAHAHA
    Nov 18
SRAW
Rocket Man

2007 Nov 6 • 2376
601 ₧
SPRINKLES GOT PWNED
Free Steam Games
    Nov 18

Pages: [1]
Forum and design copyright © 2008-2013 SuperJer.com