3

So I've always been told you should use dynamic memory when you don't know the size of the array at compilation time. For example, the user needs to input the size of the array.

    int n;

cin >> n;

int array[n];

for(int ii = 0; ii < n; ii++)
{
    array[ii] = ii;
}

for(int ii = 0; ii < n; ii++)
{
    cout << array[ii] << endl;
}

However this works fine for me, I've always thought I would need to use a pointer and the new operator. Is dynamic memory then only for when you want to change size of the array, free up space, or having the ability to control when to release the memory? Thanks.

roverred
  • 1,841
  • 5
  • 29
  • 46
  • 1
    That's a compiler extension (VLA). Use a `std::vector` instead anyway. Don't use `new`. – chris Oct 08 '12 at 06:01
  • There is a time and place for manually managing memory and for using `std::vector` (which uses a dynamic array internally, but handles it for you). In this case, I'd say stick with the latter. – Mike Bailey Oct 08 '12 at 06:04

4 Answers4

8

It works fine because it is an extension allowed by your compiler. It is not legal C++, and I'd recommend you avoid it. In order to help you avoid it, I recommend you compile with -pedantic to flag the usage of non-standard extensions as warnings, and -Werror to treat warnings as errors. Or with -pedantic-errors if you don't want to treat all warnings as errors, just warnings of this type.

But that doesn't mean you should be using a pointer and new. You should instead use std::vector. Or possibly std::deque or std::list, but only for very special purposes. For most general purpose dynamic arrays, std::vector is the choice.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • Thanks. By extension, do you mean the compiler will read it and convert it to some form of dynamic memory? I would use vectors, but the teacher prefers us using new instead. – roverred Oct 08 '12 at 06:15
  • 1
    @user1675074: By extension, I mean it is an addition to [deviation from] the C++ standard, on the part of the writers of your compiler. It's actually allowing something from the C language that is not supposed to be allowed in C++. http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html – Benjamin Lindley Oct 08 '12 at 06:18
3

First of all, what you are using is not standard C++. Variable length arrays are a language extension, and therefore not portable. Second, you can avoid explicitly using dynamically allocated memory by using classes that do it for you behind the scenes, taking care of memory management by themselves. The obvious example for this case would be std::vector:

std::vector<int> v;

for(int ii = 0; ii < n; ii++)
{
    v.push_back(ii);
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

In C++, arrays are for static storage. The size is known at compile time only. A constant sized space would be allocated during compilation.

Normally, the compiler does not allow dynamic creation of arrays. The code which you have written is an extension of C++ which allows dynamic creation of arrays. I prefer you use std::vector instead.

Shashwat
  • 2,538
  • 7
  • 37
  • 56
0

The compile-time limitation is not only for arrays.

  1. All types in C++ have that compile-time limitation
  2. The reason why new operation can avoid it is because there is no type that contains all the heap-allocated array elements - heap allocation is outside of the type system
  3. sizeof(T) documents the feature, since it's return value is always compile-time constant.
  4. std::vector can avoid the problem since the elements are outside of std::vector's type
tp1
  • 1,197
  • 10
  • 17