what is it for an array ? int f(int VAR) { int tab[VAR]; .. }

A

Alain Spineux

in

int f(int n)
{
int tab[n];
tab[n]=1;
return tab[n];
}

What does
int tab[n];
means ?

Thanks
 
T

Tom St Denis

in

int f(int n)
{
  int tab[n];
  tab[n]=1;

Undefined behaviour here.
  return tab[n];

And here.
}

What does
int tab[n];
means ?

It's a "variable length array" something added to C99 and generally a
really bad idea. Suppose I inadvertently get your function called
with n == 2^31 or something huge. Why this was added to C is beyond
me, I certainly will NEVER use it for this exact reason.

Tom
 
S

Seebs

It's a "variable length array" something added to C99 and generally a
really bad idea. Suppose I inadvertently get your function called
with n == 2^31 or something huge. Why this was added to C is beyond
me, I certainly will NEVER use it for this exact reason.

Long story short, it came from the high-performance side of the world,
I think the original proposal was from either Cray or SGI (which is ambiguity
about when it was proposed, not who proposed it). Some of the people who
care about very small proportional differences in runtime found that this
provided enough performance improvement to be worth it in some cases, when
compared with more heavyweight stuff (like malloc) or less tuned stuff (like
just declaring a "big enough" array).

I think I've actually used them, but I'm not quite sure. If so, it's in
cases where the thing that determines the size of the array is very definitely
capped, and I need an amount of scratch space but I don't know what the
amount is. So in my case, I probably could replace them with [8192] or
something. Which is why I don't know whether I've used them. But then, I'm
not in the high-performance computing part of the world.

-s
 
J

John Bode

It's a "variable length array" something added to C99 and generally a
really bad idea.  Suppose I inadvertently get your function called
with n == 2^31 or something huge.  Why this was added to C is beyond
me, I certainly will NEVER use it for this exact reason.

They have their uses. If I need a smallish temporary array, it's a
helluva lot cleaner to use a VLA than to use malloc/free.

As for your "what if...", that's what preconditions are for.
 
J

John Bode

in

int f(int n)
{
  int tab[n];
  tab[n]=1;
  return tab[n];

}

What does
int tab[n];
means ?

Thanks

C99 introduced the notion of a variable-length array (VLA), where the
array size isn't known until runtime. Note that "variable" simply
means that the array length isn't fixed at compile time. You cannot
change the length of the array after it has been declared.

So, if you called f with the value 10, it would create a 10-element
array of int (indexed 0 through 9). If you called f with the value
1000, it would create a 1000-element array.

VLAs are useful, but you need to be careful with them, and they're not
100% interchangeable with regular arrays (they can't be declared
static or extern, nor can they be members of a struct or union type).
They may be allocated in such a way that if n is too large, you will
exceed the memory available for such variables (whether it's on the
stack or some other mechanism).
 
E

Eric Sosman

in

int f(int n)
{
int tab[n];
tab[n]=1;
return tab[n];
}

What does
int tab[n];
means ?

It means "`tab' is an array of `n' elements of type `int'."
The "C99" version of the C Standard introduced the ability to
use a non-constant expression like `n' as an array size; prior
to C99 all arrays had constant sizes. The new construct is
called a "variable length array" or "VLA."

It also means "If `n' is zero or negative, or even if `n' is
positive but large, the program will do something unpredictable
and probably unwelcome." C99 added the VLA, but neglected to
add any way to detect or deal with invalid sizes or with memory
depletion.

It's worth pointing out that the code you've shown has
unpredictable behavior even if the VLA allocation is successful.
Just as the array `int flab[10]' has ten elements numbered 0
through 10-1, so `int tab[n]' has `n' elements numbered 0 through
`n-1'. When you attempt `tab[n] = 1' or `return tab[n]', you are
attempting to access an element outside the bounds of an array,
just as if you had attempted to access `flab[10]'. There's no
telling what might happen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top