473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

static array with variable amount of elements??

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
10 2738
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
Ni
Thanks!!! Is hould change that!

Jan 5 '06 #3
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
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
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
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

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

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
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
["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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in...
13
by: lovecreatesbeauty | last post by:
/* How do free() know how many elements should be freed in a dynamic array? When free a single variable, the amount of byte of memory can be retrieved from the type of variable itself. ...
2
by: lovecreatesbeauty | last post by:
/* How does free() know how many elements should be freed in a dynamic array? When it frees a single variable, the size information about amount of byte of memory can be retrieved from the...
5
by: Superfox il Volpone | last post by:
I read in the glibc documentation that using 'static' in a function can save the content of a variable. So I want to use it to write a function that return the number of days in one month. Since...
1
by: kenny8787 | last post by:
Hi, can anyone help here? I have the following code generated from a database, I want to have javascript calculate the costs of the selected items using radio buttons, subtotal the costs and...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
11
by: Bryan Parkoff | last post by:
I want to know how much static memory is limited before execution program starts. I would write a large array. The large array has 65,536 elements. The data size is double word. The static...
13
by: charlie | last post by:
I came up with this idiom for cases where a function needs a variable amount of memory for it's temporary storage so as to avoid constantly mallocing. It makes me feel a little uncomfortable to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.