Just curious, is there any difference at all, or are both represented the same when put into asm?
Just curious, is there any difference at all, or are both represented the same when put into asm?
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Why not compile and see for yourself? It'll be compiler dependant, even down to the optimisation settings you use.
There's another thread going this way on the C forum.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I don't think I've ever seen for(;;) in a real program... only in books. Not that I've studied that many real programs!
To me, while(true) just looks better!
I personally only use while(true), but I wanted to know in case of an argument with my teacher or something. Knowledge is power
Hammer, when you say compile and see for myself, do you mean run it in a profiler or do you mean find some way to look at the assembly myself? And I'll check out that thread you're talking about
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
I've seen this used with looping thru a menu for example
sort of like :
That way the break statements gets you out of the infinite loop &Code:for (; ; ) ... { int choice = menu( ); switch(choice) { case ( 1 ): some function( ); break; case ( 2 ): some other function(); break; etc. etc. etc.
you can go thru as many case statements as you want....
If you had quite a few choices "while (true)" would lead to a lengthy "If statement"
the infinite for(; ; ) loop lets you use a switch statement instead..
Using something like MSVC you can look at the ASM while debugging. There are many compiler products that offer this feature.Hammer, when you say compile and see for myself, do you mean run it in a profiler or do you mean find some way to look at the assembly myself? And I'll check out that thread you're talking about
What? You mean you can't put a switch statement inside a while loop? I'm not so sure about that, but I'll test it out... And also are you sure break will break out of both? I thought it would only break out of the switch, and if it DID break out of both, I would think that it would act in the same way with the whilethe infinite for(; ; ) loop lets you use a switch statement instead..
Ok, thanks. But if I try compiling on Release settings, I won't have the debugger anymore. Is there some easy way to get the assembly from non-debug-mode executables, or will I need to get a decompiler of some sort?Using something like MSVC you can look at the ASM while debugging. There are many compiler products that offer this feature.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Ummm... there are some apps that will convert the binary to ASM, since the conversion is rather easy. I don't know the names of any... some sort of hex editor/"disassembler" though
Away.
You know what, so does a while loop.the infinite for(; ; ) loop lets you use a switch statement instead..
Hopefully your compiler would optimize the two to the same thing.
While(1) because im lazy and dont want to type true. When i read it outloud i always say "Loop Forever". I dont think there should be any major difference between while(1) and for(;. true is just an enumeration of 1 isnt it? A literal 1? or am i mistaken...
Ok, I was hoping it would be that. But just in case, you know... like, if doing the while might require a check of some sort while the for would just loop without checking anything, maybe the for would be marginally more efficient or something. I'm about to grab a disassembler from somewhere though, I guess I'll have to see if there's any difference myself (even though I won't have a clue what's going on in the assembly )Hopefully your compiler would optimize the two to the same thing.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Well yes you could you a switch statement in a while loop, but somebody said they had never seen a for(; ; ) loop used in a real program and someone wanted to see it used so they could discuss it with their teacher.....Actually with either it depends on how you code it.......Here is the full code with the snippet I posted earlier....A fellow over at source-forge wrote this for me..
The purpose of the example was to put as many examples of C++ code in one short snippet..... and I think he gave me 5 stars worth...........classes, reference, for(; ; ), whatever..it's in here...great learning example.........
Heh !Code:// First, notice that the main() calls the function 'OperationWindow( )' // that calls the 'menu()' and used in the switch within the 'OperationWindow( )' // function. // Note when 'n' is the choice for quit ?... the switch "re-calls" the // function 'OperationWindow( )' to start a new. // Also in main(), a pointer to mainClass 'mainClass *openNewMain = new mainClass ;' // and the using 'new' sets aside the space in memory. // Last after the program ends, the 'delete openNewMain ; openNewMain = 0 ;' // frees the memory... and then sets 'openNewMain = 0 ' so not to leave trash // in memory. // Notice that no 'system("PAUSE")' has been added to the end of main(). // ******************************************************************** // This may not be world class programming, but I put a number of basic // things to give an idea or two maybe. #include<iostream> class mainClass { public: mainClass( ){} ~mainClass( ){} int OperationWindow( ); void saldot ( ); void goback ( ); void endcall ( ); protected: int menu( ); }; int mainClass::OperationWindow( ) { bool nul_operation = false; for (;;) { int choice = menu( ); switch(choice) { case ( 1 ): saldot( ); break; case ( 4 ): nul_operation = true; std::cout << "Quit Y-OR-N ? "; char con; std::cin >> con; if(con == 'y' || con == 'Y'){} else OperationWindow( ); break; default: goback( ); break; } if ( nul_operation ) break; } } int mainClass::menu( ) { int choice; std::cout << "\n mainClass Demo\n"; std::cout << "(Enter (1)> The only Choice !.\n"; std::cout << "(Enter (4)> Quit.\n\n"; std::cout << "|Here> "; std::cin >> choice; return choice; OperationWindow( ); } void mainClass::saldot( ) { system("CLS"); std::cout <<"\n What ? ... Press Any Key to continue"<<"\n"; system("PAUSE > NUL"); } void mainClass::goback( ) { system("CLS"); std::cout <<"\nNot a selection... try again ... Press Any Key to continue"<<"\n"; system("PAUSE > NUL"); } void mainClass::endcall( ) { system("CLS"); std::cout <<"\n!!! A Request to close this program has been made ... close ?"<<"\n"; system("PAUSE > NUL"); } int main( ) { mainClass *openNewMain = new mainClass ; openNewMain->OperationWindow( ); delete openNewMain ; openNewMain = 0 ; return 0; } //j@ck_
This all started over How to make a counsel app stay up without using system("pause") or getch() or cin.get() etc.
Mm, well, I wasn't asking to see an example of for(;; ) in code, I was asking if there was any difference between it and while(1) () when compiled (with full optimizations I meant, although I didn't say so). Anyways, I don't see where the "reference" is in that code, or for that matter, why a pointer to a mainClass is used instead of a simple object of it. And besides, I don't agree with this:
It's only setting a pointer which will never be used again anyways to NULL, the delete line is what got rid of the trash in memory. I appreciate your good intent thoughand then sets 'openNewMain = 0 ' so not to leave trash in memory.
**EDIT**
Ok, I give up with the disassemblers. Both of the 2 that I tried had problems; the first was missing some debugging DLL, and the second was a trial version and said something about the test-file not being a valid hex file or something... I thought a hex file is just a normal text/binary/whatever file that you've opened in a hex editor or something??
Last edited by Hunter2; 09-04-2003 at 10:02 PM.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
You should set it to NULL not 0. The only thing that this actually does is prevents you from accidently dereferencing a pointer that isn't pointing anywhere. The compiler should pick up the error.and then sets 'openNewMain = 0 ' so not to leave trash in memory.
It is also worth reading this link.