Reputation: 557
Can someone please tell me if it is acceptable to use same goto labels in different functions in the same C file?
To explain what I am facing:
function1()
{
...
goto label
...
label:
...
}
function2()
{
...
goto label;
...
label:
...
}
Now whats happening is that the code after the label is being used for cleanup of malloc'ed' data. And the whole thing is crashing in function2. I printed out the mem. locations which are being free and the same are being freed multiple times. Is this because of all the gotos? Is this valid use of the goto statement?
Upvotes: 26
Views: 9871
Reputation: 21595
As Joachim said here, labels are local; but note that labels are local to functions - not to the current block. goto
statements do not respect scoping, except for whole-function scoping.
Upvotes: 13
Reputation: 409176
Labels are local, so you can use the same label in multiple functions.
The question about if you should use goto
is a different matter though, and one that is not easily answered. In short, don't use goto
. But as with everything (especially when it comes to programming) there are exceptions where goto
may be useful.
Upvotes: 28