Breaking out of loops with return?
I''m just wondering about something; how safe is it to exit a function when you''re still in an iteration? Does it mess up any loose ends that break; or continue; normally tie up?
It's safe unless you're doing something that would make it unsafe. For example, this is safe:
But this wouldn't be safe:
Of course, in this situation, we could just use break. If we can't use break (because of nesting of some sort), and you don't want to use a goto, then you'd have to make sure to free 'magic' before returning 'a'.
[edited by - Null and Void on August 5, 2002 3:06:19 PM]
int func(int param) { static int magic[ ... ]; int a; for(a=0; a<10; ++a) { if(magic[a] == param) return a; } return -1;}
But this wouldn't be safe:
int func(int param) { int *magic; int a; magic = (int *) malloc(sizeof(int) * fish); /* ... */ for(a=0; a<10; ++a) { if(magic[a] == param) return a; /* would leak memory */ } free(magic); return -1;}
Of course, in this situation, we could just use break. If we can't use break (because of nesting of some sort), and you don't want to use a goto, then you'd have to make sure to free 'magic' before returning 'a'.
[edited by - Null and Void on August 5, 2002 3:06:19 PM]
Okay, thanks. I realise that memory has to be freed and COM objects need to be released and such, but I was just wondering if the iteration would somehow get messed up, leaving things running in the background, or not making things go out of scope, or something. Guess not, which is good.
Thanks a lot!
Thanks a lot!
You can also "return" out of a function that has void return type. Like so:
void foo()
{
while(1)
return;
}
"Break" and "continue" are helpful sometimes.
void foo()
{
while(1)
return;
}
"Break" and "continue" are helpful sometimes.
Some people think that functions should have a single exit point.
"Imagine the people who believe such things and who are not ashamed to ignore, totally, all the patient findings of thinking minds through all the centuries since the Bible was written. And it is these ignorant people, the most uneducated, the most unimaginative, the most unthinking among us, who would make themselves the guides and leaders of us all; who would force their feeble and childish beliefs on us; who would invade our schools and libraries and homes."
Isaac Asimov
"Imagine the people who believe such things and who are not ashamed to ignore, totally, all the patient findings of thinking minds through all the centuries since the Bible was written. And it is these ignorant people, the most uneducated, the most unimaginative, the most unthinking among us, who would make themselves the guides and leaders of us all; who would force their feeble and childish beliefs on us; who would invade our schools and libraries and homes."
Isaac Asimov
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement