Go Back   EQEmulator Home > EQEmulator Forums > Development > Development::Development

Development::Development Forum for development topics and for those interested in EQEMu development. (Not a support forum)

Reply
 
Thread Tools Display Modes
  #1  
Old 06-30-2009, 06:51 PM
Kobaz
Hill Giant
 
Join Date: Nov 2008
Location: Gold Coast, Oz
Posts: 119
Default Need a C++ guru

When comparing an int with an unsigned int, are both promoted to a long by the compiler?
Reply With Quote
  #2  
Old 06-30-2009, 07:24 PM
Shendare
Dragon
 
Join Date: Apr 2009
Location: California
Posts: 814
Default

According to the assembly generated, my Visual C++ 2008 appears to treat both as unsigned ints.

Different compilers may implement such things in their own way, however.
Reply With Quote
  #3  
Old 06-30-2009, 07:32 PM
KLS
Administrator
 
Join Date: Sep 2006
Posts: 1,348
Default

Depends, MSC++ 32 bit compiler treats long and int essentially the same, however you can't rely on this being the case on other platforms or software configurations.
Reply With Quote
  #4  
Old 06-30-2009, 07:50 PM
Kobaz
Hill Giant
 
Join Date: Nov 2008
Location: Gold Coast, Oz
Posts: 119
Default

So in the following code snippet:

Code:
int a = -1;

unsigned int b = 3000000000; // 3 billion

if ( a > b) { "true" >> cout } else { "false" >> cout };
what comes out in Visual C++?
Reply With Quote
  #5  
Old 06-30-2009, 08:03 PM
Shendare
Dragon
 
Join Date: Apr 2009
Location: California
Posts: 814
Default

My compiler outputs "true" in the presented situation.

Microsoft Visual C++ 2008 Standard on WinXP Pro 32-bit.
Reply With Quote
  #6  
Old 06-30-2009, 08:19 PM
Kobaz
Hill Giant
 
Join Date: Nov 2008
Location: Gold Coast, Oz
Posts: 119
Default

What about:

Code:
int a = -1;

unsigned int b = 3000000000; // 3 billion

if ( long(a) > long(b)) { "true" >> cout } else { "false" >> cout };
?
Reply With Quote
  #7  
Old 06-30-2009, 08:32 PM
Shendare
Dragon
 
Join Date: Apr 2009
Location: California
Posts: 814
Default

Still true. In 32-bit, long is the same as int, so they're both being cast as signed ints at that point, changing b from 3000000000 to -1294967296, which is less than -1.
Reply With Quote
  #8  
Old 06-30-2009, 09:04 PM
Kobaz
Hill Giant
 
Join Date: Nov 2008
Location: Gold Coast, Oz
Posts: 119
Default

Sigh. To an old Smalltalk programmer like me, I have to wonder why C++ is called "Object Oriented". I try to understand the language, but it seems to be quite unintuitive.

I'm struggling to understand the emu code - I keep expecting fundamental data types to "do the right thing, with no surprises". How do people write robust correct code in C++ without going mad?

Thanks for your time, I'll just keep away from the code, as I seem to break things due to my Smalltalkness. Perhaps I'm just too old..... everything got hard after about 1986. And don't get me started on Java....
Reply With Quote
  #9  
Old 06-30-2009, 09:51 PM
Shendare
Dragon
 
Join Date: Apr 2009
Location: California
Posts: 814
Default

Well, object-oriented has more to do with classes and inheritance and derivation than anything else.

If things are being done properly, you should practically never be in a situation where you're comparing a signed type with an unsigned type anyway.

The fundamental piece of data that's being tracked by a particular integer variable should be conceptualized as either allowing negative values or not, and should then always be stored in an appropriately typed variable.

Is there a particular piece of code in the Emu source that's causing you this grief?
Reply With Quote
  #10  
Old 07-01-2009, 01:04 AM
Kobaz
Hill Giant
 
Join Date: Nov 2008
Location: Gold Coast, Oz
Posts: 119
Default

This thread is about to go way off topic....

I was thinking of modifying the game to make it far more friendly for my kids (the youngest is only 7), so I wanted to add easier corpse recovery, and commands that would be richer macros to make it more fun for them, whilst requiring them to think and read more. So I was digging around in the code, trying to make sense of it. I was getting confused by the compiler warnings.

The problem seems to be that I read an expression like "a > b" as "the message > is sent to the instance a with the argument instance b", but it seems that a doesn't know that it's being asked to compare itself to b, because it isn't! This seems to me to be a pure procedural language with classes, and not an OOL as I understand it, which is objects being sent messages, even classes are just objects (Smalltalk background - I haven't done procedural programming for many many years). int is not an object in C++, indeed it seems that only things that are explicitly called with new are objects, which is not what I'm used to!

In Smalltalk everything in the code is either a reference or a message to a reference to an instance. If an Integer is asked to compare itself to another class, it sends a message to that class to create a new instance of the other class from itself (the Integer), and passes the message and argument to the new reference, and returns the result (this is polymorphism). This is clearly not how C++ works. I'm not sure that I have the time to learn another language, especially one that has some features that look a bit OOL but work completely differently, given my very busy life. It's the subtleties that will stymie me, as I have worked with C a long time ago, but the emu code is sufficiently different (and far more sophisticated) than any C I ever wrote that I seem to spend more time trying to work out what the language is doing than actually implementing anything.
Reply With Quote
  #11  
Old 07-01-2009, 01:42 AM
Shendare
Dragon
 
Join Date: Apr 2009
Location: California
Posts: 814
Default

Ah, I'm starting to get the picture.

C++ would allow you to create a new class based on an int in order to add functions and override operators for enhanced functionality, but at its core C++ is based on C, which is itself an abstraction of assembly and pure native machine code.

This means that the foundation of C++ consists of the fundamental data types that can be directly worked with by the CPU... just different sizes of integers and floating points.

Anything beyond the numbers is implemented programmatically via classes, functions, and operators.
Reply With Quote
  #12  
Old 07-01-2009, 11:00 AM
Yeormom
Discordant
 
Join Date: Apr 2004
Location: 127.0.0.1
Posts: 402
Default

Quote:
Originally Posted by Kobaz View Post
When comparing an int with an unsigned int, are both promoted to a long by the compiler?
You should get in the habbit of casting the variables to the degree of precision you are actually trying to get. By doing so, you never need to smoke test that procedure as you know exactly what will happen every time. It's good programming practice that unfortunately rarely makes it to the hobbyist world.
__________________
Yeorwned
Bane of Life [Custom Classic/PvP]
Reply With Quote
  #13  
Old 07-01-2009, 03:40 PM
Kobaz
Hill Giant
 
Join Date: Nov 2008
Location: Gold Coast, Oz
Posts: 119
Default

Ok, that makes sense. Since (as was pointed out above) on a 32 bit system a long is the same precision as an int, what should I be casting to in order to get the scenario I started with, and how? And is casting 32 bit signed to 64 bit signed expensive?
Reply With Quote
  #14  
Old 07-01-2009, 04:37 PM
Shendare
Dragon
 
Join Date: Apr 2009
Location: California
Posts: 814
Default

It isn't generally expensive at all. Most compilers and processors can work with 64-bit numbers inherently.

However, different compilers do use different terms for explicit 64-bit terminology. In MS C++ the internal type name is '__int64', as all of their integer types are actually aliases for __intXX precision-defining monikers (char is __int8, short is __int16, WORD would be 'unsigned __int16', etc.)

LONGLONG (all caps) is a common typedef defined in Windows.h for MS C++ as well, which maps to __int64.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 12:29 PM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3