简体   繁体   English

64位函数返回32位指针

[英]64 bit function returns 32 bit pointer

This function is buried in a complex nest so actually finding the cause is probably beyond anything I can ask, but I'm wondering if anyone might be able to give some tips on how I might go about debugging this. 这个函数被埋在一个复杂的嵌套中,所以实际上找到原因可能超出我能提出的任何要求,但我想知道是否有人可能能够提供一些关于我如何进行调试的提示。 Here is the gist of the code I'm having trouble with 这是我遇到问题的代码的要点

//func1.c
somestruct* func1(somestruct* mystruct)
{
    printf("func1: %p, %i\n", mystruct, mystruct->foo);
    return mystruct;
}
//func2.c
somestruct* func1(somestruct* mystruct);
void func2()
{
    somestruct *mystruct = malloc(sizeof(somestruct));
    mystruct->foo = 10;
    printf("func2: %p, %i\n", mystruct, mystruct->foo);
    mystruct = func1(mystruct);
    printf("back in func2: %p\n", mystruct);
    free(mystruct);
}

And I call func2. 我叫func2。 The output is like so 输出是这样的

func2: 0x7f38a00008c0, 10
func1: 0x7f38a00008c0, 10
back in func2: 0xffffffffa00008c0
(SEGFAULT trying to free 0xffffffffa00008c0)

The actual code is more complex, "mystruct" gets passed around in many other places as well without issue, the fact that the functions are in different files seems like it might be part of the problem, yes it needs to return the pointer (the returned pointer is not guaranteed to be the same as the input pointer). 实际代码更复杂,“mystruct”在许多其他地方传递也没有问题,函数在不同文件中的事实似乎可能是问题的一部分,是的,它需要返回指针(返回的指针不保证与输入指针相同)。 It seems really weird to me that it's kind of (but not actually) getting truncated to 32 bits, and then filled with ffffffff at the top. 对我来说,它有点(但实际上并没有)被截断为32位,然后在顶部填充ffffffff,这似乎很奇怪。

When compiled on a 32 bit machine it works exactly as it should. 在32位机器上编译时,它的工作原理完全正常。

I'd considered memory corruption somewhere, so I ran it through valgrind. 我在某处考虑过内存损坏,所以我通过valgrind运行它。 Valgrind reports no errors, and in fact causes it to complete successfully. Valgrind报告没有错误,实际上导致它成功完成。 Textbook heisenbug. 教科书heisenbug。 At least I can use GDB. 至少我可以使用GDB。

Does anyone have any idea what might be causing this, or at least how I might start tracking down the problem? 有没有人知道可能导致这种情况的原因,或者至少我如何开始追查问题?

Check your code if you missed out function prototype (somestruct* func1(somestruct* mystruct);).in func2.c. 如果你在func2.c中错过了函数原型(somestruct * func1(somestruct * mystruct);),请检查你的代码。

By default all return values are int. 默认情况下,所有返回值都是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. 因此,如果函数缺少原型,则编译器将返回值视为32位,并生成32位返回值的代码。 Thats when your upper 4 bytes gets truncated. 那是你的高4字节被截断的时候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM