-4

I allocated the memory but when I call the destructor, it gives me a segmentation fault. this is the code. am I using the right thing to free the memory?

class plan {
    char *symbol;
    gro  *grow;
  public:
    plan (int, char[] ); //constructor
    ~plan ( ); //destructor
};

plan::plan (int num_of_sm, char sm[]){
  try {
    symbol = new char [strlen(sm) + 1];
  }
  catch (std::bad_alloc) {
    symbol = NULL;
  }


  if (symbol != NULL) {
    if (sm == NULL) {
      strcpy (symbol, "");
    }
    else {
      strcpy (symbol, sm);
    }
  }
  gro = new grow [num_of_sm]; 
}

plan::~plan( ){
  delete [ ] symbol;
  delete [ ] gro;
}
Raedwald
  • 46,613
  • 43
  • 151
  • 237
user1318393
  • 398
  • 1
  • 4
  • 8
  • 4
    It's difficult to say, because you haven't shown the complete definition of your class. Please read http://sscce.org. The most likely explanation is that `symbol` is not a valid pointer. – Oliver Charlesworth Apr 06 '12 at 23:24
  • What does your constructor look like..? – Brendan Long Apr 06 '12 at 23:24
  • How and where did you allocate the memory for symbol and grow? – Haatschii Apr 06 '12 at 23:26
  • @user1318393: Edit your post and put it in there. – Nicol Bolas Apr 06 '12 at 23:28
  • 2
    Did you follow [the rule of 5](http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11)? – Mooing Duck Apr 06 '12 at 23:45
  • Show the *actual full constructor*, not just a piece of it. Prefereably you should give us enough code that we can compile it. – Brendan Long Apr 06 '12 at 23:52
  • I am sorry if my question is not clear. This is my first time posting a question. should I delete and post my question again and make it clear? – user1318393 Apr 06 '12 at 23:53
  • @user1318393: No, just edit it to include more information (such as a [short, self-contained, correct, compilable code snippet](http://sscce.org/)) – In silico Apr 06 '12 at 23:57
  • 2
    This class manages two naked resources, something that's commonly advised against. (If `new grow` throws, you leak `symbol` in the ctor.) That means you need a holder class for `symbol` and one for `grow`. It seems `std::string` will do nicely for the former, and `std::vector` for the latter. Stop writing C-With-Classes code. It will do a lot of harm. – sbi Apr 07 '12 at 01:43
  • Does not look like homework to me. – Raedwald Sep 28 '12 at 17:52

3 Answers3

5

Use std::string and be done with it.

Just in case it is not obvious, that will take care of the problem.

But also, to avoid some similar problems and to just understand a bit more of the issues involved, do look up the rule of 3, or as it's now known with C++11, the rule of 5.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

Memory problems can be very tricky because the program doesn't always crash right away. If you delete the same pointer twice, for example, the program may continue just fine until some later delete where it crashes. If you're running on Linux I suggest running valgrind (assuming it's installed just type valgrind followed by the normal command. eg valgrind myprog arg1 arg2)

In any case, don't assume the problem has to be where the program crashes. Try inspecting any other delete/free statements for problems. Also are you sure num_of_sm is a reasonable value?

John Gordon
  • 2,576
  • 3
  • 24
  • 29
0

when I call the destructor, it gives me a segmentation fault.

Did you mean the test code (bellow)

int main()
{
    plan a_plan(2, "hello");
    a_plan.~plan();
}

If so, I think the problem is you delete the pointer twice. The first time in plan::~plan(), and the second when leaving main(). So, don't call plan::~plan().

And you should follow Rule of Three (or rule of five), that's to say: if you defined the destructor , you should define the copy constructor and the assign operator at the same time.

Jid
  • 1
  • 1