Topic: Can a conforming implementation define inline member functions ?for standard containers?
Author: usenet@mkarcher.dialup.fu-berlin.de (Michael Karcher)
Date: Thu, 25 Jun 2009 16:28:04 CST Raw View
James Kanze <james.kanze@gmail.com> wrote:
> > Casting function pointers to data pointers to do runtime
> > analysis of the generated code to find out whether the vector
> > functions are inlined is possible,
> I don't see how? As soon as you take the address of a function
> (inline or not), you'll get an out-of-line copy of it in the
> program.
like this:
#include <iostream>
#include <ostream>
#include <vector>
using namespace std;
void test(void)
{
vector<int> v;
v.reserve(0x8888);
}
int main(void)
{
int calls = 0;
int inlined = 0;
unsigned char * testptr = (unsigned char*)&test;
while(*testptr != 0xc3 /* return */)
{
if(*testptr == 0xE8)
{
calls++; /* call near */
testptr += 4;
}
if(*testptr >= 0x70 && *testptr <=0x7f)
inlined = 1; /* conditional jump */
testptr++;
}
if(inlined || calls != 3) /* constructor, reserve, destructor */
cout << "was inlined\n";
}
(works on x86. Outputs nothing with "gcc", outputs "was inlined" with "gcc
-O")
Regards,
Michael Karcher
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Alf P. Steinbach" <alfps@start.no>
Date: Fri, 26 Jun 2009 00:44:48 CST Raw View
* Michael Karcher:
> James Kanze <james.kanze@gmail.com> wrote:
>>> Casting function pointers to data pointers to do runtime
>>> analysis of the generated code to find out whether the vector
>>> functions are inlined is possible,
>> I don't see how? As soon as you take the address of a function
>> (inline or not), you'll get an out-of-line copy of it in the
>> program.
>
> like this:
>
> #include <iostream>
> #include <ostream>
> #include <vector>
>
> using namespace std;
>
> void test(void)
> {
> vector<int> v;
> v.reserve(0x8888);
> }
>
> int main(void)
> {
> int calls = 0;
> int inlined = 0;
> unsigned char * testptr = (unsigned char*)&test;
> while(*testptr != 0xc3 /* return */)
> {
> if(*testptr == 0xE8)
> {
> calls++; /* call near */
> testptr += 4;
> }
> if(*testptr >= 0x70 && *testptr <=0x7f)
> inlined = 1; /* conditional jump */
> testptr++;
> }
> if(inlined || calls != 3) /* constructor, reserve, destructor */
> cout << "was inlined\n";
> }
>
> (works on x86. Outputs nothing with "gcc", outputs "was inlined" with "gcc
> -O")
Uhm, to avoid silly-comments you should have quoted your own earlier article
that this code exemplifies:
> Casting function pointers to data pointers to do runtime analysis of
> the generated code to find out whether the vector functions are inlined
> is possible, and you probably will manage to write such a program for
> your platform. But the program will need to use expressions with
> unspecified or (even worse) undefined behaviour (most notably: the cast
> I mentioned at the beginning of this paragraph), so it will not be a
> "conforming" program.
Cheers,
- Alf
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Joshua Maurice <joshuamaurice@gmail.com>
Date: Fri, 26 Jun 2009 00:55:24 CST Raw View
On Jun 25, 3:28 pm, use...@mkarcher.dialup.fu-berlin.de (Michael
Karcher) wrote:
> James Kanze <james.ka...@gmail.com> wrote:
> > > Casting function pointers to data pointers to do runtime
> > > analysis of the generated code to find out whether the vector
> > > functions are inlined is possible,
> > I don't see how? As soon as you take the address of a function
> > (inline or not), you'll get an out-of-line copy of it in the
> > program.
>
> like this:
>
> #include <iostream>
> #include <ostream>
> #include <vector>
>
> using namespace std;
>
> void test(void)
> {
> vector<int> v;
> v.reserve(0x8888);
>
> }
>
> int main(void)
> {
> int calls = 0;
> int inlined = 0;
> unsigned char * testptr = (unsigned char*)&test;
[snip]
Casting a "pointer to function" to "pointer to unsigned char" and
using it after is undefined behavior (according to the C++ standard).
The statement made was that you could not tell the difference between
if the standard library function was declared inline or not for a
conforming program, and your program is not conforming (to the C++
standard) because of that cast.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: James Kanze <james.kanze@gmail.com>
Date: Fri, 26 Jun 2009 23:11:12 CST Raw View
On Jun 26, 12:28 am, use...@mkarcher.dialup.fu-berlin.de
(Michael Karcher) wrote:
> James Kanze <james.ka...@gmail.com> wrote:
> > > Casting function pointers to data pointers to do runtime
> > > analysis of the generated code to find out whether the vector
> > > functions are inlined is possible,
> > I don't see how? As soon as you take the address of a function
> > (inline or not), you'll get an out-of-line copy of it in the
> > program.
> like this:
> #include <iostream>
> #include <ostream>
> #include <vector>
> using namespace std;
> void test(void)
> {
> vector<int> v;
> v.reserve(0x8888);
> }
> int main(void)
> {
> int calls = 0;
> int inlined = 0;
> unsigned char * testptr = (unsigned char*)&test;
The above line is not legal C++, and requires a compiler
diagnostic. It won't compile with a conforming compiler.
And regardless: if you have an inline function, and you take its
address, the compiler is required to generate an out of line
copy somewhere.
> while(*testptr != 0xc3 /* return */)
> {
> if(*testptr == 0xE8)
> {
> calls++; /* call near */
> testptr += 4;
> }
> if(*testptr >= 0x70 && *testptr <=0x7f)
> inlined = 1; /* conditional jump */
> testptr++;
> }
> if(inlined || calls != 3) /* constructor, reserve, destructor */
> cout << "was inlined\n";
> }
> (works on x86. Outputs nothing with "gcc", outputs "was
> inlined" with "gcc -O")
Sounds like a bug in the compiler---the code shouldn't compile,
according to the standard. (I get a diagnostic when I compile
it with g++, although the compiler does give me an executable.)
And I still don't see where it's relevant in any way to whether
the library can define the functions "inline" or not---at best,
it might indicate that the compiler hasn't included an out of
line copy, or that it's simply generating the code differently
than you expect. The very fact that you are getting different
results depending on optimization rather proves, in fact, that
you're not testing whether the function was declared inline or
not. On my Linux box, your code says "was inlined" regardless
of the optimization level, or whether I compiled with or without
debugging. If I examine the sources, however, the function
reserve is NOT inlined.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: James Kanze <james.kanze@gmail.com>
Date: Fri, 26 Jun 2009 23:12:25 CST Raw View
On Jun 26, 8:55 am, Joshua Maurice <joshuamaur...@gmail.com> wrote:
> On Jun 25, 3:28 pm, use...@mkarcher.dialup.fu-berlin.de (Michael
> [snip]
> Casting a "pointer to function" to "pointer to unsigned char" and
> using it after is undefined behavior (according to the C++ standard).
No it's not. It's an error, and requires a diagnostic.
> The statement made was that you could not tell the difference
> between if the standard library function was declared inline
> or not for a conforming program, and your program is not
> conforming (to the C++ standard) because of that cast.
But even his code doesn't say anything about whether the
function was inline or not. Anytime the compiler generates
something other than what he is expecting, for whatever reasons,
his program outputs "was inlined". On my Linux PC box, it
always outputs "was inlined", despite the fact that at least one
of the functions involved (vector<>::reserve) is definitely not
inlined in the library sources.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]