0

a skeleton of my code is as follows:

vector<char**> myFunc(some param) {
     char**first = new char*[some size];
     char*second = new char[some other size];          
 }//sizes depend on param

while(...) {
     vector<char**> myVec;
     myVec = myFunc(param);
     /* some stuff happens
      *
      */

 }//end while

As can clearly be deducted, I need to delete both first and second before the end of my while loop. I have tried to both delete[] myVec[i] (in a loop) but to no avail, as well as other permutations of this. Any ideas?

thanks! (I used new instead of malloc for 'simplicity' sake; as my understanding both are the same (besides delete only with new etc.)

  • What functionality do you exactly need? Do you need a dynamic array of strings? Then you are much better off using: `std::vector`. Where are you storing `first` and `second`, Clarify your question to reflect your intents clearly. Also, there is a huge difference between `new` and `malloc` do not assume they are same. – Alok Save Feb 19 '13 at 03:51
  • I am 'new'ing both first and second because I am pushing them onto my vector that is being returned from myFunc, and they would be out of scope once myFunc returns and I need them to stay in tact throughout the while loop. – Peter Shempx Feb 19 '13 at 03:59
  • How can you push `second` which is of type `char *` in a vector of type `vector`? – Alok Save Feb 19 '13 at 04:01
  • [Does vector::erase() on a vector of object pointers destroy the object itself?](http://stackoverflow.com/questions/6353149/does-vectorerase-on-a-vector-of-object-pointers-destroy-the-object-itself) might help. – Alok Save Feb 19 '13 at 04:06
  • I looked at that very thread, and it just confirmed that erase() is not enough. I am only pushing first onto the vector but first is created with instances of second – Peter Shempx Feb 19 '13 at 04:11
  • Well the answers there tell you what you should be doing but anyways the fundamental Q is what exactly you need? Why are you using `char **`? You neither express your intentions behind using this easy to go wrong error prone way of doing things neither do you show the code of how exactly you are doing it, So effectively without knowing those two things answering your question is impossible. – Alok Save Feb 19 '13 at 04:14

1 Answers1

0

Solution: use std::vector<std::vector<std::string>>>

Community
  • 1
  • 1
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78