解析转换为指​​针,从不同大小的错误整数的64位转换 [英] Resolving cast to pointer from integer of different size error in 64-bit conversion

查看:190
本文介绍了解析转换为指​​针,从不同大小的错误整数的64位转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改 PTHotKeyLib 是64位友好,但我碰到的在code一个问题,我不知道如何解决。在PTHotKeyCenter的registerHotKey方法创建一个EventHotKeyID实例,然后充塞了PTHotKey对象到id属性。原来的code用长。我将它转换为NSInteger的每苹果公司的64位编程指南。

I'm working on modifying the PTHotKeyLib to be 64-bit friendly, but I've run into an issue in the code that I'm not sure how to get around. In PTHotKeyCenter, the registerHotKey method create an EventHotKeyID instance and then stuffs the of PTHotKey object into the id attribute. The original code used a long. I converted it to NSInteger per Apple's 64 bit programming guide.

  - (BOOL)registerHotKey:(PTHotKey *)theHotKey {
  OSStatus error;
  EventHotKeyID hotKeyID;
  EventHotKeyRef carbonHotKey;
  NSValue *key = nil;

  if ([[self allHotKeys] containsObject:theHotKey])
    [self unregisterHotKey:theHotKey];

  if (![[theHotKey keyCombo] isValidHotKeyCombo])
    return YES;

  hotKeyID.signature = kHotKeySignature;
  hotKeyID.id = (NSInteger)theHotKey;
  ... //Rest is not relevant
  }

当用户触发热键,它调用sendCarbonEvent:方法,将试图拉PTHotKey实例出来EventHotKeyID的。它曾在32位的土地,但对64位编译时,它提供了强制转换为大小不同的指针的整数警告

When a user triggers the hot key, it calls the sendCarbonEvent: method that will try to pull the PTHotKey instance out of EventHotKeyID. It worked in 32-bit land, but when compiling against 64-bit, it gives a "cast to pointer from integer of different size" warning

- (OSStatus)sendCarbonEvent:(EventRef)event {
  OSStatus error;
  EventHotKeyID hotKeyID;
  SGHotKey *hotKey;

  NSAssert(GetEventClass(event) == kEventClassKeyboard, @"Unknown event class");

  error = GetEventParameter(event,
                            kEventParamDirectObject, 
                            typeEventHotKeyID,
                            nil,
                            sizeof(EventHotKeyID),
                            nil,
                            &hotKeyID);
  if (error)
    return error;

  NSAssert(hotKeyID.signature == kHotKeySignature, @"Invalid hot key id" );
  NSAssert(hotKeyID.id != 0, @"Invalid hot key id");

  hotKey = (SGHotKey *)hotKeyID.id; // warning: cast to pointer from integer of different size
// Omitting the rest of the code
}

从x86_64的回I386开关消除了警告,一切都编译并运行正常。在x86_64的它会导致破碎机,我不知道如何解决这个问题。就如何解决它有什么建议?

Switching from x86_64 back to i386 removes the warning and everything compiled and runs properly. Under x86_64 it causes a crasher, and I'm not sure how to get around that issue. Any suggestions on how to resolve it?

推荐答案

指针和整数之间的铸造不建议,因为它会导致不可移植的code。

Casting between pointers and integers is not recommended because it results in non-portable code.

你是做什么样的结果在什么C99定义为未定义行为。这基本上意味着它可能工作,它可能不会。 C语言而闻名,让你做这样的事情,因为它隐含地假设你知道,当你在和你有wizardly 37337疯狂的skillz和多年的经验,知道什么时候是安全的,像做键入了你在做什么这一点,当它不是。

What you are doing results in what C99 defines as "undefined behavior". This basically means it might work, and it might not. The C language is famous for letting you do things like this because it implicitly assumes that you knew what you were doing when you typed it in and that you have the wizardly 37337 mad skillz and years of experience to know when it's safe to do something like this and when it's not.

可以肯定地说,这,这是那些时代之一,当它的安全做。

It's safe to say this this is one of those times when it was not safe to do.

这个问题,根据上下文,可能是由于在一个 INT 尺寸可变铸造了64位指针。 Mac OS X的64位ABI是被称为 LP64 ,这意味着指针是64位宽,而 INT 是32位宽。因此,简而言之,在你的指针 INT 之一,回来铸件,你砍掉前32位,这只是碰巧是非常重要的。

The problem, from context, is probably due to casting a 64-bit pointer in to an int sized variable. Mac OS X 64-bit ABI is what is known as LP64, which means that longs and pointers are 64-bits wide, and that ints are 32-bits wide. So, in short, during one of your pointer to int and back again castings, you chopped off the top 32 bits, which just so happened to be really important.

看一下文档,为 ID 的类型是 UInt32的。所以,简单的答案是,你不能把64位指针到32位大小的整数。

Looking at the docs, the type for id is UInt32. So, the short answer is you can't put 64-bit pointers in to 32-bit sized integers.

这篇关于解析转换为指​​针,从不同大小的错误整数的64位转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆