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

8
  • 1
    I have errors. What compiler are you trying this on? Commented Oct 8, 2011 at 11:52
  • sir i am using gcc linux and its woking perfectly fine :/ Commented Oct 8, 2011 at 11:56
  • u can only use const while creating arrays. like const int size = strlen(argv[2]); int array[size]; Commented Oct 8, 2011 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. Commented Oct 8, 2011 at 12:02
  • @iammilind the same way any stack variable is allocated: by subtracting from ESP. Commented Oct 8, 2011 at 12:04

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@mainajaved: It's the latest ISO standard for the C language, from 1999.
5

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

Comments

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).

Comments

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.

3 Comments

sir it gave me no error like i dont understand why... all my concepts are ruined :/
@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.
ok right thanks sir also sir kindly dive me an example to compile using -pendantic option i didnt find it any where
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.

2 Comments

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 You're right. I edited the answer to make it clear I'm talking about C++.
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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.