gcc: warning: will never be executed


 
Thread Tools Search this Thread
Top Forums Programming gcc: warning: will never be executed
# 1  
Old 12-12-2009
gcc: warning: will never be executed

I get this gcc warning, but the function works correctly. Any ideas?

Quote:
gcc -std=c99 -pedantic -O3 -Wall -Wunreachable-code a.c
Quote:
a.c:13: warning: will never be executed
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
    char A[50] = "";
    strcat(A, "Hello World!");

    int COUNT = 0;
    while(A[COUNT])
    {
        A[COUNT] = (char)(tolower(A[COUNT]));
        COUNT++;
    }
    puts(A); // output: hello world!
    return 0;
}


Last edited by limmer; 12-12-2009 at 12:09 PM..
# 2  
Old 12-12-2009
What version of gcc are you using and on what platform? I do not get this warning message with either gcc 3.3 or gcc 4.2.
# 3  
Old 12-12-2009
gcc (Debian 4.3.4-6) 4.3.4
# 4  
Old 12-12-2009
Try moving the init of COUNT to above the strcat. The compiler may be confused....
# 5  
Old 12-12-2009
Quote:
Originally Posted by jp2542a
Try moving the init of COUNT to above the strcat. The compiler may be confused....
Tried, but still the same warning.

I tried with gcc-4.4 (Debian 4.4.2-3) 4.4.2 and get the same error but this time repeated:

Quote:
a.c: In function ‘main’:
a.c:13: warning: will never be executed
a.c:13: warning: will never be executed
a.c:13: warning: will never be executed
a.c:13: warning: will never be executed
a.c:13: warning: will never be executed
a.c:13: warning: will never be executed


---------- Post updated at 02:05 PM ---------- Previous update was at 01:43 PM ----------

Got it, it is the optimize cflag.

Bug?
# 6  
Old 12-12-2009
Could be that since you're not working on a variable, but a fixed string the compiler optimizes away the loop and the tolower() call. Since you might expect to see the function called (eg. for profiling purposes) it warns you that there'll be no such call.
# 7  
Old 12-12-2009
The warning looks correct to me.

Given char A[50] = ""; and count=0;, the compiler can put two and two together and realize that A[count] is A[0] is the first character of the empty string "", which is NULL, therefore, the code inside the loop will never execute.

[edit] I missed the strcat. And so is the compiler. Must be a bug. It is a variable, but it's initialized from a fixed string, so the optimizer may be treating it as such.

Last edited by Corona688; 12-12-2009 at 06:44 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script is not getting executed

Hi All, I have written a programme where i need to work in one session but when that session get executed it doesnt execute other programme till I write exit. Below is the script #!/bin/bash echo 'Going to start' exectrusteduser.com sh.com cd /UAT_Logs/CDCI_LOGS/SEEDADM pwd echo... (6 Replies)
Discussion started by: ripudaman.singh
6 Replies

2. Shell Programming and Scripting

If condition - else is never executed

In the following code snippet, 'if' part is always executed irrespective of whether I enter 'y' or 'n'. I am unable to figure out my mistake even after spending couple of hours. Can someone point out the mistake? Thank you! echo "Do you want to delete?(y/n):" read USERINPUT echo $USERINPUT ... (2 Replies)
Discussion started by: Madhus
2 Replies

3. Shell Programming and Scripting

Command is executed twice

Hi I try to run a command using bash script. The script should now look like this: #!/bin/bash case "$1" in start) sudo su - cispmgm -c "/usr/local/jdk/bin/java -Dworking.dir=/opt/ibm/cisp -Denvironment.target= -Xms512M -Xmx1024M -classpath... (3 Replies)
Discussion started by: nikolai.straess
3 Replies

4. AIX

Script not getting executed via cron but executes when executed manually.

Hi Script not getting executed via cron but executes successfully when executed manually. Please assist cbspsap01(appuser) /app/scripts > cat restart.sh #!/bin/ksh cd /app/bin date >>logfile.out echo "Restart has been started....." >>logfile.out date >>logfile.out initfnsw -y restart... (3 Replies)
Discussion started by: samsungsamsung
3 Replies

5. Shell Programming and Scripting

How command is executed

as i have been new to unix can any one can tell,when be type a command on unix shell what processing it goes through before execution of it (1 Reply)
Discussion started by: shashank1311
1 Replies

6. Programming

help with gcc warning

I get the following gcc warning. I do not see a double being declared anywhere... how can I fix this? (This code is just an example, I realize that the first if bracket will never be executed.) #include <stdio.h> int main(void) { float A = 0.0; int B = 150; if(B == 0) ... (1 Reply)
Discussion started by: limmer
1 Replies

7. Solaris

Installing gcc - recieve error message gcc : cannot execute

AIM- Install Oracle 11g on Solaris using VMWare Steps 1.Logged on as root 2.Created subfolders à /usr/local/bin & /usr/local/bin/gcc 3.Downloaded gcc & libiconv & unzipped them on my harddrive & burnt them on CD 4.Copied files from CD to /usr/local/bin/gcc 5.Terminal (root) à pkgadd -d... (8 Replies)
Discussion started by: Ackers
8 Replies

8. Solaris

which processes executed first

When we login to a solaris terminal then which processes executed first? I beleive that first system profile file and user .profile gets executed. So please let me know what happens exactly. The question may like this that which rocesses arre executed first while loging into unix server? (1 Reply)
Discussion started by: surjyap
1 Replies

9. Programming

warning from gcc

I get this error warning. test.c: In function 'main': test.c:5: warning: incompatible implicit declaration of built-in function 'memset' After compiling this code #include <stdio.h> int main() { char pBuffer; memset(pBuffer, 0, 32); return 0; } What seems... (2 Replies)
Discussion started by: sledge76
2 Replies

10. Programming

gcc compiler warning

The gcc compiler is giving following error in my code. Can Anyone help, why is this warning popping up. "void format, different type arg (arg 1)" The function's on which warning is given, looks like this: void fprint_stderr_sockarray(int* sockets) { .......... .......... } (2 Replies)
Discussion started by: Ahsan
2 Replies
Login or Register to Ask a Question