static array with variable amount of elements??

N

Ni@m

I found out smth strange in g++ >= 3.3.5 =>
I was able to do
register char buff[outSTDBuffer];//// where
outSTDBuffer is a variable!!!!

Is it a bug in gcc or specification of c++ changed?? I've noticed this
a mistake after successfull compilation of my program!

Should I change the code. I think `register char buff[outSTDBuffer];`
is much safer then use pointers in case if outSTDBuffer is small!!!
 
N

Neelesh Bodas

Ni@m said:
I found out smth strange in g++ >= 3.3.5 =>
I was able to do
register char buff[outSTDBuffer];//// where
outSTDBuffer is a variable!!!!

Is it a bug in gcc or specification of c++ changed?? I've noticed this
a mistake after successfull compilation of my program!

Not according to standard. Its one of those gnu extensions to C89 (and
C++)
http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html#Variable-Length
Should I change the code.

Yes if you want it to be portable, and C++ std compliant.
 
J

Jack Klein

Neelesh Bodas дµÀ:
C99 supports this feature, too. :)

In general, yes, but not in this case. I've put back the OP's code
snippet:
register char buff[outSTDBuffer];//// where

Defining register variable of array type produces undefined behavior
in C, because any attempt to use the array in any way, even with a
subscript, results in the address of its first element being taken.
Taking the address of a register variable in C is illegal.
 
P

Peter T. Breuer

In general, yes, but not in this case. I've put back the OP's code
snippet:
register char buff[outSTDBuffer];//// where
Defining register variable of array type produces undefined behavior
in C, because any attempt to use the array in any way, even with a
subscript, results in the address of its first element being taken.
Taking the address of a register variable in C is illegal.

"Register" is only a programmer's request - the compiler is not
required to obey. One would deduce that it hasn't, since it can't.

Peter
 
N

Ni@m

So, if it's possible to use `char buff[outSTDBuffer];` what
compilators supports this?? Now I definetily know that gcc supports
that and some other beautifull things but if smbd wants to compile this
code for example with icc or VC 6.0?? Because some parts of my code
have `throw blocks` and it's really suitable to use stach array!! I
shouldn't delete[] it in any exception!!
 
N

Neelesh Bodas

Ni@m said:
So, if it's possible to use `char buff[outSTDBuffer];`

it is NOT according to std c++. Period.

First step is to make sure whether you want to code in c89 or c99 or
std c++ or some compiler's C++.
but if smbd wants to compile this code for example with icc or VC 6.0??

That is to say you want a _portable_ code. Donot write such a code
then,

Why not use std::vector instead?
 
M

Michiel.Salters

Peter said:
In comp.os.linux.development.system Jack Klein said:
Neelesh Bodas ____:
register char buff[outSTDBuffer];//// where
Defining register variable of array type produces undefined behavior
in C, because any attempt to use the array in any way, even with a
subscript, results in the address of its first element being taken.
Taking the address of a register variable in C is illegal.

"Register" is only a programmer's request - the compiler is not
required to obey. One would deduce that it hasn't, since it can't.

That sounds reasonable, but it doesn't work that way. The compiler
isn't
required to obey anything anymore, as the code causes undefined
behavior.

The logic is similar to like ' a /= 0; ' // it can't do this, so it
hasn't?

HTH,
Michiel Salters
 
P

Peter T. Breuer

In said:
Peter said:
Neelesh Bodas ____:
register char buff[outSTDBuffer];//// where
Defining register variable of array type produces undefined behavior
in C, because any attempt to use the array in any way, even with a
subscript, results in the address of its first element being taken.
Taking the address of a register variable in C is illegal.

"Register" is only a programmer's request - the compiler is not
required to obey. One would deduce that it hasn't, since it can't.
That sounds reasonable, but it doesn't work that way. The compiler
isn't required to obey anything anymore, as the code causes undefined
behavior.

You miss the point, I think. Perhaps it's different in C++ but
"register" is like "inline" in C, in that the compiler is not required
to obey the directive - indeed, it can't, since there are a limited
number of registers on the chip, and you can define an arbitrarily
large number of register variables.

Or are you perhaps saying that qualifying an array with "register"
is disallowed? In that case, the compiler ought to flag an error.

C90 requires automatic and register variables of aggregate type
(struct, array, or union) to have initializers containing only
constant expressions. (Many compilers do not adhere to this
restriction, however.)

(http://david.tribble.com/text/cdiffs.htm)
I deduce that register arrays are directly allowed for in the C90
standard, if that quote is accurate. They would have to be small
arrays, or else the compiler would have to do a no-op there.

C99 removes that restriction, allowing non-constant expressions to
be used in such initializers.

C++ allows non-constant expressions to be used in initializers for
automatic and register variables. (It also allows arbitrary
non-constant expressions to be used to initialize static and
external variables.)
The logic is similar to like ' a /= 0; ' // it can't do this, so it
hasn't?

Well, it would have to be only a similarity, since a/=0 is perfectly
doable, at least for floats, and results in a well-defined undefined
result.


Peter
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top