0

I have the following code snippet:

A.h

include B.h

class A
{
public:
    void createB();
    B* getB();
private:
    B *b;
};

A.cpp

void A::createB()
{
    b = new B();
}

B* A::getB()
{
    return b;
}

Main.cpp

int main()
{
    int n = 3;

    A *a_array = new A[n];

    for ( int i = 0; i < n; i++ ) {

        A *a = new A();

        a_array[i] = *a;
    }

    for ( int i = 0; i < n; i++ ) {

        A a = a_array[i];

        a.createB();
    }

    a_array[0].doStuff();   // OK

    a_array[0].getB();  // returns NULL
}

Output:

    A created
    A created
    A created
    B created
    A destroyed
    B created
    A destroyed
    B created
    A destroyed

It seems A objects are destroyed in the loop, but i have access them without loop, but can't access to A's object members.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Kun
  • 1
  • 2
  • 1
    Way too many pointers. To overwrite an object in the array, just use `a_array[i] = A();`. No need for `new` here. C++ isn't Java. – Pete Becker Jan 31 '13 at 11:22

3 Answers3

2

The problem is that in the second loop you call createB on a copy of the corresponding element in the array a_array. Try changing the loop to:

for ( int i = 0; i < n; i++ ) {

    A& a = a_array[i];

    a.createB();
}

Also please note you have a lot of memory leaks in your program - you allocate a lot of stuff that you never free.

In the first cycle there is no need to allocate a dynamically before assigning it to an element in the array. Simply do(as pointed out by Pete Becker):

for ( int i = 0; i < n; i++ ) {
    a_array[i] = A();
}
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0
int main()
{

int n = 3;

A *a_array = new A[n];

for ( int i = 0; i < n; i++ ) {

    A *a = new A();

    a_array[i] = *a;   // here you are copying a and then causing a memleak
}

what you probably meant was

A **a_array = new A*[n];

for ( int i = 0; i < n; i++ ) {
    A *a = new A();
    a_array[i] = a;   // now array points to instance of a
}

now when you reference the pointer:

for ( int i = 0; i < n; i++ ) {
  A* a = a_array[i];
  a->createB();
}

a_array[0]->doStuff();   // OK
a_array[0]->getB();  // returns NULL
...

don't forget to delete array and instances of a.

You should however take a look at vector<> and array<>, using STL containers is better

AndersK
  • 35,813
  • 6
  • 60
  • 86
  • Thank for the answer. By the way, do you know a good c++ tutorial explaining relationships between classes and pointer utilization. I think these are the most confusing subjects for c++. But even most popular c++ book do not have a deeper look into these issues. – Kun Jan 31 '13 at 11:37
  • been a while since i read a tutorial in C++ but I suspect getting pointers explained would be equally if not better explained in a book about the C language. In C++ one tends to try - at least to a certain degree - avoid pointers and use instead things like STL. – AndersK Jan 31 '13 at 11:41
0

A a = a_array[i] in the second for loop creates a copy on the stack which is deleted again after the loop scope. The a_array[0] access after the loop refers to another object then.

anhoppe
  • 4,287
  • 3
  • 46
  • 58