| | |
Purpose of Pointers?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I have been reading about pointers, which IS confusing. I'm sure there is a purpose, but I don't know why I need them yet. For example
I think I might know what pointers do. I don't, however, know what the practical use of a pointer is. From what I've learned about pointes, is that they directly give the value of whats at that location in memory.
My question is: what is the practical use of pointers?
Examples && Explanations = *Welcomed :cheesy:
var1 = 23; // assigning 23 to var1 var2 = var1; // assigning the value of var1 to var2 var3 = &var1; // assigning the address of var1 to var3
I think I might know what pointers do. I don't, however, know what the practical use of a pointer is. From what I've learned about pointes, is that they directly give the value of whats at that location in memory.
My question is: what is the practical use of pointers?
Examples && Explanations = *Welcomed :cheesy:
If in doubt, reach into the trash can and remove the user guide.
•
•
Join Date: Nov 2004
Posts: 13
Reputation:
Solved Threads: 1
i havent slept in days and i hope i dont say something completely stupid here:
pointers contain the memory location of a variable. one of the reasons that this is useful concerns memory efficiency. if you're passing a variable to a function then the function has to make another copy of that variable and if that variable is an object of type NUCLEAR_REACTOR_PLAN, then you might run out of memory mighty fast. but if you pass a variable which points to the NUCblahblahblah variable's address, then you dont have to make the copy, you can just work with the original. Another potential use of pointers is in the implementation of a template class.
pointers contain the memory location of a variable. one of the reasons that this is useful concerns memory efficiency. if you're passing a variable to a function then the function has to make another copy of that variable and if that variable is an object of type NUCLEAR_REACTOR_PLAN, then you might run out of memory mighty fast. but if you pass a variable which points to the NUCblahblahblah variable's address, then you dont have to make the copy, you can just work with the original. Another potential use of pointers is in the implementation of a template class.
•
•
Join Date: Dec 2004
Posts: 22
Reputation:
Solved Threads: 1
I got the similar problem with you, Geek-Master. What kaiser<lucy> said is still a little bit abstract for me.
So Geek-Master have you tried the dynamic memory allocation using new and delete operator? They could only deal with pointers.
Now my own problem comes. Books say we can use new operator to reserve memory for array of unknown size like this:
<code>
int* ptr = new int[100]; //the array ptr has interger type and 100 elements
</code>
Now another version:
<code>
int n;
int ptr[n];
cin >> n; //ask user to enter size of array, now array has size
</code>
So what is the difference between the two versions of array declaration?
In both cases the array is declared but doesn't have defined-size unless it is determined during runtime.
So Geek-Master have you tried the dynamic memory allocation using new and delete operator? They could only deal with pointers.
Now my own problem comes. Books say we can use new operator to reserve memory for array of unknown size like this:
<code>
int* ptr = new int[100]; //the array ptr has interger type and 100 elements
</code>
Now another version:
<code>
int n;
int ptr[n];
cin >> n; //ask user to enter size of array, now array has size
</code>
So what is the difference between the two versions of array declaration?
In both cases the array is declared but doesn't have defined-size unless it is determined during runtime.
•
•
Join Date: Dec 2004
Posts: 22
Reputation:
Solved Threads: 1
Let me quote the lecture notes from my school:
The above example they call it 'dynamic array'. The value of n is essentially unknown during compilation time. Am I right? Now look at this:
The size of n is also not known until the programme is run. Then what is the point to use pointer and new operator to declare the array in the first case?
C++ Syntax (Toggle Plain Text)
int main(void) { cout << “How many students? “; cin >> n; //Declare an array to hold students' information int *grades = new int[n]; . . . . . . . . . . . . . . . . . . }
C++ Syntax (Toggle Plain Text)
int main(void) { cout << “How many students? “; cin >> n; //Declare an array to hold students' information int grade[n]; . . . . . . . . . . . . . . . . . . }
>Then what is the point to use pointer and new operator to declare the array in the first case?
Because the second case is ill-formed and thus, illegal in C++. Array sizes in C++ must be constant integral values. If it works when you try it then that's because your compiler allows it as an extension. Only the first case is guaranteed to be correct (assuming n is already defined).
Because the second case is ill-formed and thus, illegal in C++. Array sizes in C++ must be constant integral values. If it works when you try it then that's because your compiler allows it as an extension. Only the first case is guaranteed to be correct (assuming n is already defined).
I'm here to prove you wrong.
Pointers are still a vast subject that I am still working on to learn. I still need time to look over all of the technical writing of it all. I just needed to grasp the purpose of them.
by: kaiser<lucy>
I can kinda see the purpose now, dealing with memory usage. Thanks :cheesy:
•
•
•
•
but if you pass a variable which points to the NUCblahblahblah variable's address, then you dont have to make the copy, you can just work with the original. Another potential use of pointers is in the implementation of a template class.
I can kinda see the purpose now, dealing with memory usage. Thanks :cheesy:
If in doubt, reach into the trash can and remove the user guide.
pointers are c++ most useful things and is what sets it aside from other languages. Just to name a few uses:
Dynamic arrays
Linked lists
Memory management (eg. passing pointers rather than objects, alloc'ing memory ect..)
Binary Trees and Heaps (great for gaming)
Speed improvement (you can simplify code in some cases with pointers/function ptrs)
the last few being especially important in game programming.
If you still arent sure they are useful then try this: make a list class (or a set of functions) that can grow and shrink in size BUT not lose the contents (so it is NOT dynamic arrays...) without using STL (which, I believe, is a steam based linked list anyway). TIP: You need a pointer to the next and previous list item for it to work!
Dynamic arrays
Linked lists
Memory management (eg. passing pointers rather than objects, alloc'ing memory ect..)
Binary Trees and Heaps (great for gaming)
Speed improvement (you can simplify code in some cases with pointers/function ptrs)
the last few being especially important in game programming.
If you still arent sure they are useful then try this: make a list class (or a set of functions) that can grow and shrink in size BUT not lose the contents (so it is NOT dynamic arrays...) without using STL (which, I believe, is a steam based linked list anyway). TIP: You need a pointer to the next and previous list item for it to work!
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
Similar Threads
- pointers and classes (C++)
Other Threads in the C++ Forum
- Previous Thread: passing an array of structs from a member function
- Next Thread: Editor Question
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






