指针分配将指针从64位截断为32位

时间:2019-01-03 17:02:10

标签: c void-pointers

我一直在尝试修改likewise-open中的一段代码,并完全陷入了困境。

一些背景

正在研究此file,尝试围绕一些LDAP查询进行编码:

typedef void *MYH;
typedef MYH HANDLE;

HANDLE hDirectory = NULL;     
hDirectory = LsaDmpGetLdapHandle(pConn);

LsaDmpGetLdapHandle()已定义here

typedef void *MYH;
typedef MYH HANDLE;

HANDLE
LsaDmpGetLdapHandle(
    IN PLSA_DM_LDAP_CONNECTION pConn
    )
  {
    return pConn->hLdapConnection;
  }

其中跟随以下typedef的PLSA_DM_LDAP_CONNECTION为struct

struct _LSA_DM_LDAP_CONNECTION
  {
  ...
    // NULL if not connected
    HANDLE hLdapConnection;
  ...
  };

基本上,到处都有HANDLE类型。

注意:为避免各种*.h文件对其进行不同定义,我在两个文件中都添加了typedef void *MYH;

麻烦

hDirectory返回的行中分配了LsaDmpGetLdapHandle的行之后,代码将崩溃,而我尝试进一步使用hDirectory

到目前为止,我已调试的内容

hLdapConnection中附加gdb,pConn是:

(gdb) p pConn->hLdapConnection
$5 = (void *) 0x7feb939d6390

但是,hDirectory是:

(gdb) p hDirectory
$6 = (void *) 0xffffffff939d6390

我无法理解分配后的差异为何?

还要注意,两个指针地址中的939d6390很常见。

有趣的是,这两种方法都有效

// If I pass hDirectory reference
LsaDmLdapGetHandle(pConn, &hDirectory);

// where this function is defined as, in the other file:
DWORD
LsaDmLdapGetHandle(
    IN PLSA_DM_LDAP_CONNECTION pConn,
    OUT HANDLE* phDirectory)
{
  HANDLE hDirectory = NULL;
  hDirectory = LsaDmpGetLdapHandle(pConn);
  *phDirectory = hDirectory;
  return ERROR_SUCCESS;
}

// Or I call another function, which then call LsaDmpGetLdapHandle(), in the other file
hDirectory = LsaDmLdapGetHandleCopy(pConn);
HANDLE
LsaDmLdapGetHandleCopy(
    IN PLSA_DM_LDAP_CONNECTION pConn)
{
  HANDLE hDirectory = NULL;
  hDirectory = LsaDmpGetLdapHandle(pConn);
  return hDirectory;
}

我认为,这可能与这两个文件中的HANDLE定义不同有关,因此我在两个文件中都添加了自己的void *定义

1 个答案:

答案 0 :(得分:2)

看起来像this的重复项

  

默认情况下,所有返回值均为int。因此,如果缺少函数原型,则编译器会将返回值视为32位,并为32位返回值生成代码。那就是当您的高4个字节被截断时。

相关问题