-4

I am new to c++ programming and wasn't quite able to understand the following code difference:

int n;
cin >> n;
int list[n];

and

int n;
cin >> n;
int *list = new int[n];

and how c++ compiler deals with this kind of code ?

Edit: Please its not about pointers. I want to know how memory is allocated for "list" variable in the first block of the code as "n" value is determined at runtime.

possible duplicate: Why aren't variable-length arrays part of the C++ standard?

Community
  • 1
  • 1
prototype
  • 30
  • 7
  • 3
    There is surely a duplicate of this, anyway the first version allocates the vector header and class data on the stack, the second does everything on the heap (except the pointer) – Marco A. Sep 02 '14 at 11:06
  • This stuff should be covered in the first few chapters of any decent C++ book - you do have a decent C++ book I hope ? – Paul R Sep 02 '14 at 11:06
  • 1
    I've edited the title of your question to make it more adequate to the actual question. However, I didn't make it "perfectly-worded" as I didn't want to use words like "pointers" that would partially be the answer. Please review the new title and re-edit it as you seem it fit! – quetzalcoatl Sep 02 '14 at 11:08
  • 1
    You really need to spend hours reading a good [C++ programming book](http://www.stroustrup.com/Programming/). – Basile Starynkevitch Sep 02 '14 at 11:10
  • possible duplicate of [What is a Pointer?](http://stackoverflow.com/questions/153874/what-is-a-pointer) – quetzalcoatl Sep 02 '14 at 11:12
  • @MarcoA I know the second code block allocates data on heap. But i want to know how c++ compiler allocates data on stack as "n" is determined at runtime ? – prototype Sep 02 '14 at 11:13
  • @prototype n is determined at runtime but vector isn't a normal C-array, it's dynamic. You're telling it at runtime "please have a size of n elements". That information is stored on the stack or on the heap but it doesn't prevent you from adding new elements even beyond the number of n elements, in that case a reallocation happens (and that might be performance expensive). – Marco A. Sep 02 '14 at 11:18
  • thanks @MarcoA. But in place of "vector" you can use normal C array also. I think this kind of variables are allocated at the end of the stack so that stack size is not resized as these variables size is allocated at runtime. – prototype Sep 02 '14 at 11:25
  • 1
    @prototype: In (standard) C++, an automatic array has to be a fixed size; `int list[n]` is not allowed. In C, or C++ with a non-standard extension, a variable-length array is probably handled as you describe. But that's very different to `vector`, which is a fixed-size object managing a dynamic, heap-allocated array. – Mike Seymour Sep 02 '14 at 11:26
  • thanks @MikeSeymour. It solved my issue. I should have checked better before asking this question. My bad. – prototype Sep 02 '14 at 11:34

1 Answers1

3

The first creates a local automatic variable. This is (typically) stored on the function's stack frame, a block of temporary memory allocated (from the thread's stack) when the function execution begins, and released when the function returns. The object will be destroyed when it goes out of scope; that is, when the program leaves the current code block. This is usually how you want to create variables, unless you need them to live longer than that.

The vector object itself, on the stack, is a fixed size. It manages a dynamic array of n objects, allocated from the heap as described below. It automatically destroys that array, freeing its memory, when it is destroyed.

The second creates a dynamic object, from the program's free store (also known as the heap). This won't be destroyed until you explicitly do so:

delete list;

It's a very bad idea to deal directly with pointers to dynamic objects like this; it's very easy to leak the object (perhaps by returning from the function, or throwing an exception, and losing the only pointer to it), or to accidentally copy the pointer and try to delete it twice.

Hopefully, your tutorial book will teach you how to manage dynamic resources safely with smart pointers, and the general principle of RAII. If it doesn't, throw it away and get a better one.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 2
    +1 for "throw it away".. so many bad learning resources out there :| – quetzalcoatl Sep 02 '14 at 11:10
  • thanks for the info but i know that fact. I want to know how is memory is allocated for "list" in the first code block ? – prototype Sep 02 '14 at 11:17
  • @prototype: I've added some detail about memory allocation from the stack (for the vector object in the first case) and the heap (for the vector's array in both cases, and for the vector object in the second). – Mike Seymour Sep 02 '14 at 11:20
  • @MikeSeymour you should add a small bit of detail for OP's doubt [here](http://stackoverflow.com/questions/25621858/what-is-the-difference-between-normal-variable-and-variable-with-and-new?noredirect=1#comment40028657_25621858) – Marco A. Sep 02 '14 at 11:30