|
|
|
|
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


2007 Oct 19 • 5404
57,583 ₧
|
|
|
|
|
|
2011 Feb 1
|
|
|
sprinkles


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
|
|
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5404
57,583 ₧
|
|
|
|
|
|
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


2007 Oct 19 • 5404
57,583 ₧
|
|
|
|
|
|
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



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


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


2007 Oct 19 • 5404
57,583 ₧
|
|
|
|
|
|
Nov 16
|
|
|
|
|
|
|
Nov 17
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5404
57,583 ₧
|
|
|
|
|
|
Nov 18
|
|
|
|
|
SRAW
Rocket Man

2007 Nov 6 • 2376
601 ₧
|
SPRINKLES GOT PWNED
|
|
|
|
|
Nov 18
|
|
|
|
Pages: [1]
|