Pointers, why are they useful?
I cannot figure out the usuefulness of pointers. I have read many tutorials, and readmes and so on... none of them could give a good example of a useful pointer... To me they seem useless, but I know they aren''t, to many ppl talk about them.
I am a signature virus. Please add me to your signature so that I may multiply.
They''re mostly useful for accessing memory on the free store as well as passing data to functions by reference. I''m at work so I can''t go in-depth but you should be able to find stuff on those two subject with a Google search.
---signature---" Actually, at the time, I didn't give a damn about the FoxNews issue... just that you would come swooping in with your Super Mith cape flapping behind you and debunk it just because it didn't happen in your living room." - InnocuousFox
ok, im not exactly an expert, but when i first found points, i asked the same question.
what i have mainly been using pointers for, is to dynamically resize arrays.
now, i think that works...if not, its an example of a use
[edited by - StormUK on February 28, 2003 1:35:37 PM]
what i have mainly been using pointers for, is to dynamically resize arrays.
#include <iostream.h>int main {int width=0, height=0;int *myArray; //the declaration of the arraymyArray = new int[20]; //creats an array size 20delete [] myArray; //somthing to do with memory leaks *shrug*return 0;}
now, i think that works...if not, its an example of a use
[edited by - StormUK on February 28, 2003 1:35:37 PM]
You can easily accomplish that with static arrays. Here's a somewhat better example.
You can't do something like that with normal arrays. What's inside the brackets of an array declaration must be a number or a constant, since the compiler must be able to determine the size of the array at compile time. Dynamic arrays solve this problem.
Of course, pointers have many other useful applications. Polymorphism (if you know about classes and inheritance) relies on them. You can't copy or compare blocks of memory without pointers (try writing your own memcpy() or strcmp() functions).
[edited by - micepick on February 28, 2003 1:33:59 PM]
[edited by - micepick on February 28, 2003 1:34:40 PM]
#include <iostream>int main(){int length;int *myArray; //the declaration of the arraystd::cin >> length;//creats an array whose size can only be determined at runtimemyArray = new int[length]; // if you don't do the following, the memory used for the array// will be lost until you restart the computer (or the OS cleans// it up for you)delete [] myArray; return 0;}
You can't do something like that with normal arrays. What's inside the brackets of an array declaration must be a number or a constant, since the compiler must be able to determine the size of the array at compile time. Dynamic arrays solve this problem.
Of course, pointers have many other useful applications. Polymorphism (if you know about classes and inheritance) relies on them. You can't copy or compare blocks of memory without pointers (try writing your own memcpy() or strcmp() functions).
[edited by - micepick on February 28, 2003 1:33:59 PM]
[edited by - micepick on February 28, 2003 1:34:40 PM]
If you want a bunch of good examples where pointers are really usefull, pick up a book on data structures.
the real power of pointers is access to dynamic memory even beyond dynamic arrays (as was mentioned by micepick). take a simple address book example. the entire function of your program is to store addresses. you have no idea how many addresses the user will enter. it could be 5, it could be 500 or even 5000. only with pointers could you handle such a system.
with pointers you can create complex data structures within dynamic memory. google: linked list, binary tree (think BSP tree), quad trees & oct trees (also relevant to game programming).
you can also have properly managed textures, bitmaps and whatnot in your game. say you have 5 objects each with the same texture. rather than loading the texture seperately for each of the 5 objects, you could just load one instance of the texture and give each of your models a pointer to that texture thus saving on lots of memory space, but still maintaining the "illusion" that each object has access to it's own texture. perhaps not the best example, but it should give you the idea.
-me
[edited by - Palidine on February 28, 2003 1:41:16 PM]
with pointers you can create complex data structures within dynamic memory. google: linked list, binary tree (think BSP tree), quad trees & oct trees (also relevant to game programming).
you can also have properly managed textures, bitmaps and whatnot in your game. say you have 5 objects each with the same texture. rather than loading the texture seperately for each of the 5 objects, you could just load one instance of the texture and give each of your models a pointer to that texture thus saving on lots of memory space, but still maintaining the "illusion" that each object has access to it's own texture. perhaps not the best example, but it should give you the idea.
-me
[edited by - Palidine on February 28, 2003 1:41:16 PM]
I have no way of getting books so I have to stick only with network... believe me, I would have already been more knowledgeable if I could have gotten books.
I am a signature virus. Please add me to your signature so that I may multiply.
I don''t know if you''re using C or C++, but same thing goes with both, here''s it in C-style:
If you want to create a variable or a structure of some sort during runtime, you allocate space in memory for the program to use. That means you need pointers to point at the memory you just created, otherwise you wouldn''t know how to access it!
int *pointer; <-- // *pointer doesn''t point at a
// valid memory-location
pointer = malloc(......) //reserve memory for an int
// and let pointer point at it
// ie. pointer = address to free
// memory
*pointer = 1234; // write 1234 to address
Note that pointer = The address that pointer points at
*pointer = the data the current address holds
just like
int anumber = 5;
anumber = the data that anumber contains
&anumber = the address where the data (5) is located.
So if you need new chunks of memory were to store more data under runtime, you have to allocate it, and if you want to use it, you have to have something pointing at it, the exact same way you need to type
int mynumber = 5; if you want to store the number 5, right?
And then pointers are useful to other things aswell, when you want to pass things by reference as Utwo already said, that is, pass a variable to a function, and not a copy of it.
int main()
{
int anumber = 5;
functionA(anumber); // send 5 to function
printf("%d", anumber); // prints out "5"
functionB(&anumber); // give the address of anumber to
// function
printf("%d", number); // prints out "1234"
}
void functionA(int a)
{
a = 1234; // local variable in functionA gets 1234
}
void functionB(int *a)
{
*a = 1234; //the data in the address of a is now 1234
} // (which happens to be the address of anumber)
If you want to create a variable or a structure of some sort during runtime, you allocate space in memory for the program to use. That means you need pointers to point at the memory you just created, otherwise you wouldn''t know how to access it!
int *pointer; <-- // *pointer doesn''t point at a
// valid memory-location
pointer = malloc(......) //reserve memory for an int
// and let pointer point at it
// ie. pointer = address to free
// memory
*pointer = 1234; // write 1234 to address
Note that pointer = The address that pointer points at
*pointer = the data the current address holds
just like
int anumber = 5;
anumber = the data that anumber contains
&anumber = the address where the data (5) is located.
So if you need new chunks of memory were to store more data under runtime, you have to allocate it, and if you want to use it, you have to have something pointing at it, the exact same way you need to type
int mynumber = 5; if you want to store the number 5, right?
And then pointers are useful to other things aswell, when you want to pass things by reference as Utwo already said, that is, pass a variable to a function, and not a copy of it.
int main()
{
int anumber = 5;
functionA(anumber); // send 5 to function
printf("%d", anumber); // prints out "5"
functionB(&anumber); // give the address of anumber to
// function
printf("%d", number); // prints out "1234"
}
void functionA(int a)
{
a = 1234; // local variable in functionA gets 1234
}
void functionB(int *a)
{
*a = 1234; //the data in the address of a is now 1234
} // (which happens to be the address of anumber)
1. Alot of the ''power'' of C pointers has been taken by C++ references
a reference is variable that holds the address of another variable.
eg.
int x = 10; //integer variable
int& r_x; //r_x holds the ''address'' of x.
Why is THIS useful?
say we want a function that adds 10 to an int.
passing by value, it would look like this:
int AddTen(int n)
{
n += 10;
return n;
}
x = AddTen(x);
a more common example is:
void Swap(int x, int y)
{
int temp = x; x = y; y = temp;
}
int a = 10;
int b = 20;
Swap(a,b);
cout << "a: " << a <[output]
>a: 10
>b: 20
But, if we use references, the code might look like:
void Swap(int& x, int& y)
{
int temp = x; x = y; y = temp;
}
int a = 10;
int b = 20;
Swap(a,b);
cout << "a: " << a <[output]
>a: 20
>b: 10
Why does this happen? When you call a fucntion by value, it makes COPIES fo the values passed into it - thus the function is actually operating on the variables, but the values they contain.
It''s like saying Does the operation on these variables that hold the VALUE of ''a and ''b''
And remember, you couldn''t ''return'' these values, because you can only return 1 value (often best left as an error code)
Also, there is a performance gain when calling by reference, because there is no copy being made (which can be REAL expensive when using classes)
a reference is variable that holds the address of another variable.
eg.
int x = 10; //integer variable
int& r_x; //r_x holds the ''address'' of x.
Why is THIS useful?
say we want a function that adds 10 to an int.
passing by value, it would look like this:
int AddTen(int n)
{
n += 10;
return n;
}
x = AddTen(x);
a more common example is:
void Swap(int x, int y)
{
int temp = x; x = y; y = temp;
}
int a = 10;
int b = 20;
Swap(a,b);
cout << "a: " << a <[output]
>a: 10
>b: 20
But, if we use references, the code might look like:
void Swap(int& x, int& y)
{
int temp = x; x = y; y = temp;
}
int a = 10;
int b = 20;
Swap(a,b);
cout << "a: " << a <[output]
>a: 20
>b: 10
Why does this happen? When you call a fucntion by value, it makes COPIES fo the values passed into it - thus the function is actually operating on the variables, but the values they contain.
It''s like saying Does the operation on these variables that hold the VALUE of ''a and ''b''
And remember, you couldn''t ''return'' these values, because you can only return 1 value (often best left as an error code)
Also, there is a performance gain when calling by reference, because there is no copy being made (which can be REAL expensive when using classes)
Even at my father''s funeral - I farted.-Japanese Haiku [anonymous]
ok, the real place you NEED pointers, is when 2 things each need to know about each other ... in this case (a circular association) they cannot contain each other (how can something conatin a different thing, which contains the original thing?) ... so at least 1 must POINT to the other ...
simple example ... if you we''re implementing a class scheduling system .. and you have Students, and Classes (i''m omiting allt he other stuff you''d want) ...
A student has a list of classes, and a class has a list of students ... but the student can''t OWN the class, cause what would happen if the student changed classes, where would the other students look up class information? and how would they know to get it from the first student who registered ...
The classes likewise cannot own the students, because the student information (such as grade point average, and disipline record) must be maintained ever between classes, and years ...
So a student would contain a current schedule which would contain POINTERS to classes they are in ...
And a Class could contain a list of registered students, which would be POINTERS to student information ...
simple example ... if you we''re implementing a class scheduling system .. and you have Students, and Classes (i''m omiting allt he other stuff you''d want) ...
A student has a list of classes, and a class has a list of students ... but the student can''t OWN the class, cause what would happen if the student changed classes, where would the other students look up class information? and how would they know to get it from the first student who registered ...
The classes likewise cannot own the students, because the student information (such as grade point average, and disipline record) must be maintained ever between classes, and years ...
So a student would contain a current schedule which would contain POINTERS to classes they are in ...
And a Class could contain a list of registered students, which would be POINTERS to student information ...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement