2

What is the difference between these two statements?

stringstream *myStream = new stringstream(s);

And

stringstream myStream(s);  

I heard that first return a pointer and "dynamically" allocates memory. But I don't truly understand the difference.

Thank you in advance

Paul Brewczynski
  • 2,540
  • 3
  • 27
  • 39

4 Answers4

5

If you are inside a function, in this case:

stringstream myStream(s);  

myStream is allocated on the stack. It will be destroyed automatically at the end of the scope. It is also very efficient.

In this case:

stringstream *myStream = new stringstream(s);

myStream is a pointer to an object allocated on the heap. It will be destroyed when you call delete myStream.

Most of the time you want to use the stack. It is efficient and the memory gets freed automatically for you. It also just happens to be less to type.

Sometimes you need more control. You want a function to create an object and have it exist until you explicitly delete it. These cases should be rare, and with modern C++ you should use something like unique_ptr or shared_ptr to make managing the memory easier.

Vaughn Cato
  • 59,967
  • 5
  • 75
  • 116
  • "myStream is allocated on the stack" - myStream may be on the stack if inside a function, but it likely won't be if outside a function (implementations can choose, but there's normally a memory region for dynamically initialised globals/statics). "[for new case] myStream is allocated on the heap" - no, `myStream` is a pointer variable and allocation is as before, but it will address memory newly allcoated on the heap. `myStream` isn't destroyed by `delete myStream` - just pointed-to object is. Might seem nit-picking, but if someone needs to ask this stuff they won't "know what you mean". – Tony Delroy Apr 19 '13 at 02:24
1

I heard that first return a pointer and "dynamically" allocates memory. But I don't truly understand the difference.

That's correct. There are several types of memory your program can use:

  • static/global memory, which is allocated at compile time with the same address used for that variable throughout the lifetime of the program

  • stack memory, which is local to a function/scope such that the variable is created as the scope is entered and destroyed automatically when the scope's exited. The address relative to the stack pointer is calculated at compile time, so it's very efficient and fast, for example:

    if (int x = f()) std::cout << x; // x is in scope inside the "if" scope, but after this line it's destroyed

  • free-store (heap) memory, which is dynamically allocated which means the address of memory where the variable can be created is calculated at run time, and depends on what other dynamic memory requests have already been made. This is relatively slow, and manual in that you must have a matching delete to destroy the object and deallocate the memory.

Tony Delroy
  • 94,554
  • 11
  • 158
  • 229
0

When you use new to obtain pointer to new object (first case), you must explicitly delete this pointer some time later. If you don't, you will have memory leak.

In second case, you do not need to worry about this - object will be automatically disposed of after leaving current scope by calling its destructor.

mvp
  • 94,368
  • 12
  • 106
  • 137
0

In the first case: stringstream *myStream = new stringstream(s);

Your objects are stored on the stack which has some limited memory and if you allocate very big objects in this way (objects whose members are also allocated on the stack and having some insane size), you may get stack overflow. You might feel this constraint especially in embedded systems, which has limited resources.

Moreover, the destructors of the objects allocated on stack are called as soon as program goes out of scope, which makes them unpractical in many situations. Therefore it is usually preferred to store objects on the heap by using keyword new and objects are stored in heap memory whose size is roughly limited to your available RAM. However, in this case, you need to delete the object in the right place or you can get memory leaks.

fatihk
  • 7,509
  • 22
  • 44