CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Deleting vectors

    I'm having some troubles when I try to delete some pointers.

    I have a structure like this:
    Code:
    struct vPos{
    	int *vector;
    	int **pos;
    };
    
    struct Triangle{
    	int nrows;
    	vPos *positions;
    };
    I know there is a simpler way to do it, using a double-pointer for example (and clasess, ok). But it doesn't matter.

    It works like this:
    positions ------> pos....... vector [__][__][__][__][__][__][__][__][__][__][__]....
    .......................[__]--------------------^
    .......................[__]----------------------------------^
    .......................[__]------------------------------------------------------^
    .......................[__]---------------------------------------------------------------------------^

    The problem is that I don't know how to delete all vectors.
    If I do delete [] pos, I will delete pos[0], pos[1], pos[2], pos[3], etc but delete pos is like delete pos[0], and pos[0] points to vector[0], so I think vector will be deleted too. And that's de problem. How can I delete all vector, and pos (and then positions) ?

    Thanks!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Deleting vectors

    Quote Originally Posted by Tronfi View Post
    I'm having some troubles when I try to delete some pointers.
    Use STL containers, and you don't have these problems. Any reason why you're not using one?

    Also, I would highly suggest you not call your member variables "vector", as there already is a std::vector class in C++.
    I have a structure like this:
    Code:
    struct vPos{
    	int *vector;
    	int **pos;
    };
    
    struct Triangle{
    	int nrows;
    	vPos *positions;
    };
    I know there is a simpler way to do it, using a double-pointer for example (and clasess, ok). But it doesn't matter.

    It works like this:
    positions ------> pos....... vector [__][__][__][__][__][__][__][__][__][__][__]....
    .......................[__]--------------------^
    .......................[__]----------------------------------^
    .......................[__]------------------------------------------------------^
    .......................[__]---------------------------------------------------------------------------^

    The problem is that I don't know how to delete all vectors.
    If I do delete [] pos, I will delete pos[0], pos[1], pos[2], pos[3], etc but delete pos is like delete pos[0], and pos[0] points to vector[0], so I think vector will be deleted too. And that's de problem. How can I delete all vector, and pos (and then positions) ?

    Thanks!
    Show us exactly in code how you built this hierarchy that you're talking about -- then and only then can someone answer "how to delete" and which order to delete what.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: Deleting vectors

    Thanks for the response Paul.
    I have to compare the performance using STL and dont using it, so in this case i can't use any container.

    You're right about calling the variable "vector". I'll change it.

    Here is the code:

    Code:
    void Start(Triangle &t){
    	t.nrows=0;
    	t.positions=new vPos;
    }
    
    void FreeT(Triangle &t){
    	if(t.positions){
    		delete [] t.positions->vector;
    		delete [] t.positions->pos;
    		delete [] t.positions;
    		t.positions=0;
    	}
    }
    
    void AddRow(Triangle &t){
    	t.nrows++;
    	if(t.nrows==1){
    		t.posiciones->vector=new int;
    		t.posiciones->pos=&t.posiciones->vector;
    	}
    	else Resize(t, GetNElements(t));
    }
    
    void Resize(Triangle &t, int n){
    	vPos *aux=new vPos;	
    	aux->pos=new int*[t.nrows]; 
    	aux->vector=new int[n];
    	
    	for(int i=0; i<n; i++)   aux->vector[i]=t.positions->vector[i];	
    	for(int j=0; j<t.nrows; j++)   aux->pos[j]=&(aux->vector[GetptrPos(j)]); 
    	
    	delete [] t.positions->vector;
    	delete [] t.positions->pos;
    	delete t.positions;		
    	t.positions=aux;	
    }
    
    int GetptrPos(int n){
    	---------------------------------------------------------
    	|1|-|2, 3|-|4, 5, 6|-|7, 8, 9, 10| - |11, 12, 13, 14, 15|
    	---------------------------------------------------------
    	 ^   ^      ^         ^               ^ 
       ptr1 ptr2   ptr3      ptr4            ptr5              
    	 
    	return n*(n+1)/2;
    }
    Thank you again.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Deleting vectors

    Quote Originally Posted by Tronfi View Post
    Thanks for the response Paul.
    I have to compare the performance using STL and dont using it, so in this case i can't use any container.
    Prove this.

    You're not going to just say "I compared the performance", and not back up your claim that using STL is slower than what you're doing now. Overuse of the calling "new" is in itself slow. I dare to say, your class is slower than using an STL container.

    As a matter of fact, your Resize() is definitely slower than what a vector.resize() does -- plain and simple.

    How about a main() program to show how you're creating this class.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Mar 2009
    Posts
    51

    Re: Deleting vectors

    Quote Originally Posted by Paul McKenzie View Post
    Prove this.
    I think you misunderstood, proving it (or not) IS the point of this exercise, he is MEANT to compare the two, using this code.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Deleting vectors

    As to your problem:
    If I do delete [] pos, I will delete pos[0], pos[1], pos[2], pos[3], etc
    Your terminology is confusing in terms of what you want to do.

    When you call delete [] or delete, you are deleting one thing. For delete[], you're removing an array that was dynamically allocated with new[]. Even though it is an array, it is the only thing that is deleted.

    So if you have this:
    Code:
    int **p;
    p = new int*[10];
    delete [] p;
    You are removing the array that you allocated with new[]. If the array is an array of pointers, the array items themselves (whatever those pointers in the array are pointing to) are not removed or deleted. All you are doing is removing the array that those pointers happen to be residing in.

    The only time when you are deleting p[0], p[1], p[2], etc. is if p[0], p[1],
    p[2] etc. are pointing to dynamically allocated memory, and each one was created using new (or new[]):
    Code:
    int **p;
    p = new int*[10];
    for (int i = 0; i < 10; ++i)
       p[i] = new int;
    //...
    for (int i = 0; i < 10; ++i )
       delete p[i];
    
    delete [] p;
    In the example above, you are actually deleting p[0], p[1], etc... Nowhere in your code did you do this, as nowhere did you dynamically allocate the data similar to the above.

    In the above example, if you failed to remove the dynamically allocated data in the loop, you would have a memory leak.

    So exactly, what are you trying to accomplish? Forget about new and delete, what are you really trying to do with your code?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 27th, 2009 at 10:08 AM.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Deleting vectors

    Quote Originally Posted by Zaccheus@Work View Post
    I think you misunderstood, proving it (or not) IS the point of this exercise, he is MEANT to compare the two, using this code.
    OK.

    Right away, I know that vector is faster in terms of resize. The reason being is that the OP's version of resize() is not smart. It doesn't check if the new size is the same as the old size, and it doesn't reserve memory if the new size is less than the original size. That is enough to show that the code is slower.

    Also, the whole exercise of the OP trying to write code that competes with code written by C++ professionals (some of the best in the business), means that there is little chance the OP can beat what vector does. It's like putting up a beginner in boxing against a world heavyweight champion. Unless it is a given that the OP cannot win, and instead this is an exercise given to show how hard it is to write a proper class, then I don't see a point in the exercise.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 27th, 2009 at 09:56 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured