memory advice for programmer used to garbage collection

Hello, my prior programming experience has been limited to Java and C#. I am still trying to learn the intricacies of C++'s memory management. Here is what I am trying to do:
I have a struct (we'll call it A) that contains an array of structs (B), and each of those structs contains an array of structs (C). I have a function that creates all of these things, here is the function declaration:
void CreateA(A * returnA, char * filename);
So that's great and all but all of the B and C structs that I create in the CreateA function are local variables and appear to be lost once the function exits. In my previous experience it seems that the garage collectors would never touch those since A still had references to the B's which still had their references to the C's. But here they are all lost and when it goes back to main everything in A is nothing like what it was in the CreateA function. The debugger shows things like "bad pointer" or just really incorrect values for ints. Am I just going about it the wrong way now that I am working in C++? Any help is greatly appreciated.
closed account (S6k9GNh0)
When you create a local struct, the reference to that struct is only there until the function scope is gone or until the function ends. Your createA function ends, badabing, everything is gone and the outA pointer should actually point to NULL. As a result, you either want to make your struct static and then pass it or push it to the stack.

So we could make the function like:
1
2
3
4
void CreateA(A * outA)
{
   outA = new A; //Creates enough memory for the struct and it's members and gives you an empty struct
}


or we could make it static which I don't recommend:
1
2
3
4
5
void CreateA(A * outA)
{
    static A out;
    outA = &out; //I think this is how you do it. I don't mess with static variables often.
}


But to be quite honest, all of this isn't relevant or done often because this is done inside of where you want outA.

1
2
3
4
5
int main()
{
   A whore = new A;
   //whore says, "Do what you will!"
}
Last edited on
I think you meant
1
2
3
4
5
6
7
8
9
void CreateA(A **a){
    *a=new A;
}

void CreateA(A **a){
    static A b;
    *a=new A(b); //This is one of the most pointless things ever. But then, so
                 //is the previous function.
}


EDIT: Oh, I understand. OP doesn't seem to know constructors exist:
1
2
3
4
5
class A{
    A(char *str){
        //...
    }
};
Last edited on
I recommend picking up a copy of Effective STL by Scott Meyers. One good thing about C++ that you will be happy with is that it provides many forms of containers that manage your memory so that you can minimize manual garbage collection. You simply have to study those containers and learn their interfaces.

At this link you will find the documentation for a dynamic array template class. The reference section of this website contains links for all std containers and algorithms which you should begin reading ASAP.
http://cplusplus.com/reference/stl/vector/

There is also a container called an auto_ptr which automatically manages memory for you so that you can use operator new without worrying about collecting the garbage.
http://cplusplus.com/reference/std/memory/auto_ptr/

If you have a class that has ptr attributes you could make them auto_ptr types so that you can allocate the memory and not worry so much about their destruction since their scope will then be tied to the scope of the encapsulating class.
Topic archived. No new replies allowed.