Results 1 to 13 of 13

Thread: Use/Misuse of Pointers

  1. #1
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Use/Misuse of Pointers

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Use/Misuse of Pointers

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use/Misuse of Pointers

    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.

  4. #4
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use/Misuse of Pointers

    Quote Originally Posted by high_flyer View Post
    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.
    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.

    If it seems natural to use a pointer, do it.
    Could you please specify in detail at least some of the scenarios when it would seem natural/ unnatural to use a pointer.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Use/Misuse of Pointers

    Could you please specify in detail at least some of the scenarios when it would seem natural/ unnatural to use a pointer.
    Polymorphisem is implemented with pointers for example.
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use/Misuse of Pointers

    Quote Originally Posted by ct View Post
    Could you please specify in detail at least some of the scenarios when it would seem natural/ unnatural to use a pointer.
    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).

  7. #7
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use/Misuse of Pointers

    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:
    1. void myfunc()
    2. {
    3. struct mypointer* ptr;
    4.  
    5. //...
    6.  
    7. //...
    8.  
    9. //..a long long function
    10. }
    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

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Use/Misuse of Pointers

    I was kind of wondering, is pointers related to garbage collection ??
    Related in what way?
    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.

  9. #9
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use/Misuse of Pointers

    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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use/Misuse of Pointers

    Quote Originally Posted by ct View Post
    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 ?
    No, it might be a problem because on some architectures you'll be trying to use more memory than it's available for you.

    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...
    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:
    Qt Code:
    1. void fn(){
    2. int var1;
    3. {
    4. int var2;
    5. }
    6. // var2 is no longer valid here
    7. }
    To copy to clipboard, switch view to plain text mode 

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

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

  11. The following user says thank you to wysota for this useful post:

    ct (8th May 2007)

  12. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use/Misuse of Pointers

    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

  13. #12
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use/Misuse of Pointers

    aah..I got it now...dumb me..

    1. void fn(){
    2. int var1;
    3. {
    4. int var2;
    5. }
    6. // var2 is no longer valid here
    7. }
    and this is a neat operation, I had nearly forgotten about the block scope operator..thanx a lot wysota...

    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

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use/Misuse of Pointers

    Quote Originally Posted by ct View Post
    and this is a neat operation, I had nearly forgotten about the block scope operator..thanx a lot wysota...
    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.

Similar Threads

  1. Pointer to a 2D array of pointers ??
    By aamer4yu in forum General Programming
    Replies: 2
    Last Post: 1st February 2007, 11:16
  2. Is it possible to store pointers in a database ?
    By probine in forum General Programming
    Replies: 8
    Last Post: 5th April 2006, 21:28
  3. another Q_OBJECT and pointers (?)
    By caminoix in forum Qt Programming
    Replies: 8
    Last Post: 26th March 2006, 21:06
  4. Problem with pointers while using localtime() and time()
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 11th January 2006, 15:48
  5. K&R's strcpy function - problem with pointers
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 8th January 2006, 15:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.