Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C++ application giving a wrong result (Visual C++)

c++ application dll visual studio

Best Answer AceInfinity , 05 October 2014 - 12:40 PM

A C cast is less safe than using a pre-defined C++ style cast, however understand that a C cast "chooses" the best cast by assumption based on what you're trying to cast. You definitely do not understand these principles and it seems like you are guessing more than anything... That's okay, but it won't get you very far with C++ because this is a language where you really need to understand how your code works and how the language works in order to be successful; much less forgiving than most other languages. I'd suggest looking up some online documentation for casts, perhaps some tutorials on pointers & reference, just to start. :)

Additionally, Instead of using a pointer for 'Infos' from GetInfos(), why not a reference? It would help you avoid having to dereference each time you want to change the value.

It looks like (even though it's a bad attempt) that you're still trying to do the improper thing and setting the address of 'Infos' to the version, based on what I gather from what you think your code should be doing, and what I'd assume you WANT to do.
 

*Infos = MAJOR_VERSION;
*Infos = MINOR_VERSION;
Infos is a pointer type, you need to dereference it to change it's value, otherwise you'll be changing the pointer/address itself and not what it points to. You probably naively used reinterpret_cast due to compiler errors you were getting about not being able to assign to the variables due to pointer and int conflicts.

My suggestion would be to not do this in the future and understand what you're trying to do and why those compiler warnings and errors are displaying after you attempt to compile. Mainly because even if you managed to get a hacky cast to work in a certain case, it doesn't mean that the program at runtime will demonstrate defined behavior, nor does it mean that it'll work as you expect it to.

Aside from that, I don't quite understand the point of this due to the fact that these are pre-defined by you:
unsigned const MAJOR_VERSION = 1;
unsigned const MINOR_VERSION = 0;
I can't see these being meaningful? :S And there's a signdedness factor here from unsigned to a signed integer as well.

There are better ways to accomodate version updates than this. :) Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#13 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts

Posted 01 October 2014 - 12:58 PM

Any reasons why not take 2nd parameter of GetInfos method by reference? Also, as gregwarner pointed out, reinterpret_cast is bad, unless you really really know what you're doing.

 

Both MAJOR_VERSION and MINOR_VERSION are ints, and second parameter of GetInfos is int as well, so there's no need for any cast.


The roots of education are bitter, but the fruit is sweet.


#14 zohratl

zohratl

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 03 October 2014 - 03:31 AM

But visual studio wouldn't accept a direct assignment! It requiers the use of reinterpret_cast or any C cast!



#15 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 03 October 2014 - 05:32 AM

Did you read my previous post about static_cast?

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#16 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts

Posted 05 October 2014 - 12:40 PM   Best Answer

A C cast is less safe than using a pre-defined C++ style cast, however understand that a C cast "chooses" the best cast by assumption based on what you're trying to cast. You definitely do not understand these principles and it seems like you are guessing more than anything... That's okay, but it won't get you very far with C++ because this is a language where you really need to understand how your code works and how the language works in order to be successful; much less forgiving than most other languages. I'd suggest looking up some online documentation for casts, perhaps some tutorials on pointers & reference, just to start. :)

Additionally, Instead of using a pointer for 'Infos' from GetInfos(), why not a reference? It would help you avoid having to dereference each time you want to change the value.

It looks like (even though it's a bad attempt) that you're still trying to do the improper thing and setting the address of 'Infos' to the version, based on what I gather from what you think your code should be doing, and what I'd assume you WANT to do.
 
*Infos = MAJOR_VERSION;
*Infos = MINOR_VERSION;
Infos is a pointer type, you need to dereference it to change it's value, otherwise you'll be changing the pointer/address itself and not what it points to. You probably naively used reinterpret_cast due to compiler errors you were getting about not being able to assign to the variables due to pointer and int conflicts.

My suggestion would be to not do this in the future and understand what you're trying to do and why those compiler warnings and errors are displaying after you attempt to compile. Mainly because even if you managed to get a hacky cast to work in a certain case, it doesn't mean that the program at runtime will demonstrate defined behavior, nor does it mean that it'll work as you expect it to.

Aside from that, I don't quite understand the point of this due to the fact that these are pre-defined by you:
unsigned const MAJOR_VERSION = 1;
unsigned const MINOR_VERSION = 0;
I can't see these being meaningful? :S And there's a signdedness factor here from unsigned to a signed integer as well.

There are better ways to accomodate version updates than this. :)

Edited by AceInfinity, 05 October 2014 - 01:26 PM.

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image


#17 zohratl

zohratl

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 06 October 2014 - 12:02 AM

Thank you AceInfinity, such a great answer. Indeed, I am not a C++ programmer, and it's my first time using it, I am a hardware designer actually, and this work was assigned  to me and I have to deliver it even though it's not my cup of tea! Anyway, the version file is to be changed manually when the programmer updates the DLL I am trying to create. It was specified by my superiors actually! I used the cast operator due to a compiler error as you guessed. I'll try to read about references and implement it and come back with the result!






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download