Topic: Is this defined?


Author: "Rufus V. Smith" <Rufus_Smith@GuntherIntl.com>
Date: 1999/07/13
Raw View
>Anthony DeRobertis wrote in message ...
>>void f3(const char *x) {
>>   cout << *x;
>>   f2();
>>   cout << *x << endl;
>>}
>>
>> is the compiler entitled the
>>assumption that *x in f3 won't change because it is const?
>
>--
-

with the const char* x,  you only are telling the compiler to disallow
modification of *x within
the function body of f3.  You are saying what you will do to it, not what
you expect of it.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Tristan Wibberley <bloater@cus.umist.ac.uk>
Date: 1999/07/13
Raw View
Bill Wade wrote:
>
> Anthony DeRobertis wrote in message ...
> >void f3(const char *x) {
> >   cout << *x;
> >   f2();
> >   cout << *x << endl;
> >}
> >
> > is the compiler entitled the
> >assumption that *x in f3 won't change because it is const?
>
> The compiler may not make that assumption.

How can a compiler tell that f2 will not change the value of *x? Does
this mean that the useful optimisation can never be done without the
programmer explicitly telling the compiler it can?

--
Tristan Wibberley


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: AllanW <allan_w@my-deja.com>
Date: 1999/07/13
Raw View
Anthony DeRobertis wrote
> > >void f3(const char *x) {
> > >   cout << *x;
> > >   f2();
> > >   cout << *x << endl;
> > >}
> > >
> > > is the compiler entitled the
> > >assumption that *x in f3 won't change because it is const?

Entitled? No. However, the assumption does allow more optimized
code. Your compiler might use this optimization by default.
Look for a setting which disables "anti-aliasing"; if you can't
find this, then just try turning off optimizations completely.

In article <37891B83.553AA5A0@cus.umist.ac.uk>,
> How can a compiler tell that f2 will not change the value of
> *x? Does this mean that the useful optimisation can never be
> done without the programmer explicitly telling the compiler
> it can?

Ideally, this is correct. However, some compilers (for
instance, ".000001 Not-Hard") automatically make several
assumptions unless you tell it not to. I'm not sure if this
is one of those assumptions.

--
Allan_W@my-deja.com is a "Spam Magnet," never read.
Please reply in newsgroups only, sorry.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1999/07/14
Raw View

Tristan Wibberley wrote in message <37891B83.553AA5A0@cus.umist.ac.uk>...
>
>Bill Wade wrote:
>>
>> Anthony DeRobertis wrote in message ...
>> >void f3(const char *x) {
>> >   cout << *x;
>> >   f2();
>> >   cout << *x << endl;
>> >}
>> >
>> > is the compiler entitled the
>> >assumption that *x in f3 won't change because it is const?
>>
>> The compiler may not make that assumption.
>
>How can a compiler tell that f2 will not change the value of *x? Does
>this mean that the useful optimisation can never be done without the
>programmer explicitly telling the compiler it can?


"never" is a bit strong.  If the compiler can see the source to f2()  it
might be able to determine that there is no aliasing problem.  If the
compiler can see the source to the caller of f3() it might know that there
is no aliasing problem (*x is an auto variable in the caller, and the caller
hasn't given its address to anyone else, or *x is actually a const static or
const auto object).  Remember the compiler is allowed to inline f2() or f3()
or both, even if you don't ask it to.

You can tell the compiler not to worry about aliasing by copying *x to a
local variable (which may be an anti-optimization on some systems), or on
many systems using a compiler switch (or pragma) to get this otherwise
illegal optimization.



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1999/07/09
Raw View
Anthony DeRobertis wrote in message ...
>void f3(const char *x) {
>   cout << *x;
>   f2();
>   cout << *x << endl;
>}
>
> is the compiler entitled the
>assumption that *x in f3 won't change because it is const?

The compiler may not make that assumption.  I believe proper use of the
'restrict' keyword which is being discussed for C9X would allow the compiler
to make that assumption.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: derobert@erols.com (Anthony DeRobertis)
Date: 1999/07/04
Raw View
Is the result of the following defined?

// file1.cp
char c = 'c';

char *f1() { return &c; }
void f2() { c = 'd'; }

// file1.h
extern char *f1();
extern void f2();

// file2.cp
#include "file1.h"

void f3(const char *);
int main() {
   char *x = f1();
   f3(x);
   return 0;
}

void f3(const char *x) {
   cout << *x;
   f2();
   cout << *x << endl;
}


I'd think the result should be "cd\n", or is the compiler entitled the
assumption that *x in f3 won't change because it is const?

--
Windows 95 (win-DOH-z), n. A thirty-two bit extension and graphical
shell to a sixteen bit patch to an eight bit operating system
originally coded for a four bit microprocessor which was used in a PC
built by a two bit company that couldn't stand one bit of competition.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]