2

I am trying to play a little bit with memory in C++, I have defined myself a class and then I create an instance of that class inside of heap.

#include <iostream>

class mojeTrida {
  public:
  
  void TestPrint()
  {
    std::cout << "Ahoj 2\n";
  }
};

int main() {
  mojeTrida *testInstance = new mojeTrida();
  
  testInstance->TestPrint();
  
  std::cout << "Hello World!\n";
}

If I understand c++ correctly, anytime I am calling the keyword “new”, I am asking OS to give me certain amount of bytes to store a new instance of class inside of heap.

Is there any way I can store my class inside of stack?

ShoeMaker
  • 149
  • 1
  • 2
  • 9

1 Answers1

10

The way to create your object (i.e. class instance) on the stack is even simpler – local variables are stored on the stack.

int main() {
  mojeTrida testInstance;  // local variable is stored on the stack
  
  testInstance.TestPrint();
  
  std::cout << "Hello World!\n";
}

As you have noticed according to your comment, the operator . is used instead of -> when calling a method of the object. -> is only used with pointers to dereference them and access their members at the same time.

An example with a pointer to a local variable:

int main() {
  mojeTrida localInstance;  // object allocated on the stack
  mojeTrida *testInstance = &localInstance; // pointer to localInstance allocated on the stack
  
  testInstance->TestPrint();
  
  std::cout << "Hello World!\n";
  // localInstance & testInstance freed automatically when leaving the block
}

On the other hand, you should delete the object created on the heap using new:

int main() {
  mojeTrida *testInstance = new mojeTrida();  // the object allocated on the heap, pointer allocated on the stack
  
  testInstance->TestPrint();

  delete testInstance;  // the heap object can be freed here, not used anymore
  
  std::cout << "Hello World!\n";
}

See also: When should I use the new keyword in C++?

Melebius
  • 6,183
  • 4
  • 39
  • 52
  • Meaning I call method inside of object that is on stack with “.” and a method of object inside of heap with “->”? – ShoeMaker Jun 24 '20 at 13:28
  • 2
    no, in the stack example type of `testInstance` is `mojeTrida`, in heap example the type is `mojeTrida*` (pointer). That's what makes the `.` vs `->` difference. You can still obtain pointer to stack instance like `mojeTrida testInstance; mojeTrida* testInstancePtr = &testInstance; testInstancePtr->TestPrint();` ... just be aware the heap instance stays allocated until deleted, the stack instance memory gets released at the end of the `{}` block, so any pointer to it becomes meaningless (pointing to memory unused or used by other code block). – Ped7g Jun 24 '20 at 13:31
  • @ShoeMaker similarly you can create temporary reference to the heap instance like `mojeTrida & testRef = *testInstance; testRef.TestPrint();` ... the references are generally a bit "safer" in the way that they can't be `nullptr` (if the code is correct), while the pointer can be. – Ped7g Jun 24 '20 at 13:37
  • 1
    @Ped7g I wouldn’t complicate the topic with references right now! :-D – Melebius Jun 24 '20 at 13:38