Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Limitations and workarounds to expressions defining size of an array

Reply
Thread Tools

Limitations and workarounds to expressions defining size of an array

 
 
army1987
Guest
Posts: n/a
 
      09-04-2012
On Tue, 04 Sep 2012 21:26:29 +0000, army1987 wrote:

> On Tue, 04 Sep 2012 17:07:39 -0400, Eric Sosman wrote:
>
>> Don't confuse definition with optimization. Also, try your
>> compilers on
>>
>> strcspn("BartC", "abcdefghijklmnopqrsBCuvwxyz")
>>
>> ... and let us know if they replace it with `3'.

>
> You meant strspn?


BTW, I'm shocked by the result. It optimizes away the call to strspn but
it actually calls printf with arguments "%d\n" and 3? WTF?



--
[ T H I S S P A C E I S F O R R E N T ]
Troppo poca cultura ci rende ignoranti, troppa ci rende folli.
-- fathermckenzie di it.cultura.linguistica.italiano
<http://xkcd.com/397/>
 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      09-04-2012
On 2012-09-04, BartC <(E-Mail Removed)> wrote:
> "Kaz Kylheku" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On 2012-09-04, Francois Grieu <(E-Mail Removed)> wrote:
>>> Can I
>>> - use floating point at all ?
>>> - if yes: use sqrt, log, exp ?

>>
>> No. Constant expressions cannot contain funtion calls.

>
> Yet, an expression such as sqrt(9.0) seems to be reduced to 3.0 on the first
> two compilers I tried.


That has no bearing on what is constitutes "constant expression".

> It's not much of a stretch to allow that to be used as an array dimension,
> the main stumbling block being that 3.0 is not integral, while adding an
> (int) cast makes it look like a runtime expression.


Why does a floating to integer conversion "look" runtime, but a square
root does not?
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      09-04-2012
On 09/04/2012 05:31 PM, army1987 wrote:
> On Tue, 04 Sep 2012 21:26:29 +0000, army1987 wrote:
>
>> On Tue, 04 Sep 2012 17:07:39 -0400, Eric Sosman wrote:
>>
>>> Don't confuse definition with optimization. Also, try your
>>> compilers on
>>>
>>> strcspn("BartC", "abcdefghijklmnopqrsBCuvwxyz")
>>>
>>> ... and let us know if they replace it with `3'.

>>
>> You meant strspn?

>
> BTW, I'm shocked by the result. It optimizes away the call to strspn but
> it actually calls printf with arguments "%d\n" and 3? WTF?


The only printf() calls explicitly mentioned in this thread so far were
in my example program, which used "%zu", not "%d", so you're presumably
referring to some other program. What does the source code look like?

 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      09-04-2012
On 2012-09-04, army1987 <(E-Mail Removed)> wrote:
> On Tue, 04 Sep 2012 21:26:29 +0000, army1987 wrote:
>
>> On Tue, 04 Sep 2012 17:07:39 -0400, Eric Sosman wrote:
>>
>>> Don't confuse definition with optimization. Also, try your
>>> compilers on
>>>
>>> strcspn("BartC", "abcdefghijklmnopqrsBCuvwxyz")
>>>
>>> ... and let us know if they replace it with `3'.

>>
>> You meant strspn?

>
> BTW, I'm shocked by the result. It optimizes away the call to strspn but
> it actually calls printf with arguments "%d\n" and 3? WTF?


Optimizing printf format strings is harder to do than optimizing calls to
strspn.

To optimize strspn (in the above way), all you have to do is check that the two
arguments are both literal constants, and then call strspn inside the
compiler's execution environment, and substitute the result.

To optimize "%d\n" you have to parse the format string.

People implement whatever is easier. (Which is why we have numerous
concessions all over the C language in the first place.)
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      09-04-2012


"Kaz Kylheku" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2012-09-04, BartC <(E-Mail Removed)> wrote:
>> "Kaz Kylheku" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> On 2012-09-04, Francois Grieu <(E-Mail Removed)> wrote:
>>>> Can I
>>>> - use floating point at all ?
>>>> - if yes: use sqrt, log, exp ?
>>>
>>> No. Constant expressions cannot contain funtion calls.

>>
>> Yet, an expression such as sqrt(9.0) seems to be reduced to 3.0 on the
>> first
>> two compilers I tried.

>
> That has no bearing on what is constitutes "constant expression".


The sqrt() call is obviously being evaluated here as the well-known
mathematical operation rather than an arbitrary C function. (Unless the
compiler is clever enough to evaluate and reduce the C code inside an actual
sqrt() function somewhere.)

So the compiler could easily include mathematical functions amongst what are
considered constant expressions. That it doesn't do so is presumably the
standard saying it's not allowed (because it's not always practical to do).
That's unfortunate, because it would be handy.

>> It's not much of a stretch to allow that to be used as an array
>> dimension,
>> the main stumbling block being that 3.0 is not integral, while adding an
>> (int) cast makes it look like a runtime expression.

>
> Why does a floating to integer conversion "look" runtime, but a square
> root does not?


From the error messages I got ('non-integer expression' with sqrt(), rather
than 'dynamic expression' with (int)sqrt(), although I can't replicate those
now..)

--
Bartc


 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      09-04-2012
On 2012-09-04, BartC <(E-Mail Removed)> wrote:
>> Why does a floating to integer conversion "look" runtime, but a square
>> root does not?

>
> From the error messages I got ('non-integer expression' with sqrt(), rather
> than 'dynamic expression' with (int)sqrt(), although I can't replicate those
> now..)


That's just a consequence of the order in which some checks are applied.

Suppose that some context requires an integral, constant expression.

If you use an expression which is floating-point, and non-constant,
which error message will appear? A complaint that the expression isn't
integral, or that it's non-constant?

If the compiler stops checking an expression after the first constraint
is found, it's going to be whatever is checked first.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-04-2012
On 9/4/2012 5:25 PM, James Kuyper wrote:
> On 09/04/2012 05:07 PM, Eric Sosman wrote:
>> On 9/4/2012 3:22 PM, BartC wrote:
>>> "Kaz Kylheku" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> On 2012-09-04, Francois Grieu <(E-Mail Removed)> wrote:
>>>>> Can I
>>>>> - use floating point at all ?
>>>>> - if yes: use sqrt, log, exp ?
>>>>
>>>> No. Constant expressions cannot contain funtion calls.
>>>
>>> Yet, an expression such as sqrt(9.0) seems to be reduced to 3.0 on the
>>> first two compilers I tried.

>>
>> Don't confuse definition with optimization. Also, try your
>> compilers on
>>
>> strcspn("BartC", "abcdefghijklmnopqrsBCuvwxyz")
>>
>> ... and let us know if they replace it with `3'.

>
> That expression has a value of 0. Were you thinking of strspn()?


Indeed, I was. Too much 'c', I guess.

> [...] Does your compiler have any problems
> with it?


BartC reports the compilers he tested implemented `sqrt(9.0)'
as `3.0', implying that the actual function call was not performed
at run-time. (He presumably used extra-linguistic means to find
this out.) He went on to suggest that this somehow put `sqrt(9.0)'
on an equal footing with a "constant expression." My example was
intended to show another function whose value is knowable before
run-time, hence similar to `sqrt(9.0)', but most likely not similarly
optimized. That is, I was trying to show the fallacy of reasoning
from observed behavior backward to the Standard, instead of from the
Standard forward to required behavior.

What I showed instead was carelessness, sigh.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
"The speed at which the system fails is usually not important."
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-04-2012
"BartC" <(E-Mail Removed)> writes:
> "Kaz Kylheku" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On 2012-09-04, Francois Grieu <(E-Mail Removed)> wrote:
>>> Can I
>>> - use floating point at all ?
>>> - if yes: use sqrt, log, exp ?

>>
>> No. Constant expressions cannot contain funtion calls.

>
> Yet, an expression such as sqrt(9.0) seems to be reduced to 3.0 on the
> first two compilers I tried.


Yes, I'm sure that's a common optimization.

But the phrase "constant expression" is defined by the standard in
terms of what it can contain; it doesn't merely mean "an expression
that a sufficiently clever compiler can evaluate at compile time".

> It's not much of a stretch to allow that to be used as an array
> dimension, the main stumbling block being that 3.0 is not integral,
> while adding an (int) cast makes it look like a runtime expression.


But where do you draw the line between constant and non-constant
expressions? The authors of the standard already decided exactly
where to draw that line. Moving it in the direction of treating
more expressions as constant risks imposing requirements that some
compilers may not be able to meet.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-05-2012
Eric Sosman <(E-Mail Removed)> writes:
[...]
> That is, I was trying to show the fallacy of reasoning
> from observed behavior backward to the Standard, instead of from the
> Standard forward to required behavior.
>
> What I showed instead was carelessness, sigh.


What you showed *in addition* was carelessness.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-05-2012
On 9/4/2012 7:51 PM, Keith Thompson wrote:
> "BartC" <(E-Mail Removed)> writes:
>> "Kaz Kylheku" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> On 2012-09-04, Francois Grieu <(E-Mail Removed)> wrote:
>>>> Can I
>>>> - use floating point at all ?
>>>> - if yes: use sqrt, log, exp ?
>>>
>>> No. Constant expressions cannot contain funtion calls.

>>
>> Yet, an expression such as sqrt(9.0) seems to be reduced to 3.0 on the
>> first two compilers I tried.

>
> Yes, I'm sure that's a common optimization.
>
> But the phrase "constant expression" is defined by the standard in
> terms of what it can contain; it doesn't merely mean "an expression
> that a sufficiently clever compiler can evaluate at compile time".
>
>> It's not much of a stretch to allow that to be used as an array
>> dimension, the main stumbling block being that 3.0 is not integral,
>> while adding an (int) cast makes it look like a runtime expression.

>
> But where do you draw the line between constant and non-constant
> expressions? The authors of the standard already decided exactly
> where to draw that line. Moving it in the direction of treating
> more expressions as constant risks imposing requirements that some
> compilers may not be able to meet.


The Standard draws a line and says "All C compilers accept
everything on this side of the line as constants." But the line
is permeable: "An implementation may accept other forms of constant
expressions" (6.6p10). The upshot:

1) A C compiler must accept `static int x[2+1];', because
`2+1' is on the hither side of the line,

2) A C compiler may reject `static int x[strlen("abc")];'
because `strlen("abc")' is on the thither side, but

3) A C compiler may accept `static int x[strlen("abc")];'
under the "other forms" allowance, and need not even
issue a diagnostic.

Cases (2) and (3) illustrate why "It works with my compiler"
does not define the language.

--
Eric Sosman
(E-Mail Removed)d
"The speed at which the system fails is usually not important."
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
total array size limitations? glennklockwood C Programming 10 03-16-2009 08:49 AM
service-policy output on a 3560 doesn't work, any workarounds ? hm klette Cisco 2 10-21-2005 09:36 AM
ASP.NET restricts server forms to one per page... workarounds... ??? nzanella@gmail.com ASP .Net 3 01-14-2005 04:54 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM
g++: class size or file size limitations: NONTRIVIAL BUG Neil Zanella C++ 4 10-09-2003 01:49 PM



Advertisments