By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,974 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

static array with variable amount of elements??

P: n/a
Ni
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!!!

Jan 5 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Ni@m wrote:
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/Va...ariable-Length
Should I change the code.


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

Jan 5 '06 #2

P: n/a
Ni
Thanks!!! Is hould change that!

Jan 5 '06 #3

P: n/a
Neelesh Bodas д:
Not according to standard. Its one of those gnu extensions to C89 (and
C++)
http://gcc.gnu.org/onlinedocs/gcc/Va...ariable-Length

C99 supports this feature, too. :)
Jan 6 '06 #4

P: n/a
On Fri, 06 Jan 2006 09:44:34 +0800, albcamus <al******@hit.edu.cn>
wrote in comp.lang.c++:
Neelesh Bodas д:
Not according to standard. Its one of those gnu extensions to C89 (and
C++)
http://gcc.gnu.org/onlinedocs/gcc/Va...ariable-Length 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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 6 '06 #5

P: n/a
In comp.os.linux.development.system Jack Klein <ja*******@spamcop.net> wrote:
On Fri, 06 Jan 2006 09:44:34 +0800, albcamus <al******@hit.edu.cn>
wrote in comp.lang.c++:
Neelesh Bodas ____:
> Not according to standard. Its one of those gnu extensions to C89 (and
> C++)
> http://gcc.gnu.org/onlinedocs/gcc/Va...ariable-Length

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.


"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
Jan 6 '06 #6

P: n/a
Ni
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!!

Jan 6 '06 #7

P: n/a

Ni@m wrote:
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?

Jan 6 '06 #8

P: n/a

Peter T. Breuer wrote:
In comp.os.linux.development.system Jack Klein <ja*******@spamcop.net> wrote:
On Fri, 06 Jan 2006 09:44:34 +0800, albcamus <al******@hit.edu.cn>
wrote in comp.lang.c++:

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

Jan 6 '06 #9

P: n/a
In comp.os.linux.development.system Mi*************@tomtom.com wrote:
Peter T. Breuer wrote:
In comp.os.linux.development.system Jack Klein <ja*******@spamcop.net> wrote:
> On Fri, 06 Jan 2006 09:44:34 +0800, albcamus <al******@hit.edu.cn>
> wrote in comp.lang.c++:
>> 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
Jan 6 '06 #10

P: n/a
["Followup-To:" header set to comp.os.linux.development.system.]
On 5 Jan 2006 00:11:52 -0800, Ni@m
<ni*******@gmail.com> wrote:
Thanks!!! Is hould change that!

http://cfaj.freeshell.org/google/
--
He who lives without folly is less wise than he believes.
Jan 9 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.