0

consider this program:

#include <iostream>
using namespace std;

int main ()
{
    int i;
    cout << "How many numbers would you like to type? ";
    cin >> i;

    int * p;
    p= new int[i];

    cout << "Enter the numbers: \n";

    for (int n=0; n<i; n++)
      cin >> p[n];

    cout << "You have entered: ";

    for (int n=0; n<i; n++)
      cout << p[n] << ", ";

    delete[] p;

    return 0;
}  

and this one:

#include <iostream>

using namespace std;

int main()
{
    int num;

    cout << "How many numbers would you like to type? ";
    cin >> num;

    int arr[num];

    cout << "Enter the numbers: \n";

    for (int a = 0; a <num; ++a)
        cin >>arr[a];

    cout << "You have entered: ";  

    for (int a = 0; a <num; ++a)
        cout <<arr[a]<< ", ";
    return 0;
}

Both programs are accomplishing the same task and -to me- the latter is a lot
easier to understand than the former. And now my question is why do we need dynamic memory allocation anyway?

Samuel
  • 612
  • 2
  • 9
  • 25
  • Is `int arr[num];` really correct? I've thought it needs `const size_t` as size parameter and it will not compile. – VP. Jan 30 '15 at 15:35
  • @VictorPolevoy yes it is! – Samuel Jan 30 '15 at 15:37
  • possible duplicate of [Variable length arrays in C++?](http://stackoverflow.com/questions/1887097/variable-length-arrays-in-c) – Julian Jan 30 '15 at 15:41
  • You need dynamic allocation whenever an object's lifetime should exceed its lexical scope. – molbdnilo Jan 30 '15 at 15:42
  • Pretend the number entered has eight zeros after it, then ask yourself how much automatic storage space your process has. – WhozCraig Jan 30 '15 at 15:42

2 Answers2

1

When num is not a compiler time constant, int arr[num]; is a VLA (variable length array) and that's not standard C++. It's a language extension offered by some compilers (you're using G++, I assume).

Something that's also easy to use and doesn't require you to use raw pointers and deal with manual dynamic allocations is std::vector:

int i;
cout << "How many numbers would you like to type? ";
if (!(cin >> i)) {   // error checking is important
    cout << "Not a number, abort!\n";
    return 1;
}

std::vector<int> numbers(i);

cout << "Enter the numbers: \n";

for (int a = 0; a < i; ++a)
    cin >> numbers[a];
jrok
  • 54,456
  • 9
  • 109
  • 141
  • you're right but then how do i recognize a compiler extension (especially when i am new to programming and have only one compiler installed) one more thing on which compiler would the second program fail? – Samuel Jan 30 '15 at 16:07
0

Consider what happens if this array you create needs to survive past the scope of the function it is created in. For example, if this isn't the main() function, but some helper function that returns the memory to main. Or perhaps you have no way of knowing in advance, how many objects to create. For example, you could be maintaining a tree or graph-based data structure based on dynamic real-time data.

In the simple example you give, you're correct: dynamic allocation of an array on the heap is probably a waste and can easily lead to problems like memory leaks if done poorly (although there are ways around that, like using modern RAII techniques for all your allocation). But more complicated scenarios are made much simpler if dynamic memory is available.

Ben
  • 8,725
  • 1
  • 30
  • 48