-1

if I have the following -

#include <iostream>

class object{
    private:
    public:
       object();
       object(some_item);
};


object a;

int main()
{
   // why doesn't it let me do
   a = new object(some_item);
   
   // but lets me do
   a = *new object(some_item);
   
}

I am trying to initialize a global object which will be initialized at runtime inside the main loop but not in the part where I defined it globally.

basically I want an object that is available globally and that can be initialized at runtime inside any function.

But I don't completely understand how the new/*new operator works.

  • 10
    `a = *new object(some_item);` is just a memory leak. operator `new` is used for dynamic allocations (and therefore returns a pointer), which is not what you want here at all – UnholySheep Jan 27 '23 at 19:38
  • You don't even need the new. That object is already created an initialized. – Martin York Jan 27 '23 at 19:38
  • You want a disaster waiting to happen. Globals should typically be `constexpr`, or read-only state. The code as shown also doesn't need `new` at all. – sweenish Jan 27 '23 at 19:38
  • `new` returns a pointer to a dynamic allocation. `object a;` provides a `object`, not a pointer to an `object`. – user4581301 Jan 27 '23 at 19:39
  • 1
    Strong recommendation: This will be beaten to death fairly early in any [non-fraudulent textbook](https://stackoverflow.com/q/388242/4581301), so consider getting and reading one. – user4581301 Jan 27 '23 at 19:41
  • If you want to do it the hard way, though, [here's a great documentation page on `new`](https://en.cppreference.com/w/cpp/language/new). – user4581301 Jan 27 '23 at 19:43
  • 10
    @UnholySheep I first read that as "memory leak operator" and I refuse to put it out of my head. – sweenish Jan 27 '23 at 19:46
  • 1
    There is no `*new` operator, what you have done is `*(new object(some_item))`, which is a memory leak (unless you have some hidden mechanism for saving a pointer to the object and freeing it later). Hint: remove the `*` and remove the `new`. – David Grayson Jan 27 '23 at 19:47
  • 1
    What happens with `a = *new object(some_item);` space for a `object` is allocated from the freestore and a `object` is constructed in that memory. The pointer to the `object` is then dereferenced to get the pointed-at `object` and this `object` copied into the already existing `object` `a`. The pointer is never stored, so the dynamically allocated `object` is lost, also known as "leaked" and exceptionally hard to locate and later destroy and return to the freestore with `delete`. – user4581301 Jan 27 '23 at 19:49
  • *and that can be initialized at runtime inside any function.* -- Too late. It has already been initialized when you declared `object a;` – PaulMcKenzie Jan 27 '23 at 19:50
  • *But I don't completely understand how the `new` operator works.* Using `new` is an advanced technique, which you shouldn't worry about until you've been programming in C++ for over a year. I've been programming in C++ for 34 years, and I haven't used `new` in real code in the last 10 years, since there are much better alternatives than using the (as I'm now forever going to call it) **memory leak operator**. – Eljay Jan 27 '23 at 19:50
  • 1
    Thanks everyone, I've got it. https://stackoverflow.com/questions/8839943/why-does-the-use-of-new-cause-memory-leaks – Mohammed Zahed Jan 27 '23 at 19:53
  • 1
    I wish we had a `novice` or `beginner` tag. This is a **good** novice or beginner question, that other novices/beginners could find useful. – Eljay Jan 27 '23 at 22:33
  • You should avoid writing `new` whenever possible. If you must heap-allocate, use `std::make_unique()` then you’d have `a = *std::make_unique(some_item);` which at least won’t leak. Although better is `a = object(some_item);`. – Ben Jan 28 '23 at 00:26

1 Answers1

4

operator new returns a pointer to a dynamically created object.

So, the type of this expression:

new object(some_item)

is the pointer type object *.

On the other hand, the variable a is declared as having the type object:

object a;

So, you are trying to assign a pointer of the type object * to an object of the type object:

a = new object(some_item);

and within the class object, there is no overloaded assignment operator= declared like this:

object & operator =( object * );

So, the compiler issues an error.

However, when the pointer expression is dereferenced, then you get an object reference of the type object& , and can assign it to the variable a:

a = *new object(some_item);

using the default generated copy assignment operator=.

Pay attention to that, in this case, you will have a memory leak, because the address of the dynamically allocated object will be lost.

If you want to have a dynamically allocated object, or an array of objects, then the variable a should have the pointer type object *:

object *a;

In this case, you may write in main():

a = new object(some_item);

And then you have to free it when you are done using it:

delete a;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Object created with ```new``` needs to be manually deleted after using. ```Object *o = new Object( arguments); // do something with o; delete o;``` – sxu Jan 27 '23 at 22:38