Hi,whats wrong with my code?

Hi folks ,i dont understand why free my resources stuck.
This is my code:

Class Person
{
char* name;
public:
Person(const char* name):name(NULL)
{
This->name=strdup(name);
}
~Person(){delete[] name;}
};

Class School
{
Person* persons;
int numOfPersons;
public:
School(int numOfPersons):numOfPersons(numOfPersons)
{
persons=new Person[numOfPersons];
}
void setPerson(int index,Person& p)
{
persons[index]=p;
}
~School(){delete[] persons;}

};

void main()
{
Person p("John");
School s(2);
S.setPerson(0,p);
}

The problem is after the program has finnished it stuck in freeing memory.
I think it because it is trying to free p twice ,if this is the case what should i do?

Thank you very very much!!!!
I think it because it is trying to free p twice ,if this is the case what should i do?

First use code tags when posting code!

Second the problem is probably being caused because you're trying to delete[] something that wasn't allocated with new[]. But this is only a guess because there are errors in the code that will not allow the code to actually compile when using a modern standard compliant compiler.

It is not very good, but I tried to create something close to created by you. Try it, it works in C++ Builder 5.0:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Person
{
  char* name;
public:
  Person(const char* name)
  {
    this->name=strdup(name);
  }
  Person(){};
  ~Person(){delete[] name;}
};

class School
{
  Person **persons;
  int numOfPersons;
public:
  School(int numOfPersons)
  {
    persons=new Person*[numOfPersons];
    for(int jp=0;jp<numOfPersons;jp++)persons[jp]=new Person();
  }
  void setPerson(int index,Person *p)
  {
    delete persons[index];
    persons[index]=p;
  }
  ~School(){for(int jp=0;jp<numOfPersons;jp++)delete persons[jp];delete[] persons;}
};
void main()
{
  School s(2);
  Person *p=new Person("John");
  s.setPerson(0,p);
  Person *p2=new Person("Susan");
  s.setPerson(1,p2);
}
It is not very good,

Very true, but at least you used code tags when posting code.

You are still trying to delete[] items that were not allocated with new[]. The C function strdup() doesn't know about new or delete.

And by the way main() must be defined to return an int, and you should return an int from this function.

Try it, it works in C++ Builder 5.0:

Why are you using such an old IDE? Have you considered using a more modern, more standards compliant IDE/compiler?

I think it because it is trying to free p twice

That too.
The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3).

Person's destructor calls delete [] on something allocated with malloc.

That happens on p's destruction at the end of main().
That happens on s[0]'s destruction at the end of main() too,
because s[0].name == p.name due to copy assignment with setPerson.

The destructor of s[1] attempts to deallocate too. What does the s[1].name point to?


Your classes manage dynamic memory. Constructor and destructor are not enough.
See https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)


Please consider using std::string (for strings) and std::vector (for dynamic arrays) if you don't absolutely need to do manual dynamic memory management.
Last edited on
Why are you using such an old IDE?

It works perfectly. And Embarcadero sells C++Builder for high price...
closed account (E0p9LyTq)
skaa wrote:
It works perfectly. And Embarcadero sells C++Builder for high price...

There are free IDEs that are C+11/14/17 compliant, unlike C++Builder 5.0.

Microsoft makes several, Visual Studio 2015 Community for one. Code::Blocks is another.

The only "advantage" C++B 5.0 has is its Rapid Application Development scheme, based on Delphi. If that is so important then buy the suite.
Topic archived. No new replies allowed.