Sign in to follow this  
derek7

odd thing

Recommended Posts

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
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
Quote:
Original post by xor
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.
That can't be it. Without knowing the definition of rand because the necessary header is not included, it cannot know that rand() does not have side-effects. Therefore it can not be optimised out.
And for that matter, it doesn't even know that rand() is a function, and should not be able to compile that code.
Perhaps he messed with the iostream header?
Quote:
Why do people still respond seriously to these posts? This guy just likes to post junk posts.
Anything that he posts may catch the eyes of some other beginner, who with proper guidance, can learn from his mistakes.

Share this post


Link to post
Share on other sites

#include <stdlib.h>

int main()
{
printf("Initial value of RAND_MAX is %d. Considered this rand() give me %d.\n"
"Is obviously that the value divided by %d (converted also to a float) give me a number ~0.\n"
"I can try to write a little log function like this cause the program is only 2 lines.\n"
"And there are not many technical difficulties doing this.", RAND_MAX, rand(), RAND_MAX);
return 0;
}

Share this post


Link to post
Share on other sites
There seems to be some kind of mess with iostream there, rand is defined in stdlib.h, maybe it's being included somewhere else.

Anyway iMalc, OP says(i think) that he can compile it when he removes iostream and without cout, so the remaining line is being ignored. There is a rand() there and a definition that aren't included anywhere, and yet it compiles, so they must be ignored. Or maybe some sort of script is running at compile time that's including more stuff, and you're right after all.

Either way, this is boring, it's probrably a PEBKAC error anyway, so i'm off.

[Edited by - xor on June 3, 2005 2:04:45 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by xor
There seems to be some kind of mess with iostream there, rand is defined in stdlib.h, maybe it's being included somewhere else.


If you're using C++, you'd want to use cstdlib instead of stdlib.h . You'd also want to seed rand(). I haven't compiled it myself, but you might want to try the following:

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
srand(static_cast<unsigned>(time(0))); // there are many different ways to do this
float d = rand()/RAND_MAX;
std::cout << "RAND_MAX: " << RAND_MAX << std::endl;
std::cout << "d: " << d << std::endl;
std::cin.get();
return 0;
}


Share this post


Link to post
Share on other sites
Why it compiles?

Have a look into the project-properties under c/c++, precompiled headers. If the setting is something like "use precompiled headers" and not "auto generate precompiled headers" than thats why it compiles.

If the headers are not auto-generated, than adding/removing include-files will not affekt the precompiled headers, so, as in this case, the removed include-files are still in the precomiled header and are still beeing used.

Try to clean-compile or set the header option to auto-generate.

Oh, and rtfm...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this