Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GCC give me the -Wjump-misses-init warning?

I have a piece of code that compiles fine when I write it in this form (with the -Wjump-misses-init flag):

int some_function(void) {

        ...

        if (is_error)
                goto error;

        int a;

        a = 1;

        return a;

error:
        return 666;
}

But when I write the same function in this form I get the below warning when I compile (:

int some_function(void) {

        ...

        if (is_error)
                goto error;

        int a = 1;

        return a;

error:
        return 666;
}

test.c: In function 'some_function':
test.c:15:17: warning: jump skips variable initialization [-Wjump-misses-init]
test.c:21:1: note: label 'error' defined here
test.c:17:13: note: 'a' declared here

Why does GCC give me that warning when I declare and initialize a on the same line? Seems a bit odd to me? These examples are nonsensical but I'm afraid I'm not at liberty to divulge the real code snippet. I am running GCC 4.7.2 on Debian Wheezy 7.3.

EDIT: void typo

like image 772
c00kiemonster Avatar asked Jan 08 '14 14:01

c00kiemonster


People also ask

How do I stop a GCC warning?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.

What is GCC warning?

Warnings are diagnostic messages that report constructions that are not inherently erroneous but that are risky or suggest there may have been an error. The following language-independent options do not enable specific warnings but control the kinds of diagnostics produced by GCC.

How do I remove compiler warnings?

To turn off the warning for a specific line of code, use the warning pragma, #pragma warning(suppress : 4996) .

What are GCC errors?

Errors report problems that make it impossible to compile your program. GCC reports errors with the source file name and line number where the problem is apparent. Warnings report other unusual conditions in your code that may indicate a problem, although compilation can (and does) proceed.


1 Answers

In C++ you are not allowed to bypasses declarations with initialization but it seems to be allowed in C99. You can have gcc warn you about if you either use -Wjump-misses-init or -Wc++-compat. This is covered in the gcc docs section Options to Request or Suppress Warnings and says:

Warn if a goto statement or a switch statement jumps forward across the initialization of a variable, or jumps backward to a label after the variable has been initialized. This only warns about variables that are initialized when they are declared. This warning is only supported for C and Objective-C; in C++ this sort of branch is an error in any case.

-Wjump-misses-init is included in -Wc++-compat. It can be disabled with the -Wno-jump-misses-init option.

Note, this also applies to declarations inside a switch statement. One way to work around this is to create a new scope using {}.

In the Annex I the draft C99 standard suggests this as warning, it says:

An implementation may generate warnings in many situations, none of which are specified as part of this International Standard. The following are a few of the more common situations.

and includes the following bullet:

A block with initialization of an object that has automatic storage duration is jumped into (6.2.4).

like image 178
Shafik Yaghmour Avatar answered Sep 29 '22 06:09

Shafik Yaghmour