I\'m interested if these two lines of code are the same:
shared_ptr sp(new int(1)); // double allocation?
shared_ptr sp(make_shared<
There is also a potential subtle bug: in sp(new int)
you fist allocate an int (whose pointer is given to sp
), than sp itself has to allocate a control block (will contain the counters and the deleter).
Now, if doing this last allocation sp
fails (low memory) you are left with a heap allocated int whose pointer is not held by anyone, and thus impossible to delete. (memory leak).