c - 指针赋值将指针从 64 位截断为 32 位

标签 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;
  }

其中 PLSA_DM_LDAP_CONNECTION 是用于以下 structtypedef:

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

基本上,HANDLE 类型无处不在。

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

问题:

在从 LsaDmpGetLdapHandle 返回的内容分配 hDirectory 的行之后代码会崩溃,我尝试进一步使用 hDirectory

到目前为止我调试的内容:

附加gdb,pConn中的hLdapConnection是:

(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 * 定义

最佳答案

看起来像 this 的复制品

By default all return values are int. So if a prototype is missing for function then compiler treats the return value as 32-bit and generates code for 32-bit return value. Thats when your upper 4 bytes gets truncated.

关于c - 指针赋值将指针从 64 位截断为 32 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54026669/

相关文章:

C - 链表和指针问题

c - 你最喜欢的 C 编程技巧是什么?

c - 如何在Golang(v1.3.2)中使用C库

c++ - OS X 上的 OpenMP 汇编程序错误

C编程: void* - Why not parametric polymorphism?

c - void*,字符串和字符的指针

c - snprintf 在函数内导致中止陷阱 6 但不在 main 中

c - 获取工作站或服务器上的域名

c - 从 (void *) 类型取消引用结构

c - 在 Julia 中声明 C void 指针的正确方法