Advertisement

vb vs c++ (easy q, not a debate)

Started by September 20, 2000 11:13 AM
1 comment, last by ape 24 years, 3 months ago
I just got a bug in vb, and don''t think I would have gotten it in c++, but I dont'' have a compiler near to test it, so you folks can help out: in pseudo-c++-code int myInt; myInt = 32000; void mySubroutine( float myFloat ) { //whatever } // here''s the problem // when calling my subroutine, I''d have: mySubroutine(myInt + 1000) --- Now vb tells me that I''ve over-flowed the int myInt, but actually I''m not ever assinging myInt any illegle value. The value 33000 only exists in mySubroutine, right? Would c++ allow this to pass?
C++ would allow that to pass, but C++ doesn''t care if you overflow your vars. On a 16 bit plattform your float would have the value (33000 - MAX_INT), on 32 bit plattforms it would have the value 33000 (sizeof(int) == 4).

You suffer the overflow because you missed something. What the compiler does is:
1) create a copy of myInt (of type int)
2) increase this copy by 1000 (there is the overflow)
3) convert the copy to a float
4) this float will be the parameter
Advertisement
There''s probaby something going on with the conversion. First, convert the value to a double by using CDouble( myInt + 1000 ) or, have the sub/function catch the value using the byValue message as opposed to byRef.

This topic is closed to new replies.

Advertisement