0

I just do not get something about about a dynamic array with a container class here is an example of how I do it.

A container class:

class Container{
private:
    int n, current;
    Class *C;
public:
    Container(): C(NULL), n(0), current(0){}
    void expandC(int ammount){
      Class *NewClass= new Class[ammount];
      for (int i = 0; i < n; i++)
        NewClass[i] = C[i];
        delete []C;
        C = NewClass;
        n = ammount;
    }

};

Why do I get an error on a delete[] C line?

EDIT: If caught the essence of the rule of three it means that you have to define a copy constructor an assignment operator or a destructor. In my case the most important is probably the copy constructor.
Here is a how I understood how they should be defined in my case:

Container(): C(NULL), n(0), current(0){}
Container(int N, vector<string> a){
    C = new Class[N];
    for(int i = 0; i<n; i++){C->setA(a[i]);}
    n=N;
}
~Container(){ delete [] C;}

It is a good practice and I am going to use it i future but I in this case it have not helped me.

I have noted in the comments that my error is related with access violation by I am posting it here just in case.

Unhandled exception at 0x53f0edfc (msvcr90d.dll) in dinamicTest.exe: 0xC0000005: Access violation writing location 0xabababab.

Here is the requested initial full version of this program http://pastebin.com/djTz36Tu

Povylas
  • 776
  • 4
  • 16
  • 31
  • Something about "Access violation writing location..." – Povylas Mar 13 '12 at 18:27
  • Your code sample is too long to read, and too incomplete to test. Please create a *short*, *complete* program that demonstrates the error you are seeing, and then post that shorter, complete program in your question. See http://sscce.org/. – Robᵩ Mar 13 '12 at 18:33
  • if you've determined that Als's advice is correct, then the point is moot, and there is no reason to improve the sample code. But, no, this version is not sufficient. In order to be certain what a program's problem is, we need to see an **complete** program. A complete program includes `int main()`, probably some `#include`s, etc. In your specific case, we can know for certain that the Rule of Three is the cause *only if* we see how your class is being used. Who calls `expandC`? What is the state of your objects at the crash? Please read http://sscce.org for more info. – Robᵩ Mar 13 '12 at 19:14
  • is it better now? It is not full program because it would be really long. But if you insist on complete but short version of this program here you go http://pastebin.com/djTz36Tu – Povylas Mar 13 '12 at 19:27
  • Note that the pastebin version of the program contains an unrelated bug: it calls `free` when it should have called `delete[]`. – Robᵩ Mar 13 '12 at 20:02
  • Sorry I was trying out different ways to fix this. I must have missed to recreate that but it still reproduces the same error so it not a big difference. – Povylas Mar 13 '12 at 20:16

2 Answers2

3

Most likely reason of You getting a error is that you are not following the Rule of Three.

Note that, You have a pointer member C in your class Container, this pointer member points to dynamically allocated memory. Now think about the scenario where your code needs to create copies of Container class, Since You do not provide an overloaded copy constructor, the compiler generated implicit copy constructor will be used. The pointer member will be shallow copied in to the newly created object. When this happens object member pointers of your multiple pointers point to the same dynamic memory when one of those gets deleted the other becomes a dangling pointer and cause the problem.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

The problem lies in addC. Consider these two lines:

      expandC(n+1);
      C[n].setA(name);

This appears to increase the size of the dynamically-allocated array, and then store a value in the final entry in the array. Certainly if the array is n+1 entries big, then the final entry is indexed by n.

But, n is not a local variable to addC. n is a member of the class, and is set as a side-effect of the expandC operation.

Consider these annotated lines:

    // n starts out as 10:
    expandC(n+1);  // calls expandC(11), of course
                   // but expandC sets n to 11!
    // n is now 11
    C[n].setA(name);  // C has 11 entries, but n is 11: BOOM!

You can fix this problem thusly:

    void addC(string name){
      expandC(n+1);
      C[n-1].setA(name);
    }
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • I fixed the program and added some more little tweaks and the final working example can be found here : http://pastebin.com/ZseDEPna – Povylas Mar 13 '12 at 20:29