1

I know that I'm not supposed to do this in C90, and it's a rather basic stuff.

char name[strlen(s)];

ArrayLength.c:11: warning: ISO C90 forbids variable length array ‘name’

Did they want me to specifically use malloc? I'm just curios here about the logic behind it.

2

4 Answers 4

8

It's forbidden because C90 doesn't support variable-length arrays (VLAs). It's really as simple as that.

Your options are:

  • Declare a fixed-length array that can cope with the maximum string length you want to work with.
  • Dynamically-allocate the array (using malloc).
  • Work with a compiler that offers VLAs a non-standard language extension, e.g. GCC. (I don't recommend this, because it means you'll end up writing non-portable code.)
  • Use C99 instead, where VLAs are supported. Note that VLAs are allocated on the stack, which can cause all sorts of issues if you run out of stack space (unlike with malloc, there's no concept of being able to check that the allocation was successful).

[Note: If you're allocating an array in order to make a copy of s, you'll need to use strlen(s)+1 as the size (remember the null terminator).]

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

1 Comment

+1 To add to this, malloc allocates memory on the heap, while C99's variable length arrays allocate memory on the stack (similar to the non-standard alloca function).
1

It's not that "they" don't want you to do it, it's simply not part of the language (or rather, wasn't prior to 1999). The standard workaround is to use malloc or alloca. (alloca is essentially identical to variable length array allocation, but is not a standard function, so it may not be available on all systems of interest. Also, some people have strong objections to it's use, but they tend to object strong to variable-length arrays for the same reasons.)

1 Comment

+1 for alloca, which I like. But typically you shouldn't (a) mix it with VLAs, (b) use it in loops, or (c) expect VLA sizeof semantics.
0

This warning points to the usage of a GNU gcc extension is a serious portablity problem.

The code is illegal, because the value of strlen(s) is not known at compile time. GNU gcc provides an extension for automatic arrays that allocate based on run time values; but relying on these makes the code out of compliance with the standard.

If the value of strlen(s) isn't known until run-time then one can bring the code into compliance by either converting to performing the allocation/deallocation explicitly on conventions arrays, or by using STL containers.(e.g. std::vector).

10 Comments

VLAs are not an extension. They have been standard C for 12 years now, and do not pose serious portability problems. The warning only tells you that the code will not work with truly antediluvian compilers.
@StephenCanon: AFAICS, MSVC doesn't support VLAs.
@OliCharlesworth: MSVC doesn't support C. (I'm being snarky, but there's some truth to it.)
@StephenCanon: MSVC doesn't support C99 (it's a matter of opinion whether C99 == C nowadays). Either way, regardless of one's opinions of MSVC's (lack of) standard support, it is a widely-used compiler. If one is aiming to write cross-platform code, one should consider MSVC as a target.
@Oli I dare say Stephen counts that one as antediluvian.
|
0

It's a matter of the language having restrictions for the presumed convenience of the compiler and its intended runtime environment.

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.