Could anyone point out what are the advantages/disadvantages of using pointers. Also when should a programmer always try to use pointers and when should he try to avoid using pointers.
Thanks.
Could anyone point out what are the advantages/disadvantages of using pointers. Also when should a programmer always try to use pointers and when should he try to avoid using pointers.
Thanks.
Humans make mistake because there is really NO patch for HUMAN STUPIDITY
The short answer (since the long one is beyond the scope of this forum):
As long as you can do something without pointers in a convenient way - do it without pointers.
Otherwise - use pointers - but be careful.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
Just don't force avoiding pointers. If it seems natural to use a pointer, do it. Just remember that copying a pointer doesn't imply copying the object pointed by it.
Well if you think hard you can do most of the things without pointers and there is less hassle has you don't have to allocate and free the memory. Moreover, using references can solve some of the things that can be done by pointers. So, guess what I was asking was the long answer. Any help would be appreciated.
Could you please specify in detail at least some of the scenarios when it would seem natural/ unnatural to use a pointer.If it seems natural to use a pointer, do it.
Humans make mistake because there is really NO patch for HUMAN STUPIDITY
Polymorphisem is implemented with pointers for example.Could you please specify in detail at least some of the scenarios when it would seem natural/ unnatural to use a pointer.
Reference is bound to its object, so every time you want to have the same reference point to other objects you use a pointer.
The long answer can't be answered here, all though wysotas answer and mine do gove it in a nutshell.
You should read some material on the subject.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
It's natural to use a pointer for linked lists or other objects which may be moved in memory. It's faster to copy pointers than to copy objects.
It's unnatural (in C++) to use pointers as an argument to function/method if you intend to change the object in the function. Use references instead.
Using pointers is nothing bad (although some people think different), so if you know how to work with them, you can use them whenever you want and ignore people saying that PointersAreBad(C).
I was kind of wondering, is pointers related to garbage collection ?? Although, I haven't yet come across a point where I need to delete the garbage because of memory overload, but could it be one possible usage.
I like to put things literally so,
Qt Code:
void myfunc() { struct mypointer* ptr; //... //... //..a long long function }To copy to clipboard, switch view to plain text mode
in case if we have a long function with many variables then all of their scope will last until the function ends and might cause memory overflow, however if we use pointers then we could free the memory ourselves so there will be less memory load. However, I have yet to come across some scenario, is it a possibility or am I thinking too much
Humans make mistake because there is really NO patch for HUMAN STUPIDITY
Related in what way?I was kind of wondering, is pointers related to garbage collection ??
Pointer which are holding memory that is no more used - AND that was dynaically allocated are pointer to grabage, and should be deleted.
Refering to your example, allocating on the stack is limited due to the limited stack size.
So it is very possible (hapanned to me) to allocate more than the stack can give - then you have to use dynamic allocation (pointers) or resize the stack.
It doesn't have to me many variables, it can be just one - but big, say a char array to hold a 16MB image...
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
hmm..let me get this right... if I have a character array which holds a 16 mb image data then it is going to be a problem because the array won't be destroyed until the end of the function right ?
but if we use a character pointer that holds the same amount of data then we have the flexibility of deleting the garbage data within the function itself and we don't have to wait till the end of the function...
I was kind of relating pointers to garbage collection in the sense that if a pointer is no longer used on the later half the function then we could free the memory of that pointer within the function i.e. an act of garbage collection. However if we have same amount of huge data say an array of 16 mb then we have to wait till the end of the function hence we cannot garbage collect...
However I am a bit confused about dynamic allocation of stacks. Do you mean that if I have big variable( say array) having around 32mb then I cannot create other variables and have to create pointers and allocate the memory ?
Humans make mistake because there is really NO patch for HUMAN STUPIDITY
No, it might be a problem because on some architectures you'll be trying to use more memory than it's available for you.
In general one uses pointers when he wants the variable to survive the end of the function, not to be destroyed earlier. You can achieve the same by using the stack (at least in theory) by using scopes:but if we use a character pointer that holds the same amount of data then we have the flexibility of deleting the garbage data within the function itself and we don't have to wait till the end of the function...
Qt Code:
void fn(){ int var1; { int var2; } // var2 is no longer valid here }To copy to clipboard, switch view to plain text mode
Garbage collection means that there is a mechanism which is able to track if a piece of memory is used or not and clean it up if possible without the programmer having to do it by himself. So the parent-child relationship in Qt can be treated as a very simple gc.I was kind of relating pointers to garbage collection in the sense that if a pointer is no longer used on the later half the function then we could free the memory of that pointer within the function i.e. an act of garbage collection. However if we have same amount of huge data say an array of 16 mb then we have to wait till the end of the function hence we cannot garbage collect...
The stack size is limited on many architectures (especially embedded ones), so you can't freely use up stack space - so actions like allocating large amount of variables or having a large number of recursions is not possible there.However I am a bit confused about dynamic allocation of stacks. Do you mean that if I have big variable( say array) having around 32mb then I cannot create other variables and have to create pointers and allocate the memory ?
ct (8th May 2007)
Why are you so afraid of pointers?
Pointers are cool !
Regarding stack size and stack overflow - take the case of recursive functions: are you willing to compute how many stack frames will you have and derive the number of vars and pointers that you must use, depending on the stack size? I doubt you can/will do that.
Therefore use pointers to avoid such issues and whenever you are in doubt.
For most it is common sense to know when to use pointers or allocate on the stack.
Regards
aah..I got it now...dumb me..
and this is a neat operation, I had nearly forgotten about the block scope operator..thanx a lot wysota...1. void fn(){
2. int var1;
3. {
4. int var2;
5. }
6. // var2 is no longer valid here
7. }
I will definitely use pointers when I feel necessary..ther very point I was asking this question was because I get all these notions from my classmates who are afraid of the site of pointers....
Humans make mistake because there is really NO patch for HUMAN STUPIDITY
Sure, but beware of the fact that this doesn't mean the variable is deallocated when it goes out of the inner scope. I'd assume it to be very compiler dependent, because the memory may be freed only when returning from the function, as this is done automatically by modifying the stack pointer in the CPU to reference the previous stack frame so no additional moves of the pointer are needed. So probably the memory is really freed when going out of inner scope on platforms that have a limited stack space. Otherwise it's just waste of CPU power, so the whole stack frame is probably freed in one go.
Bookmarks