Sign in to follow this  
Thaligar

double to int impossible

Recommended Posts

ok, now i'm getting really crazy, i'm using msvc 2003 net, until now i never had a problem with a double to int conversion, but now it doesn't work anymore. my code looks like that (i know it have to work but it doesn't)
double dValue = x.GetSize(); // returns the size of the object
int nRet = dValue;

during debugging the assignment from GetSize to dValue works, but nRet gets to 1223296 when the size is 1.907, i also tried to cast, but still doesn't work. the other way around it works. very interesting is the fact that even the double has the same value the int changes the assigned value over time. is there any reason, or shall i try something else?? thanks in advance, tgar

Share this post


Link to post
Share on other sites
Are you debugging a release build? Are you sure you don't have a buffer overrun or similar error that causes a non-local change to the variable?

Share this post


Link to post
Share on other sites
no i'm debugging a debug build, all other variables are ok,
i also tried now this one


double dV = 3.14;
int nG = dV;



same error, i also restarted my system,
i checked now also these value and got also pointer to the values,
now the really interesting part,

the debugger says -> dV = 3.14
the memory location says -> dV = 1.26+011
so both are VERY different from 1223856

if you have any suggestions, maybe msvc has several bugs...

tgar

Share this post


Link to post
Share on other sites
It's much more likely that your code has bugs. Try posting a minimal, but complete code sample that demonstrates your problem.

Share this post


Link to post
Share on other sites
ok,


std::vector<int> Compute(double dMinSize, double dMaxSize, double dStability, int nStepSize, bool bNeg)
{
//LOG(dMinSize);

double dV = 3.14;
double pD = &dV;
int nG = dV;
int * pN = &nG;

std::vector<int> vRet;
//vRet.push_back( ... ); // here i wanted to compute something where the return value is a double, and then i add it to the int vector
vRet.push_back( 0 );
return ( vRet );
};




the rest is commented, not even this part works,

i tried this code snippet in another program where it worked.

i will try the code in the caller function,
i'll also try to recompile the whole program and then i'll restart my computer, this will take some time but hopefully will work...

greets,
tgar

Share this post


Link to post
Share on other sites
I believe it will compile in C. If the source file is named *.c instead of *.cpp, I believe MSVC++ will compile it as C, and thus allow meaningless conversions as such, with no warnings. (Thank God C++ fixed those issues!)

Share this post


Link to post
Share on other sites
Quote:
Original post by Jason Doucette
I believe it will compile in C.

C doesn't have std::vector, or even the components required to make it (namespaces, classes, templates).

Thaligar: Make copy and paste your new best friends when it comes to posting code to forums online. "Transcribing" code like this leaves you with fake code that's worse than useless -- it tends to cover up the actual problems you're facing, and worse, generate new problems, causing wild goose chases.

Share this post


Link to post
Share on other sites
I was just referring to that single line of code, which may compile in C on it's own. But, maybe not, since they might have to be the same size in order for it to be legal.

But, you're right, the rest of the C++ stuff would make it impossible to compile in C.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheTroll
&dV is just an int that is the address of dV.


I don't mean to be pedantic, but that's not the case on most 64-bit platforms. I'm sure you know this and that you're just being general here, but I just want to prevent any noobies from getting too comfortable with things that can and will change in the (near) future. Otherwise they rely on them...and you know the rest.

Share this post


Link to post
Share on other sites
doh, sorry,

no, it was meant this way


double * pD = &dV;



even after recompilation the code doesn't work, i have to investigate the caller function,

greets
tgar

Share this post


Link to post
Share on other sites
doh, sorry,

no, it was meant this way (i typed it again, no copy & paste)


double * pD = &dV;



even after recompilation the code doesn't work, i have to investigate the caller function,

greets
tgar

Share this post


Link to post
Share on other sites
very nice,

i tried this code snippet all in my code, even after the call to WinMain and it fails, the rest of the code is working as expected, i will try to compile it on a second machine and check if i can reproduce it.

greets,
tgar

Share this post


Link to post
Share on other sites
First off, it's pretty much useless to type out code when you're asking for help. Always copy and paste.

Secondly, a minimal, but complete, code sample that demonstrates your problem has three characteristics. 1) It should contain everything necessary to reproduce your problem. 2) You can't remove anything from the code sample and still have it demonstrate your problem. 3) It should actually demonstrate your problem.

A code sample that fails to compile will fail all three points unless you're asking about a compiler error.

If you can't think of a way to generate such a code sample from scratch, one approach is to make a copy of your project and start deleting things until the code no longer shows the problem.

Share this post


Link to post
Share on other sites
thanks to all of you,

(this with the copy'n'paste i will remember the next time)

nevertheless, i found the "bug", and SiCrane it is not MY code which is buggy ;)

i compiled the same code on another machine without ANY bugs. the error was that there is a small problem in using

vista -> virtualpc -> winxp sp2 and vs2003

somewhere between these programs there is an error, a really heavy bug.

maybe it's only occuring once in a century, but i got it.

nevertheless, i can program again and my code acts like expected without any foreign bugs,

greets to all,
thanks again
tgar

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