0

I saw this code:

int n;
cin>>n;
int c[n];

What's the difference compared to this

int *c=new int[n];
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
santosh
  • 21
  • 4
  • http://www.careerride.com/c-static-memory-dynamic-memory-allocation.aspx – Earvin Nill Castillo Mar 03 '17 at 02:12
  • This is a variable length array. Something which is not a part of C++ but some compilers (like gcc) enables it as an extension. – mpiatek Mar 03 '17 at 02:13
  • See: http://en.cppreference.com/w/cpp/language/storage_duration –  Mar 03 '17 at 02:28
  • Ya know, simply googling "stackoverflow c++ What is the difference between static and dynamic memory allocation?" Finds a lot of existing questions. For instance: http://stackoverflow.com/questions/15651486/c-what-is-the-difference-between-static-and-dynamic-allocation-of-this-array http://stackoverflow.com/questions/10575544/difference-between-declaration-and-malloc http://stackoverflow.com/questions/2672085/static-array-vs-dynamic-array-in-c http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c – TheUndeadFish Mar 03 '17 at 03:24

3 Answers3

1

The first is not valid in c++ because the size needs to be known at compile time since your data will be stored on the stack plus it has a limited scope.

The latter allocates memory for your object on the heap and will persist until manually deleted or the program execution stops.

Falla Coulibaly
  • 759
  • 2
  • 11
  • 23
0

In the first example:

int n;
cin >> n;
int c[n];

This code simply declares a variable n, accepts an integer value from the user which is stored in n, and declares an integer array of size n.

The problem here, is that you cannot have an array of variable length in your code if your compiler does not support it, because this is not allowed in c++. However, modern compilers, like the later version of the gcc compiler, support variable sized arrays, and therefore, allow variable arrays to overcome the obstacle of c++ not supporting them.

I do not know all the versions of the gcc compiler that support variable arrays, but I've tried gcc version 4.6.3, and it supports variable arrays.

Now, moving on to your second example,

int *c = new int[n];

This is a completely different concept. Here, int *c Allocates memory to hold a single element of type int. Then new int [n] allocates a block of n elements in memory of type int, with the address of the first element stored in c. Therefo, c points to an array of 5 ints. For further reading, visit: Dynamic Memory.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
0

First example

In the first example you instantiate a thing called Variable Length Array. VLAs are not defined in C++ Standard so they don't belong to the language. They are sometimes enabled by the various compilers as an extension.

VLAs have automatic storage duration which means such an array is allocated at the beginning of a code block and deallocated at the end.

Typically they are allocated on the stack (if they fit) but it's not guaranteed. So again typically they will have smaller allocation/deallocation cost compared to dynamic memory allocation (2nd example) which takes place on the heap.

Also typically VLAs allow using sizeof on them and sizeof(VLA) typically returns array_size * sizeof(type). This is true for gcc with compilation flag -std=c++14 applied.

You probably noticed a lot of typically - the thing is, as VLAs are not defined by the C++ Standard, you have to dig into their specification for the compiler you use. So best practice for C++ - don't use VLAs.

Second example

In your second example you perform dynamic memory allocation. After this is completed c points to the beginning of newly allocated block of memory of size n * sizeof(int) bytes.

c has dynamic storage duration meaning it is allocated by you and must be deallocated by you (in this case using delete[] c;

As mentioned above cost of allocation/deallocation on the heap is higher.

Note:

In C++ you should try to use std::vector<T> wherever possible. It's much safier, can grow as you add new elements and is not terribly slower.

Graham
  • 7,431
  • 18
  • 59
  • 84
mpiatek
  • 1,313
  • 15
  • 16
  • Can I get an explanation from downvoter so I can correct my answer? – mpiatek Mar 03 '17 at 03:26
  • Why is there a problem in using VLAs if the compiler supports it? Modern compilers are accepting VLAs, because it is a lot more convenient for programmers. – BusyProgrammer Mar 03 '17 at 04:02
  • @ABusyProgrammer describe a use case for VLAs that can't be implemented using standard C++ or has real downsides when implemented without VLAs. What happens in your current implementation when it doesn't fit into the stack? Are you sure about that? Are you sure when you try to compile your code using different compiler (let's say it supports VLAs) the behavior won't change? It also teaches people new to the language that this is valid C++. We should teach new people to use `vector` wherever possible. When they have more experiance they can decide if they want to use non-standard features – mpiatek Mar 03 '17 at 04:28