11

hello every one i want to ask that i have read that we can declare dynamic array only by using pointer and using malloc or newlike

int * array = new int[strlen(argv[2])];

but i have wrote

int array[strlen(argv[2])];

it gave me no error

i have read that static array can only be declared by giving constant array size but here i have given a variable size to static array

why is it so thanks


is it safe to use or is there chance that at any latter stages it will make problem i am using gcc linux

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
mainajaved
  • 7,905
  • 10
  • 36
  • 44
  • 1
    I have errors. What compiler are you trying this on? – atoMerz Oct 08 '11 at 11:52
  • sir i am using gcc linux and its woking perfectly fine :/ – mainajaved Oct 08 '11 at 11:56
  • u can only use const while creating arrays. like const int size = strlen(argv[2]); int array[size]; – YAHOOOOO Oct 08 '11 at 12:02
  • Apart from the question, sometimes I wonder, how even C99 is handling VLAs ? How can it allocate contiguous memory for the length which is not even known at compile time. – iammilind Oct 08 '11 at 12:02
  • @iammilind the same way any stack variable is allocated: by subtracting from `ESP`. – tenfour Oct 08 '11 at 12:04
  • @mainajaved It is an error in VS2010. – atoMerz Oct 08 '11 at 12:10
  • @iammilind: As tenfour says, it's not too hard, and Linux has a non-standard `alloca()` function to do the same (though the scoping is a bit funny I think this sort of construction defies the block scoping and the allocation lives till the end of the *function*). In C this sort of thing can't do much harm because of the "dumb" types. Actually I don't know of any C++ equivalent that allocates dynamically on the stack. – Kerrek SB Oct 08 '11 at 12:14
  • possible duplicate of [C/C++: Array size at run time w/o dynamic allocation is allowed?](http://stackoverflow.com/questions/737240/c-c-array-size-at-run-time-w-o-dynamic-allocation-is-allowed) – Bo Persson Mar 31 '12 at 19:59

6 Answers6

18

What you have is called a variable-length array (VLA), and it is not part of C++, although it is part of C99. Many compilers offer this feature as an extension.

Even the very new C++11 doesn't include VLAs, as the entire concept doesn't fit well into the advanced type system of C++11 (e.g. what is decltype(array)?), and C++ offers out-of-the box library solutions for runtime-sized arrays that are much more powerful (like std::vector).

In GCC, compiling with -std=c++98/c++03/c++0x and -pedantic will give you a warning.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 2
    @mainajaved: It's the latest ISO standard for the C language, from 1999. – Kerrek SB Oct 08 '11 at 12:06
  • See also [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Vadzim Nov 07 '18 at 13:32
5

C99 support variable length array, it defines at c99, section 6.7.5.2.

lostyzd
  • 4,515
  • 3
  • 19
  • 33
3

What you have written works in C99. It is a new addition named "variable length arrays". The use of these arrays is often discouraged because there is no interface through which the allocation can fail (malloc can return NULL, but if a VLA cannot be allocated, the program will segfault or worse, behave erratically).

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
1
int array[strlen(argv[2])];

It is certainly not valid C++ Standard code, as it is defining a variable length array (VLA) which is not allowed in any version of C++ ISO Standard. It is valid only in C99. And in a non-standard versions of C or C++ implementation. GCC provides VLA as an extension, in C++ as well.

So you're left with first option. But don't worry, you don't even need that, as you have even better option. Use std::vector<int>:

std::vector<int> array(strlen(argv[2])); 

Use it.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • sir it gave me no error like i dont understand why... all my concepts are ruined :/ – mainajaved Oct 08 '11 at 11:57
  • @mainajaved: As I said, it is provided as extension. If you're using GCC, then compile your program with `-pedantic` option, you will get error. – Nawaz Oct 08 '11 at 11:58
  • ok right thanks sir also sir kindly dive me an example to compile using -pendantic option i didnt find it any where – mainajaved Oct 08 '11 at 12:11
1

Some compilers aren't fully C++ standard compliant. What you pointed out is possible in MinGW (iirc), but it's not possible in most other compilers (like Visual C++).

What actually happens behind the scenes is, the compiler changes your code to use dynamically allocated arrays.

I would advice against using this kind of non-standard conveniences.

Paul Manta
  • 30,618
  • 31
  • 128
  • 208
  • I realize that you may be answering the `C++` part of the question and be completely right if the question is considered as such, but for a `C` question, your answer is all wrong. Variable length arrays are standard, are typically allocated on the stack, and Visual C does not support them only because it's not C99-compliant. – Pascal Cuoq Oct 08 '11 at 11:57
  • @Pascal You're right. I edited the answer to make it clear I'm talking about C++. – Paul Manta Oct 08 '11 at 11:58
1

It is not safe. The stack is limited in size, and allocating from it based on user-input like this has the potential to overflow the stack.

For C++, use std::vector<>.

Others have answered why it "works".

tenfour
  • 36,141
  • 15
  • 83
  • 142