Re: int and short int on intel
Re: int and short int on intel
- Subject: Re: int and short int on intel
- From: Jonathan Fewtrell <email@hidden>
- Date: Fri, 12 Jan 2007 11:00:42 +0800
On 12 Jan 2007, at 10:22, Chris Suter wrote:
On 12/01/2007, at 1:12 PM, Jonathan Fewtrell wrote:
I have two classes, JF1 and JF2, each of which has an ivar called
'scale' of type int, with normal getter and setter accessors.
As part of an array-sorting routine, I use -
sortedArrayUsingFunction:, the function in question being as follows:
int JFSortByScale( id panel1 , id panel2, void *context )
{
int scale1, scale2;
scale1 = [panel1 scale];
scale2 = [panel2 scale];
if ( scale1 > scale2 ) return NSOrderedDescending;
else if (scale1 < scale2 ) return NSOrderedAscending;
else return NSOrderedSame;
}
panel1 and panel2 can be of class JF1 or JF2, hence I use id
rather than specifying the class. Incidentally, neither JF1 nor
JF2 is a subclass of the other.
The problem I am having is that, on intel machines, if scale
exceeds a certain value (which I think is 32k), scale1 or scale2
is set to a different value. It appears that either FF FF or 00 01
gets substituted for the first two bytes. It seems to be something
to do with 2 byte and 4 byte ints.
I get a compiler warning that there is more than one method called
-scale (apart from the ones in JF1 and JF2). The other is in
NSDecimalNumber and is a short int. I assume this is leading to
the problem, but I don't see why. I have been ignoring the warning
because my understanding of ObjC was that at runtime the message -
scale would be sent to the JF1 or JF2 instance and the method
would duly be found and used and the NSDecimalNumber method would
be irrelevant. And on PPC that is true. Even on intel, stepping
through with the debugger shows the correct method is called, and
yet scale1 and scale2 somehow end up being wrong.
What have I misunderstood? I can obviously fix the problem by
renaming the ivars, but why should I have to?
If I read what you're saying correctly, I think the compiler has
encountered two methods with the following definitions:
- (int)scale;
- (short int)scale;
The compiler warning is significant in this case because if it
thinks it's calling the -(short int)scale method when it's actually
calling the - (int)scale method, it will truncate the result to 16
bits and sign extend.
Similarly, if it thinks it's calling - (int)scale when it's
actually calling -(short int)scale, it could end up using garbage
in the top 16 bits (although it depends upon the exact machine code
that your scale method uses).
To solve it, you should cast panel1 and panel2 to the expected type.
Thanks Chris. This confirms my understanding of ObjC was wrong. I
thought that if an object was typed id, the method called would only
be determined at runtime and the return type would be the type of
that method. From what you are saying, it seems that the return type
will be set at compile time (short int in this case) and will not be
changed to the return type of the method that is actually called at
runtime. That's a pity because it means my naming scheme is
constrained by names used in classes that I am not even using
(NSDecimalNumber in this case).
As regards casting, I don't see how I can cast in this case because I
do not know which class panel1 and panel2 will be. They can each be
JF1 or JF2. That's why I used id in the function declaration.
Jon
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden