我有一段代碼,當我寫它以這種形式(與-Wjump-misses-init
標誌),編譯罰款:爲什麼GCC給我-Wjump-misses-init警告?
int some_function(void) {
...
if (is_error)
goto error;
int a;
a = 1;
return a;
error:
return 666;
}
但是,當我寫相同的功能以這種形式,我得到了下面的警告當我編譯(?:
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
爲什麼GCC給我的警告,當我宣佈並在同一行初始化a
似乎有點奇怪我這些例子是荒謬的,但我怕我不能隨意?泄露真實的代碼片段我在Debian Wheezy 7.3上運行GCC 4.7.2。
編輯:無效錯字
在C++中[你不允許通過初始化跳過聲明](http://stackoverflow.com/questions/20963288/c-conditional-storage-allocation-for-object-caused-by-scope/20963326 #20963326)但我無法在C99中找到相同的語句。 –
我不知道錯誤是否適用於不同的優化標誌。或者如果組件不同。也許在一種情況下,操作順序略有不同。這只是一個瘋狂的猜測。 – luk32
另外,'int void'看起來是一個錯字。 –