Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > g++ -O2 OK, -O3 Segmentation fault

Reply
Thread Tools

g++ -O2 OK, -O3 Segmentation fault

 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      07-08-2008
Lionel B a écrit :
> Not relevant to this thread, but inlining may sometimes actually *reduce*
> code size - e.g. for very small functions (a common case for inlining)
> where the function call (code size) overhead might actually be larger
> than the inlined function code itself...
>
> int foo(const int n)
> {
> return n*n;
> }
>
> int main()
> {
> return foo(3);
> }
>
> On my system (compiled with g++ and -O<whatever>) this produces a smaller
> executable when foo() is declared inline.


In gcc, the functions considered for inlining are much bigger (default
is 600 lines of pseudocode IIRC) unless you tune it to allow only
smaller functions.

In practice, I would use -O3 optimization on very small parts of the
project.

--
Michael
 
Reply With Quote
 
 
 
 
Lionel B
Guest
Posts: n/a
 
      07-08-2008
On Tue, 08 Jul 2008 11:40:10 +0200, Michael DOUBEZ wrote:

> Lionel B a écrit :
>> Not relevant to this thread, but inlining may sometimes actually
>> *reduce* code size - e.g. for very small functions (a common case for
>> inlining) where the function call (code size) overhead might actually
>> be larger than the inlined function code itself...
>>
>> int foo(const int n)
>> {
>> return n*n;
>> }
>>
>> int main()
>> {
>> return foo(3);
>> }
>>
>> On my system (compiled with g++ and -O<whatever>) this produces a
>> smaller executable when foo() is declared inline.

>
> In gcc, the functions considered for inlining are much bigger (default
> is 600 lines of pseudocode IIRC) unless you tune it to allow only
> smaller functions.


Sure, the full(ish) story is:

-fno-inline
Don't pay attention to the inline keyword. Normally this option is
used to keep the compiler from expanding any functions inline. Note that
if you are not optimizing, no functions can be expanded inline.

-finline-small-functions
Integrate functions into their callers when their body is smaller
than expected function call code (so overall size of program gets
smaller). The compiler heuristically decides which functions are simple
enough to be worth integrating in this way.

Enabled at level -O2.

-finline-functions
Integrate all simple functions into their callers. The compiler
heuristically decides which functions are simple enough to be worth
integrating in this way.

If all calls to a given function are integrated, and the function is
declared static, then the function is normally not output as assembler
code in its own right.

Enabled at level -O3.

-finline-limit=n
By default, GCC limits the size of functions that can be inlined.
This flag allows coarse control of this limit. n is the size of functions
that can be inlined in number of pseudo instructions.

Inlining is actually controlled by a number of parameters, which may
be specified individually by using --param name=value. The -finline-
limit=n option sets some of these parameters as follows:

max-inline-insns-single
is set to n/2.
max-inline-insns-auto
is set to n/2.

....

--
Lionel B
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      07-08-2008
On Jul 7, 3:23*pm, Rares Vernica <ra...@ics.uci.edu> wrote:
> Hello,
>
> I have a strange problem.
>
> I compile my code with g++ (GCC) 4.1.2. My code runs just fine if I use
> the -O2 optimization flag. valgrind does not report any memory leak.
>
> When I use the -O3 flag, everything falls apart. The program reports
> Segmentation fault. valgrind reports some Invalid read.
>
> Moreover, if I cout something near the line for which valgrind reports
> the invalid read, then everything works fine, no more segmentation faults
> or invalid reads.


Before you follow Gianni's advice, make sure you have initialized
*every* variable, especially pointers.
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      07-08-2008
Rares Vernica wrote:
> I compile my code with g++ (GCC) 4.1.2. My code runs just fine if I use
> the -O2 optimization flag. valgrind does not report any memory leak.
>
> When I use the -O3 flag, everything falls apart. The program reports
> Segmentation fault. valgrind reports some Invalid read.


I had a similar problem when using -O3 in connection
with -fomit-frame-pointer. Boost_Regex would just
segfault on some occasions.

Removed -fomit-frame-pointer and all was fine
(this was gcc 4.2.3 IIRC).

Regards

M.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-08-2008
On Jul 8, 12:32 am, Rares Vernica <ra...@ics.uci.edu> wrote:
> Rares Vernica <ra...@ics.uci.edu> writes:
> > I compile my code with g++ (GCC) 4.1.2. My code runs just
> > fine if I use the -O2 optimization flag. valgrind does not
> > report any memory leak.


> > When I use the -O3 flag, everything falls apart. The program
> > reports Segmentation fault. valgrind reports some Invalid
> > read.


> > Moreover, if I cout something near the line for which
> > valgrind reports the invalid read, then everything works
> > fine, no more segmentation faults or invalid reads.


> It was a bug in my code. Now it works fine with -O3.


> Still, I am a little bit confused how the bug generated a
> segmentation fault only with -O3.


The generated code will be different according to the
optimization level. What the differences may be, we can only
speculate, not having seen your code, nor knowing what error
you'd made.

--
James Kanze (GABI Software) email:
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
 
Reply With Quote
 
Diego Martins
Guest
Posts: n/a
 
      07-08-2008
On Jul 7, 7:32*pm, Rares Vernica <ra...@ics.uci.edu> wrote:
> Rares Vernica <ra...@ics.uci.edu> writes:
> > I compile my code with g++ (GCC) 4.1.2. My code runs just fine if I use
> > the -O2 optimization flag. valgrind does not report any memory leak.

>
> > When I use the -O3 flag, everything falls apart. The program reports
> > Segmentation fault. valgrind reports some Invalid read.

>
> > Moreover, if I cout something near the line for which valgrind reports
> > the invalid read, then everything works fine, no more segmentation faults
> > or invalid reads.

>
> It was a bug in my code. Now it works fine with -O3.
>
> Still, I am a little bit confused how the bug generated a segmentation
> fault only with -O3.
>
> Thanks.
> Rares


what was the bug?
 
Reply With Quote
 
Rares Vernica
Guest
Posts: n/a
 
      07-08-2008
Diego Martins <> writes:

> On Jul 7, 7:32Â*pm, Rares Vernica <ra...@ics.uci.edu> wrote:
>> Rares Vernica <ra...@ics.uci.edu> writes:
>> > I compile my code with g++ (GCC) 4.1.2. My code runs just fine if I use
>> > the -O2 optimization flag. valgrind does not report any memory leak.

>>
>> > When I use the -O3 flag, everything falls apart. The program reports
>> > Segmentation fault. valgrind reports some Invalid read.

>>
>> > Moreover, if I cout something near the line for which valgrind reports
>> > the invalid read, then everything works fine, no more segmentation faults
>> > or invalid reads.

>>
>> It was a bug in my code. Now it works fine with -O3.
>>
>> Still, I am a little bit confused how the bug generated a segmentation
>> fault only with -O3.
>>
>> Thanks.
>> Rares

>
> what was the bug?


The bug was related to the number of processing steps required to
produce the results. Because I was updating some counters incorrectly,
the algorithm had to do more steps to produce the results. Still, the
results were correct, but the code was less efficient.

Nevertheless, the processing steps should all be within correct memory
bounds.

Thanks.
Rares
 
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
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces on Solaris - Segmentation fault ldvmbs@gmail.com XML 0 05-16-2005 07:21 AM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM
Re: segmentation fault exception handling Ivan Vecerina C++ 0 06-29-2003 10:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57