# double to int impossible

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

## 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 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 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 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 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 vectorvRet.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 on other sites
Did you copy and paste this code exactly? Because this...

double pD = &dV;

should not work. In fact, this shouldn't even compile.

##### 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 on other sites
Quote:
 Original post by Jason DoucetteI 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 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 on other sites
&dV is just an int that is the address of dV. So you are just assigning the address and not the value.

theTroll

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631431
• Total Posts
3000040
×