Results 1 to 12 of 12

Thread: base pointer question

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default base pointer question

    hello,
    could someone explain me if I have to delete the first element of pvec and how many manner (syntax way) I have to do it?

    thanks:
    Qt Code:
    1. vector<int*>* pvec = new vector<int*>();
    2. int ten=10, twenty=20, thirty=30;
    3. int* pten = new int(10);
    4. int* ptwenty = &twenty;
    5. int* pthirty = &thirty;
    6.  
    7. pvec->push_back(pten);
    8. pvec->push_back(ptwenty);
    9. pvec->push_back(pthirty);
    10. vector<int*>::iterator it;
    11.  
    12. for(it=pvec->begin(); it != pvec->end(); ++it) {
    13. cout << "*it " << *(*it) << " ";
    14. }
    15. delete (*pvec)[0]; //necessary or not? how many way to do it?
    16. delete pvec;
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: base pointer question

    For each new there should be a delete somewhere, so yes, you have to delete it and there is probably countless number of ways you can do it.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: base pointer question

    Quote Originally Posted by jacek View Post
    For each new there should be a delete somewhere, so yes, you have to delete it and there is probably countless number of ways you can do it.
    sorry, could me enumerate the other ways to delete it please?
    Regards

  4. #4
    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: base pointer question

    Quote Originally Posted by mickey View Post
    sorry, could me enumerate the other ways to delete it please?
    Oh, oh, I have one - you can turn off your computer

    Seriously, Jacek probably meant that you can call delete from various contexts, but in the end somewhere you'll just have to use delete (or some other function/object that calls it for you).

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: base pointer question

    Quote Originally Posted by wysota View Post
    Oh, oh, I have one - you can turn off your computer

    Seriously, Jacek probably meant that you can call delete from various contexts, but in the end somewhere you'll just have to use delete (or some other function/object that calls it for you).
    Sincerly, I with "manners" I meant "syntax manner" like this:
    Qt Code:
    1. delete ((*pvec)).at(0);
    To copy to clipboard, switch view to plain text mode 
    So, are there other way?
    thanks.
    Regards

  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: base pointer question

    But other than what? You mean like this?

    Qt Code:
    1. delete (*pvec).at(0);
    2. delete pvec->at(0);
    3. delete (*pvec)[0];
    4. delete (**(&pvec))[0];
    5. delete (*(&pvec))->at(0);
    6. // etc..
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: base pointer question

    Quote Originally Posted by wysota View Post
    But other than what? You mean like this?

    Qt Code:
    1. delete (*pvec).at(0);
    2. delete pvec->at(0);
    3. delete (*pvec)[0];
    4. delete (**(&pvec))[0];
    5. delete (*(&pvec))->at(0);
    6. // etc..
    To copy to clipboard, switch view to plain text mode 
    yes I meant this but I hoped to find something different from "at" and "[]"
    Regards

  8. #8
    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: base pointer question

    Well... those are the methods of your vector class. With Qt you could do:
    Qt Code:
    1. qDeleteAll(*pvec);
    To copy to clipboard, switch view to plain text mode 

    BTW. What is the rationale of storing pointers to ints in a vector?

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: base pointer question

    Quote Originally Posted by wysota View Post
    Well... those are the methods of your vector class. With Qt you could do:
    Qt Code:
    1. qDeleteAll(*pvec);
    To copy to clipboard, switch view to plain text mode 

    BTW. What is the rationale of storing pointers to ints in a vector?
    It's just one prove.
    I guess you allude to the fact that it's the same write int ten=10; vec.push_back(ten); or better vec.push_back(10) and I haven't any advantage to have pointer to primitive types (and in this way I write less and clear code).???
    However: could you explain me what happen on stack/heap? I mean: pvec point to a block on the heap with "new"; pten with "new" point to a block (that contains an int) on the heap too; ptewnty and pthirty point on the stack; so on the heap I have pvec that contain 3 point to int: one of these, points to a block on the heap (10); the others two, point to a block on the stack..Is this?
    Again:
    if I have:
    Qt Code:
    1. vector<int*>* pvec = new vector<int*> ();
    2. {
    3. int ten = 10;
    4. int* pten = &ten;
    5. pvec.push_back(pten);
    6. }
    7. //here the pointers of pvec are still OK
    To copy to clipboard, switch view to plain text mode 
    Out the scope pten was undefined but inside pvec there is still a valid pointer becasuse the push_back make a copy of pointer?Is this?

    Thanks
    Regards

  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: base pointer question

    Quote Originally Posted by mickey View Post
    I guess you allude to the fact that it's the same write int ten=10; vec.push_back(ten); or better vec.push_back(10) and I haven't any advantage to have pointer to primitive types (and in this way I write less and clear code).???
    Yes, something like that. Plus storing pointers can be Dangerous(TM).

    However: could you explain me what happen on stack/heap?
    Nothing happens on the stack. Vectors allocate their data on heap regardless of where the container itself was created. When you add something to the vector, the object gets copied from its original place to the space allocated for vector's data (so on heap). If you store pointers, then only the pointer gets copied and not the data it points to.

    //here the pointers of pvec are still OK
    No, they are not. Once the flow leaves this scope "ten" gets destroyed and pten points somewhere to the stack and will be quickly overwritten by some other data. A simple and efficient way to corrupt the stack...

    Out the scope pten was undefined but inside pvec there is still a valid pointer becasuse the push_back make a copy of pointer?Is this?
    The pointer is pointing to random data. If you change the value it points to, you are asking for a segmentation fault.

    Take a debugger and see what happens on the stack and on the heap.

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: base pointer question

    Sorry, debugger show the content of pvec right all the time; this is the complete code:
    Qt Code:
    1. vector<int*>* pvec = new vector<int*>();
    2. {
    3. int ten=10, twenty=20, thirty=30;
    4. int *pten = &ten;
    5. //int* pten = new int(ten);
    6. int* ptwenty = &twenty;
    7. int* pthirty = &thirty;
    8. pvec->push_back(pten);
    9. pvec->push_back(ptwenty);
    10. pvec->push_back(pthirty);
    11. }
    12.  
    13. *(*pvec)[0] = 99; //no problems
    14. int q = 1000;
    15. (*pvec)[0] = &q;
    16. vector<int*>::iterator it;
    17. for(it=pvec->begin(); it != pvec->end(); ++it) {
    18. cout << "*it " << *(*it) << " ";
    19. }
    20. //delete (*pvec)[0];
    21. delete pvec;
    To copy to clipboard, switch view to plain text mode 
    Obviously ten will be destroyed out the scope but out of it (*pvec)[0] still contains the number 10 (and debugger show that this pointer doens't change....)
    I'm sorry but could you explain me more? thanks
    Regards

  12. #12
    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: base pointer question

    Try this:
    Qt Code:
    1. #include <vector>
    2. #include <iostream>
    3.  
    4. void go(std::vector<int*>* pvec){
    5. int ten=10, twenty=20, thirty=30;
    6. int *pten = &ten;
    7. int* ptwenty = &twenty;
    8. int* pthirty = &thirty;
    9. pvec->push_back(pten);
    10. pvec->push_back(ptwenty);
    11. pvec->push_back(pthirty);
    12. std::cout << *(pvec->at(0)) << std::endl;
    13. std::cout << *(pvec->at(1)) << std::endl;
    14. std::cout << *(pvec->at(2)) << std::endl;
    15. }
    16.  
    17. int main(){
    18. std::vector<int*>* pvec = new std::vector<int*>();
    19. go(pvec);
    20. std::cout << *(pvec->at(0)) << std::endl;
    21. std::cout << *(pvec->at(1)) << std::endl;
    22. std::cout << *(pvec->at(2)) << std::endl;
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 

    Try it, if it displays the same set of numbers two times then you're damn lucky
    Last edited by wysota; 11th March 2008 at 15:48.

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

    mickey (12th March 2008)

Similar Threads

  1. pointer base problem
    By mickey in forum General Programming
    Replies: 2
    Last Post: 7th November 2007, 21:36
  2. Connecting to a base class signal?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 22:37
  3. simple pointer question
    By mickey in forum General Programming
    Replies: 6
    Last Post: 16th June 2006, 09:19

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.