473,842 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

g++ -O2 OK, -O3 Segmentation fault

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.

Thanks in advance,
Rares


Jul 7 '08 #1
15 10652
Rares Vernica 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.
See if this persists on the latest version of GCC.

Without looking at the code I'd point the finger at the compiler but
strange things can happen that may expose a bug in your code.

If it is a compiler bug, then file a bug against GCC - they're very good
at pointing you at a workaround and also fixing the bug in later releases.

G
Jul 7 '08 #2
Gianni Mariani schrieb:
Without looking at the code I'd point the finger at the compiler but
strange things can happen that may expose a bug in your code.
Eh? Without looking at the code I'd point my finger at the programmer.
Sounds like a definite case of invoked UB which changes its
manifestation with increased optimization level. Maybe stack corruption
or such. Valgrind may miss it with -O2 because no illegal accesses take
place with -O2 - the story may be different with -O3.

Interesting might be assembly level debugging, diff the objdump of -O2
against -O3 and inspect the location.

Regards,
Johannes

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electron ics <47************ ***********@new s.freenet.de>
Jul 7 '08 #3
Sam
Rares Vernica writes:
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.
This sounds like your code violates some nuance of C++ or C, when it comes
to pointer aliasing, and at the higher optimization level, gcc's slightly
higher aggressive optimization ends up breaking your code.

A compiler bug is always possible, but you need to prove that it's a
compiler code. And you do that by posting the minimum amount of complete
code that demonstration your problem.
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.
Yes, sounds like -O3's higher optimization level ends up exposing some error
in the code's logic that technically violates C++/C.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkh yl3YACgkQx9p3GY HlUOK+HACfSnF7v bhh6Yxdl71Gm114 fLk1
FE8AnituISGbVJW uZ8zDNjgSxfcoNO 2L
=e3eh
-----END PGP SIGNATURE-----

Jul 7 '08 #4
Rares Vernica <ra***@ics.uci. eduwrites:
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
Jul 7 '08 #5
Rares Vernica wrote:
Rares Vernica <ra***@ics.uci. eduwrites:
>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 Undefined in Undefined Behaviour?

--
Ian Collins.
Jul 7 '08 #6
Rares Vernica wrote:
Rares Vernica <ra***@ics.uci. eduwrites:
....
Still, I am a little bit confused how the bug generated a segmentation
fault only with -O3.
Using uninitialized memory ?
Jul 8 '08 #7
On Jul 7, 9:59 pm, Gianni Mariani <gi4nos...@mari ani.wswrote:
Rares Vernica wrote:
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.
See if this persists on the latest version of GCC.
Without looking at the code I'd point the finger at the
compiler but strange things can happen that may expose a bug
in your code.
While one can never exclude the possibility of a compiler bug,
especially when optimization is involved, I'd consider some
undefined behavior in his code as a more likely reason. The
time when I found more errors in the compiler than in my code
are long past.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 8 '08 #8
Rares Vernica wrote:
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 "I added a debug print command and now it doesn't crash" is a
typical symptom of something writing out of boundaries and messing up
things. I don't know exactly what valgrind checks, but if the write
happens to be done on valid, allocated memory (but not allocated by the
piece of code which is doing the writing) it may be valgrind just
doesn't notice.

-O3 probably produces code/data which is arranged differently from -O2
and just happened to show the symptom. Your -O2 code was most probably
also erroneous, but the erroneous code happened to write somewhere where
it didn't matter (or, at least, didn't cause a crash).
Jul 8 '08 #9
On Tue, 08 Jul 2008 10:23:38 +0200, Michael DOUBEZ wrote:
Rares Vernica a écrit :
>Rares Vernica <ra***@ics.uci. eduwrites: Still, I am a little bit
confused how the bug generated a segmentation fault only with -O3.

-O3 turns on the -finline-functions, -fweb and -frename-registers
options.

If you have a mind to it, you can investigate which of the optimization
trigger the bug. However, UB is UB so don't expect great discoveries.

As a matter of fact, you could make benchmarks also: some optimizations
generate much more code (like inlining) and thus reduce performances on
some systems.
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.

--
Lionel B
Jul 8 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6818
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my script execution,
3
1939
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers: /var/www/cgi-bin/script.cgi when i debug the program i get Segmentation fault gdb ./script.cgi
16
9010
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those who don't speak french arbre means tree.
6
2317
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
3
11474
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
5
3001
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
27
3379
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
7
5884
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
3
5194
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
6
5049
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on gcc. The only difference is that in first I only call "main" and in second call
0
10612
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7858
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7038
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5696
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4499
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4089
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.