dynamic memory

Whats the difference between these 2 and why should I use one over the other?

1
2
3
4
5
6
7
    int i;
    cout << "how many dogs?" << endl;
    cin >> i;

    int dogs[i];



1
2
3
4
5
6
7
    int i;
    cout << "how many?" << endl;
    cin >> i;

    int *dogs= new int[i];

    delete [] dogs;


Both of these seem to achieve the same thing.


can someone explain how this works exactly?
 
int *dogs= new int[i];


why is it necessary to have a pointer to dogs?

I get that it has something to do with run time but can someone provide an example of where we don't use new which wouldn't work because of a run time error?
Last edited on
1 does not work. You can't use i as a size because it is not a constant, and you can't delete arrays stored on the stack.

2 works. You should probably wrap it in some sort of a smart pointer though, but it is otherwise correct.
The first one is incorrect, as in it's illegal in C++. Some compilers allow line 5 to work as a language extension, but the standard only permits array sizes to be constant. Line 7 is erroneous, as arrays declared this way are stored on the stack. You don't need to manually free memory allocated on the stack.

The second is correct and legal, and the array is stored on the heap. Line 5's new int[i] allocates a block of memory on the heap that's enough for i integers, and returns a pointer to the start of that memory, which is stored in dogs. The only way to access that memory is by dereferencing a pointer to it. Should you lose that pointer, you lose that memory, and for that matter, so does the OS until your program terminates.

Generally, new is used when the amount of memory required isn't known at compile-time for any reason, or when you need to allocate a massive amount of memory (the stack, compared to the heap, is quite small).

-Albatross
Last edited on
Sorry I didn't mean to leave delete there on the first 1. I understand that bit.

I think I need further explanation to fully grasp what's going on here. This is the first I hear of smart pointers.

I can almost see it but I still don't get it. This is used in vectors isn't it? To dynamically grab more memory as it needs to grow.

Is the purpose of my provided example just to show how an array can be created and destroyed? Will it have a purpose when I learn how vector uses it?
Last edited on
Hmm...I had a similar question about the pointer and the 'new' keyword few days ago.

So when you do this, int *dogs = new int[i]
the alternative way to do this is this,

1
2
int something[3] = {3, 5, 7};
int *dogs = &something[3];


only difference is that the first one will be stored in the heap and the second one will be stored in the stack.
Last edited on
Smart pointers are a way for programmers to admit that they are undisciplined and cannot keep track of their own memory to reduce the chance of a memory leak happening, namely when a pointer to memory on the heap gets lost.

With the number of things in C++ that can suddenly cause a pointer to memory to be lost (namely exceptions causing local variables to go out of scope), it's a very good habit to use smart pointers.

https://meetingcpp.com/index.php/br/items/an-overview-on-smart-pointers.html
http://www.geeksforgeeks.org/smart-pointers-cpp/ (Note that auto_ptr is deprecated.)

-Albatross
Last edited on
Thanks Albatross for the in depth explanation and your funny comment.

So smart pointers just get rid of having to use delete but in a way that guarantees that the memory will be freed?

And thanks to you too yj.
Topic archived. No new replies allowed.