From: Vladimir Vassilevsky on


Rune Allnor wrote:

> On 10 Mai, 14:57, Vladimir Vassilevsky <nos...(a)nowhere.com> wrote:
>
>>Rune Allnor wrote:
>>
>>>I don't remember off the top of my head if 'new' is a reserved
>>>word in C. Assuming it's not - and also assuming that the compiler
>>>indeed worked in C mode - that would be a bug in the compiler
>>>rather than a C/C++ clash.
>>
>>>Not that it makes any difference, where and when one hits the
>>>snag...
>>
>>Question to C++ - ers:
>
>
> No i's declared above?
>
>
>>for(int i = 0; i < 100; i++)
>> {
>> // do something
>> }
>>
>>cout << i;
>>
>>I've seen compilers that compile this as well as the others that won't
>>compile. Which ones are right?
>
>
> If a variable i has been declared before the loop,
> this should compile, but the i inside the loop hides
> the i outside the loop.

Some compilers see declarations in for(), if() or while() statements as
been outside of the { block }; others consider those declarations as
inside of the { block }. I am yet to see the explanation which variant
is right and why.


Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com
From: Vladimir Vassilevsky on


Rob Gaddi wrote:

> On 5/11/2010 5:22 PM, Erik de Castro Lopo wrote:
>
>> Rune Allnor wrote:
>>
>>> I know that C99 introduced certain keywords and extensions
>>> that did not make it to the C++ standard, but they weren't
>>> too many. Of course, some of the C99 extensions, like fixed-
>>> size integer data types, were particularly pertinent to DSP,
>>> which in turn means that somebody playing with DSP are far
>>> more likely to hit the C/C++ incopatibility snags...
>>
>>
>> Here is a piece of C code (from Chris Torek) that gives different
>> results when compiled with tandards compliant C and C++ compilers.
>>
>> #include<stdio.h>
>>
>> struct A { int x[1]; };
>>
>> int
>> main(void)
>> { struct B { struct A { int x[1000]; } b; } var;
>>
>> printf("sizeof(struct A) = %lu\n", (unsigned
>> long)sizeof(struct A));
>>
>> return 0;
>> }
>>
>> Hope this helps.
>>
>> Erik
>
>
> And yet it's a bit of a pathological case, isn't it? No, at the end of
> the day C++ and C (and for that matter C99) aren't 100% compatible, and
> you need to know which language you're writing for. But the vast
> majority of C code will just work.

Heck, any more or less complex project likely won't work when compiled
in a different version of the same toolset; not to say anything about
different toolsets or C vs C++. Although the 99.99% of code just works,
it could mess up in completely innocent places.

VLV




From: Muzaffer Kal on
On Wed, 12 May 2010 14:05:27 -0500, Vladimir Vassilevsky
<nospam(a)nowhere.com> wrote:
>Rune Allnor wrote:
>...
>> If a variable i has been declared before the loop,
>> this should compile, but the i inside the loop hides
>> the i outside the loop.
>
>Some compilers see declarations in for(), if() or while() statements as
>been outside of the { block }; others consider those declarations as
>inside of the { block }. I am yet to see the explanation which variant
>is right and why.

I have a relatively old copy of the C++ standard but here is what it
says in section 6.5.3:

The for statement
for (for-init-statement condition ; expression ) statement
is equivalent to
{
for-init-statement
while ( condition ) {
statement
expression;
}
}

So the declaration in init portion of the for is definitely in a
sub-block. Mind you this is from a april 28, 1995 draft of the
standard but I can't imagine they have changed that feature.
--
Muzaffer Kal

DSPIA INC.
ASIC/FPGA Design Services

http://www.dspia.com
From: Jason on
On May 12, 3:05 pm, Vladimir Vassilevsky <nos...(a)nowhere.com> wrote:
> Rune Allnor wrote:
> > On 10 Mai, 14:57, Vladimir Vassilevsky <nos...(a)nowhere.com> wrote:
>
> >>Rune Allnor wrote:
>
> >>>I don't remember off the top of my head if 'new' is a reserved
> >>>word in C. Assuming it's not - and also assuming that the compiler
> >>>indeed worked in C mode - that would be a bug in the compiler
> >>>rather than a C/C++ clash.
>
> >>>Not that it makes any difference, where and when one hits the
> >>>snag...
>
> >>Question to C++ - ers:
>
> > No i's declared above?
>
> >>for(int i = 0; i < 100; i++)
> >>  {
> >>  // do something
> >>  }
>
> >>cout << i;
>
> >>I've seen compilers that compile this as well as the others that won't
> >>compile. Which ones are right?
>
> > If a variable i has been declared before the loop,
> > this should compile, but the i inside the loop hides
> > the i outside the loop.
>
> Some compilers see declarations in for(), if() or while() statements as
> been outside of the { block }; others consider those declarations as
> inside of the { block }. I am yet to see the explanation which variant
> is right and why.
>
> Vladimir Vassilevsky
> DSP and Mixed Signal Design Consultanthttp://www.abvolt.com- Hide quoted text -
>
> - Show quoted text -

As Muzaffer pointed out, a declaration inside a for(), etc. statement
should have scope only inside the { } block. One notable violator of
this rule was previous versions of Microsoft Visual C++, who would
treat the declaration as if it was outside of the for() and therefore
had scope beyond the end of the for loop block. I think they've fixed
this in more recent versions, perhaps since they moved to the .NET
naming convention.

Jason