Bizzare optimization problem


 
Thread Tools Search this Thread
Top Forums Programming Bizzare optimization problem
# 1  
Old 03-11-2011
Bizzare optimization problem

I have a C program that, for the life of me, I can't see any possible stack corruption in but for some reason corrupts a local variable to 0 when not referenced. My compiler is gcc 4.3.4.

But my question's not really a code question, it's a compiler question. The glitch is weirdly specific: it happens at -O2 and higher. It does not happen when I individually and laboriously enable every possible -O1 and -O2 option(as reported by gcc -c -Q -O0 --help=optimizers, etc.) -- only the actual -O2 flag will cause it. What might gcc be doing in -O2 that it doesn't do when I enable every possible -O2 option?
# 2  
Old 03-15-2011
While this does sound wierd I tend to believe that the program is at fault rather than the compiler. One thing that might help would be to compile it with a C++ compiler. I actually do this with most of my C programs. C++ is almost a superset of C and the few things I give up are things I don't miss. C++ often detects errors in my code that C has missed. Also, rather than trimming compiler options, try to reduce the program to the smallest possible amount of code that still has the bug.
# 3  
Old 03-15-2011
I seem to recall also coming across that issue in that particular version of GCC. What happens if you compile your program with GCC 4.4 or later?
# 4  
Old 03-15-2011
That's the problem; so much as sneezing on this code causes this bug to not happen, and yet, I can't for the life of me see that I'm doing anything wrong. I've accounted for every single step along the way -- not difficult, as it happens extremely early in the program, in a completely innocuous place that uses no stack pointers at all. I've dug down and ferreted out stranger bugs before but this time, not even a gdb memory watch could determine when fd was being modified when it goes haywire -- it just said 'optimized out'... Like it suddenly decides to stop using the variable and just assume it's zero.

It works fine in gcc 4.4.4 with -O2 and -O3, so I suspect it really is that strange and wild thing, a compiler bug. Thanks for the suggestion fpmurphy. I'd better rebuild my system.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bizzare behavior on redirect of stdout

Oracle Linux 5.6 64-bit (derivative of RHEL) Dear Ann Landers, This is about as bizarre as anything I've ever seen. I have a little test script I've been working with. When I redirect stdout to a file, no file. Make a copy of the script to another name. Execute it and redirect stdout, and... (4 Replies)
Discussion started by: edstevens
4 Replies

2. UNIX for Advanced & Expert Users

Bizzare TCP/IP problem

Hi all. I have a really really weird problem that I've been working on for days. The problem manifested as users cannot connect to our web servers via SSH when they're using our wireless network. Here's where it gets weird: - Clients from anywhere other than the wireless subnet can... (4 Replies)
Discussion started by: pileofrogs
4 Replies

3. Shell Programming and Scripting

help with counting processes, bizzare behavior

I have a ksh script (dtksh Version M-12/28/93d on Solaris 10) that is run daily by cron and sometime hangs forever. I need to detect if there is an old copy hung before I start the new run, and if so send an email and exit the script. Here is part of the code: #!/usr/dt/bin/dtksh... (4 Replies)
Discussion started by: 73rdUserID
4 Replies

4. IP Networking

Bizzare network attack?

A server I host is having very rare glitches where a file the user downloads will have incorrect contents. This almost never happens when I am looking, I caught it once and only once -- a user messaged me saying his antivirus had given him a warning about an image file downloaded from his... (2 Replies)
Discussion started by: Corona688
2 Replies

5. Shell Programming and Scripting

sed optimization

I have a process using the following series of sed commands that works pretty well. sed -e 1,1d $file |sed 1i\\"EHLO Broadridge.com" |sed 2i\\"MAIL FROM:${eaddr}"|sed 3i\\"RCPT TO:${eaddr}"|sed 4i\\"DATA"|sed 5s/.FROM/FROM:/|sed 6s/.TO/TO:/|sed 7,7d|sed s/.ENDDATA/./|sed s/.ENDARRAY// >temp/$file... (1 Reply)
Discussion started by: njaiswal
1 Replies

6. Shell Programming and Scripting

AWK optimization

Hello, Do you have any tips on how to optimize the AWK that gets the lines in the log between these XML tags? se2|6|<ns1:accountInfoRequest xmlns:ns1="http://www.123.com/123/ se2|6|etc2"> .... <some other tags> se2|6|</ns1:acc se2|6|ountInfoRequest> The AWK I'm using to get this... (2 Replies)
Discussion started by: majormark
2 Replies

7. Programming

very bizzare file writing problem

I'm trying to write a function which opens a file pointer and writes one of the function parameters into the file, however for some reason Im getting a core dump error. The code is as below void WriteToFile(char *file_name, char *data) { FILE *fptr; /*disk_name_size is a... (10 Replies)
Discussion started by: JamesGoh
10 Replies

8. UNIX for Dummies Questions & Answers

Help on optimization of the script

Hi, I have prepared script which is taking more time to process. find below script and help me with fast optimized script:- cat name.txt | while read line do name=$(echo $line| awk '{print $8}') MatchRecord=$(grep $name abc.txt | grep -v grep ) echo "$line | $MatchRecord" | awk... (2 Replies)
Discussion started by: aju_kup
2 Replies

9. UNIX for Advanced & Expert Users

Bizzare (while statement)

I'm trying to use the while statement to increment a positive number, with a leading "0". when I pass it through, it seems to come out with a negative value, and all the increments remain negative. This is what I have: i=010986294184 j=010986988888 while ; do echo $i i=(($i + 1)) done... (8 Replies)
Discussion started by: Khoomfire
8 Replies
Login or Register to Ask a Question