0

Let's assume that I have the following class:

Class Foo {
public:
    int j;
    Foo(int i){
        j = i;
        std::cout << j << endl;
    };
}

I am new to C++ and I'm confused about whether the following two blocks of code perform the same activity regarding memory allocation. I remember learning that new allocates memory dynamically but I am not sure about the first block. Is the first block doing the same?

Foo foo{2};
Foo *foo2;
foo2 = new Foo(2);
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • One creates an *automatic* object `foo`, the other defines a *pointer* `foo2`, then allocates a dynamic object and stores the address of that object as the pointer's value. If you're new to this language I *strongly* urge a good book. – WhozCraig May 28 '22 at 04:35
  • Foo doesn't have any values in it so you aren't initializing anything. The constructor only prints the `i` passed. the `foo2=new Foo(2);` does allocate an object but only because c++ doesn't allow a completely empty class object. – doug May 28 '22 at 04:36
  • @WhozCraig Can you recommend any? – Isaac Martinez Jr. Mora May 28 '22 at 04:41
  • @doug I update my question to have the constructor initialize the class member j. – Isaac Martinez Jr. Mora May 28 '22 at 04:43
  • probably should be `j=i;` – doug May 28 '22 at 04:53
  • @IsaacMartinezJr.Mora You can refer to a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason May 28 '22 at 04:58
  • Please don't tag c in c++ questions – Alan Birtles May 28 '22 at 05:55
  • One should write `Foo(int i) : j(i) { }` and also `Foo *foo2 = new Foo(2);`. However, in modern code, one should prefer using `std::unique_ptr foo(2)` instead. **Read a few books on C++** to learn the language and good pratices. – Phil1970 May 28 '22 at 12:23

1 Answers1

0

Case 1

Here we consider:

Foo foo{2}; //this will create an object of type `Foo` on the stack

The above statement will create an object of type Foo on the stack using the converting constructor Foo::Foo(int).

Case 2

Here we consider:

Foo *foo2;           //creates a pointer of type `Foo*` on the stack
foo2 = new Foo(2);   //assign foo2 the pointer returned from new Foo(2)

Here first we create a pointer named foo2 of type Foo* on the stack. Note that as of now this pointer is uninitialized.

Next, when we write foo2 = new Foo(2); the following things happen:

  1. Due to new Foo(2) an object of type Foo is created on the heap using the converting constructor Foo::Foo(int).

  2. Next, a pointer to that dynamically allocated object is returned.

  3. This returned pointer is assigned to foo2 on the left hand side.


Note also that instead of assigning i to j inside the constructor you can instead initialize j in the constructor initializer list as shown below:

Class Foo {
public:
    int j;
//--------------vvvv--------------> initialize j in the member initializer list
    Foo(int i): j(i){
       
        std::cout << j << endl;
    };
}
Jason
  • 36,170
  • 5
  • 26
  • 60