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.
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.
Sincerly, I with "manners" I meant "syntax manner" like this:
So, are there other way?
thanks.
Regards
But other than what? You mean like this?
Qt Code:
delete (*pvec).at(0); delete pvec->at(0); delete (*pvec)[0]; delete (**(&pvec))[0]; delete (*(&pvec))->at(0); // etc..To copy to clipboard, switch view to plain text mode
Well... those are the methods of your vector class. With Qt you could do:
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:
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?Qt Code:
vector<int*>* pvec = new vector<int*> (); { int ten = 10; int* pten = &ten; pvec.push_back(pten); } //here the pointers of pvec are still OKTo copy to clipboard, switch view to plain text mode
Thanks
Regards
Yes, something like that. Plus storing pointers can be Dangerous(TM).
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.However: could you explain me what happen on stack/heap?
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...//here the pointers of pvec are still OK
The pointer is pointing to random data. If you change the value it points to, you are asking for a segmentation fault.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?
Take a debugger and see what happens on the stack and on the heap.
Sorry, debugger show the content of pvec right all the time; this is the complete code:
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....)Qt Code:
vector<int*>* pvec = new vector<int*>(); { int ten=10, twenty=20, thirty=30; int *pten = &ten; //int* pten = new int(ten); int* ptwenty = &twenty; int* pthirty = &thirty; pvec->push_back(pten); pvec->push_back(ptwenty); pvec->push_back(pthirty); } *(*pvec)[0] = 99; //no problems int q = 1000; (*pvec)[0] = &q; vector<int*>::iterator it; for(it=pvec->begin(); it != pvec->end(); ++it) { cout << "*it " << *(*it) << " "; } //delete (*pvec)[0]; delete pvec;To copy to clipboard, switch view to plain text mode
I'm sorry but could you explain me more? thanks
Regards
Try this:
Qt Code:
#include <vector> #include <iostream> void go(std::vector<int*>* pvec){ int ten=10, twenty=20, thirty=30; int *pten = &ten; int* ptwenty = &twenty; int* pthirty = &thirty; pvec->push_back(pten); pvec->push_back(ptwenty); pvec->push_back(pthirty); std::cout << *(pvec->at(0)) << std::endl; std::cout << *(pvec->at(1)) << std::endl; std::cout << *(pvec->at(2)) << std::endl; } int main(){ std::vector<int*>* pvec = new std::vector<int*>(); go(pvec); std::cout << *(pvec->at(0)) << std::endl; std::cout << *(pvec->at(1)) << std::endl; std::cout << *(pvec->at(2)) << std::endl; return 0; }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 16:48.
mickey (12th March 2008)
Bookmarks