Author Topic: C: what? "may be used uninitialized" but I have init it. (solved!)  (Read 877 times)

0 Members and 2 Guests are viewing this topic.

Offline squadchannelTopic starter

  • Super Contributor
  • ***
  • Posts: 1252
  • Country: jp
  • deepl translate user
SOLVED!



Code: [Select]
main.c|344|warning: 'term' may be used uninitialized [-Wmaybe-uninitialized]

Why is this happening even though I've init it?

It started happening when I added
Code: [Select]
term = 2;
if(term == 2) goto err0;

if -O3 and -s options aren't specified, it compiles normally. no errors.
"term" is not referenced anywhere other than in the highlighted section.
I'm getting a warning, but it's working fine.
hmmm....? :-//
« Last Edit: May 26, 2026, 09:17:38 am by squadchannel »
 

Offline negativ3

  • Frequent Contributor
  • **
  • Posts: 506
  • Country: th
Re: C: what? "may be used uninitialized" but I have init it.
« Reply #1 on: May 25, 2026, 11:31:59 am »
Code: [Select]
if (term == 2) {
    goto err0;
}
 

Offline squadchannelTopic starter

  • Super Contributor
  • ***
  • Posts: 1252
  • Country: jp
  • deepl translate user
Re: C: what? "may be used uninitialized" but I have init it.
« Reply #2 on: May 25, 2026, 11:36:23 am »
is valid syntax even when written on a single line.
 

Offline Andy Watson

  • Super Contributor
  • ***
  • Posts: 2340
Re: C: what? "may be used uninitialized" but I have init it.
« Reply #3 on: May 25, 2026, 11:42:15 am »
It is saying that there is some path whereby you can arrive at "err1" (line 343) without term being initialised.
More pedantically, the compiler is warning that there may be a path to line 343 that does not include initialising term. As far as I can see, the snippet of code that you shown does not give the source of the "goto err1:" , it appears to be outside the scope of the "int term = 0;"(line 171).
 
The following users thanked this post: squadchannel

Offline negativ3

  • Frequent Contributor
  • **
  • Posts: 506
  • Country: th
Re: C: what? "may be used uninitialized" but I have init it.
« Reply #4 on: May 25, 2026, 11:43:26 am »
term == 2;
This line does nothing (it's a no-op). It compares term with 2, but throws away the result.
 

Offline squadchannelTopic starter

  • Super Contributor
  • ***
  • Posts: 1252
  • Country: jp
  • deepl translate user
Re: C: what? "may be used uninitialized" but I have init it.
« Reply #5 on: May 25, 2026, 11:47:58 am »
It is saying that there is some path whereby you can arrive at "err1" (line 343) without term being initialised.
More pedantically, the compiler is warning that there may be a path to line 343 that does not include initialising term. As far as I can see, the snippet of code that you shown does not give the source of the "goto err1:" , it appears to be outside the scope of the "int term = 0;"(line 171).

Yeah, you're right. Now that I think about it, absolutely right. :-+
optimization are doing a better job than I expected.

term == 2;
This line does nothing (it's a no-op). It compares term with 2, but throws away the result.
What do you mean? valid syntax in GCC.
 

Offline negativ3

  • Frequent Contributor
  • **
  • Posts: 506
  • Country: th
Re: C: what? "may be used uninitialized" but I have init it.
« Reply #6 on: May 25, 2026, 11:52:15 am »
It is saying that there is some path whereby you can arrive at "err1" (line 343) without term being initialised.
More pedantically, the compiler is warning that there may be a path to line 343 that does not include initialising term. As far as I can see, the snippet of code that you shown does not give the source of the "goto err1:" , it appears to be outside the scope of the "int term = 0;"(line 171).

Yeah, you're right. Now that I think about it, absolutely right. :-+
optimization are doing a better job than I expected.

term == 2;
This line does nothing (it's a no-op). It compares term with 2, but throws away the result.
What do you mean? valid syntax in GCC.

It's like asking your friend "Do you want ice cream?" and then never giving them any or doing anything about it.
 

Offline squadchannelTopic starter

  • Super Contributor
  • ***
  • Posts: 1252
  • Country: jp
  • deepl translate user
Re: C: what? "may be used uninitialized" but I have init it. (solved!)
« Reply #7 on: May 25, 2026, 11:55:28 am »
I resolved the issue by moving term above err1.

When I have to terminate the program due to an error midway through, I wonder how I should free the declarated handles up to that point...
I could use a goto and jump to the end, but that would make it impossible to use VLAs, so it’s a bit of a dilemma.
grouping the handles into a struct might be a good approach.
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3909
  • Country: it
Re: C: what? "may be used uninitialized" but I have init it.
« Reply #8 on: May 26, 2026, 07:14:06 am »
What do you mean?

what do you think "term == 2;" does?
 

Offline squadchannelTopic starter

  • Super Contributor
  • ***
  • Posts: 1252
  • Country: jp
  • deepl translate user
Re: C: what? "may be used uninitialized" but I have init it. (solved!)
« Reply #9 on: May 26, 2026, 07:17:46 am »
evaluate the expression. do nothing.


Oh, maybe you mean this?
It's a typo. |O

Quote
It started happening when I added
Code: [Select]
term == 2;
if(term == 2) goto err0;
« Last Edit: May 26, 2026, 07:23:58 am by squadchannel »
 

Online gf

  • Super Contributor
  • ***
  • Posts: 1826
  • Country: de
Re: C: what? "may be used uninitialized" but I have init it. (solved!)
« Reply #10 on: May 26, 2026, 08:55:28 am »
Are there any goto err1, err2 or err3 before line 171 ?
 

Offline squadchannelTopic starter

  • Super Contributor
  • ***
  • Posts: 1252
  • Country: jp
  • deepl translate user
Re: C: what? "may be used uninitialized" but I have init it. (solved!)
« Reply #11 on: May 26, 2026, 09:16:46 am »
Yes, that exists, and I resolved it by moving the term declaration before err1.
 

Online TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 190
  • Country: us
Re: C: what? "may be used uninitialized" but I have init it. (solved!)
« Reply #12 on: May 27, 2026, 01:58:27 pm »
It's a typo. |O

"Typo" where? In this forum? Or in the actual code?

And why is it no longer present in the post? Did you actually edit the original post???
« Last Edit: May 27, 2026, 02:00:59 pm by TheCalligrapher »
 
The following users thanked this post: newbrain

Offline squadchannelTopic starter

  • Super Contributor
  • ***
  • Posts: 1252
  • Country: jp
  • deepl translate user
Re: C: what? "may be used uninitialized" but I have init it. (solved!)
« Reply #13 on: May 27, 2026, 02:18:42 pm »
Yes, I made the edits and mentioned the changes.
I’ve resolved the issue, so you don’t need to waste any more of your time on this. :-+
 

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 11140
  • Country: fi
Re: C: what? "may be used uninitialized" but I have init it. (solved!)
« Reply #14 on: May 27, 2026, 06:52:19 pm »
Yes, I made the edits and mentioned the changes.
I’ve resolved the issue, so you don’t need to waste any more of your time on this. :-+

If I may suggest for the future:

1) Reduce the code to minimum what demonstrates the problem. Usually you find the problem yourself while doing this,
2) Then post the whole code
(1 and 2 are important combined - if you don't reduce to minimum, posting it all is too much. But not posting it all leads to the problem being outside of what you posted, hence useless.)
3) Post it as text, e.g. using the code tag, not as screen capture
4) Feel free to edit to fix typos in your message, not the code. By all means edit to add ancillary information - but the code is important as-is.
 
The following users thanked this post: newbrain


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf