Jump to content
  • Advertisement
Sign in to follow this  
derek7

odd thing

This topic is 4886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
You're compiling with optimization flags, that remove redundant code.

Share this post


Link to post
Share on other sites
Do you mean "can't" compile? If so it's probably because rand is declared in <cstdlib> not <iostream>. Well at least for me it is, but my header versions are rather old.

Share this post


Link to post
Share on other sites
Quote:
Original post by xor
You're compiling with optimization flags, that remove redundant code.


maybe you wrong,I remove <iostream>,so It can still compile. I use vc2003.net

Share this post


Link to post
Share on other sites
*sigh*

NOT EVERYTHING IS IN IOSTREAM!!!

There's a reason why your include directory has many header files in it. Each of them declares different things.


#include <iostream>
#include <cstdlib>

int main(){

float d = rand()%RAND_MAX;

return 0;}




Normally I'm not one to recommend programming books, but since you are incapable of solving the most simple problems and continue spamming the boards(9 topics in 1 day) I suggest you go buy a beginning C++ book or go to your local library.

Share this post


Link to post
Share on other sites
no ,I mean I do not include any head file ,but the code still pass through the compiler.

Share this post


Link to post
Share on other sites
Well that sounds strange. Mabye it's because you're using .net, most C++ compilers should bitch at you for not including cstdlib. Anyway if it works what is this thread about?

Share this post


Link to post
Share on other sites
For the love of God!!!

Quote:
maybe you wrong,I remove <iostream>,so It can still compile. I use vc2003.net


You compiling an empty main!
The optimization flag activates a method of optimization that consists of removing redundant code, code that isn't going to be needed is removed. So that line " float d = rand()/RAND_MAX;" is going to be removed, you're compiling an empty main. So it doesn't matter what headers you include or exclude, the thing is allways going to compile, because an empty main needs nothing.

Now if you want to check this, just include a "printf("%f\n");" at the end of that code and it won't compile. Why? Because now the variable 'd' is actually used somewhere, it is not redundant, it will not be removed by the optimization.

Share this post


Link to post
Share on other sites


#include <iostream>




main()
{

float d = rand()/RAND_MAX;std::cout<< d;


}



no ,take a look this code .it output 0!!!
I debug ,and I find d exist and value is 0.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!