Understanding memory allocation

I'm new to c++ but I do have a programming background.
I understand that there are three ways of allocating memory: on the stack statically, on the stack dynamically and on the heap dynamically. I just don't understand which one will take place with the following statements and how to delete memory used by them:

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
class MyClass1
{
      int intArray[10];
      ...
}

class MyClass2
{
      int* intArray;
      ...
}

class MyClass3
{
      OtherClass anotherObject;
      ...
}

class MyClass4
{
      OtherClass* anotherObject;
      ...
      public:
          void TestMethod();
}

void MyClass4::TestMethod()
{
     OtherClass* localObject = new OtherClass;
}

Thanks in advance.
Cheers.
First off I will be assuming that you are asking about what will happen when the default constructor is ran. Also for MyClass4 this will also include TestMethod(). DC= default constructor

In MyClass1 in the DC 10 ints are made right by each other on the stack.
In MyClass2 in the DC one int pointer is made on the stack.
(The pointer is made on the stack but the variable that it could point to doesn't have to be on the stack.
If you did
1
2
3
4
5
6
7
8
class MyClass2
{
      int* intArray;
      MyClass2()
      {
            intArray=new int[5];
      }
}

then the 5 ints that intArray point to are on the heap
In MyClass3 in the DC one OtherClass is put on the stack.
In MyClass4 in the DC one OtherClass pointer is made on the stack. (Same as MyClass2 but with OtherClass instead of int)
In MyClass4 in Test Method one OtherClass pointer is made on the stack. Then is initialized to point to an OtherClass object in the heap.
Last edited on
Thanks!, that was really helpful. I got it. What about deleting the memory used in each case?
Thanks again.
In MyClass1 in the DC 10 ints are made right by each other on the stack.
In MyClass2 in the DC one int pointer is made on the stack.
[...]
In MyClass3 in the DC one OtherClass is put on the stack.
In MyClass4 in the DC one OtherClass pointer is made on the stack.
None of this is necessarily true.
A class can't know, nor does it need to know, whether its members reside on the stack or the heap. It depends on how the object was created. If the object is on the stack, its members are on the stack, if it's on the heap, they're on the heap.

As for how to free memory, a class doesn't need to free its own members. That's handled automatically by someone else (who exactly depends, again, on how the object was created). All that needs to be freed are dynamic objects, such as the one created on line 29.
So, is there a way to know exactly where will the object reside?. Is it right to ensure that if I have this main:

int main()
{
MyClass1* applicationMainController = new MyClass1;
applicationMainController->RunUntilUserQuit();
return 0;
}

the only record that will be created on the stack (not local) is a pointer to MyClass1, since all other objects are created within applicationMainController which is on the heap. Is that correct?.

Thanks you both for your time.
Yes. If an object is created on the stack, then some of its data may reside on the heap, but if its created on the heap, then none of its data can reside on the stack. (Note that I said "data", not "members". Data includes other objects that the class allocates.)
However, local objects in member functions will reside on the stack, just like it happens with any other function, regardless of how the object was created.
Last edited on
I think I got it now.
Thanks a lot.
Topic archived. No new replies allowed.