不要用C隐函数声明实际生成对象code? [英] Do Implict Function Declarations in C Actually Generate Object Code?

查看:96
本文介绍了不要用C隐函数声明实际生成对象code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此讨论的过程中<一个href=\"http://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc\">about铸造的malloc的返回值 很多人都声称,隐含的声明的malloc 将导致回报值转换为 INT 然后再转换回至 T * 可能导致局势指针的截断其中:

In the course of this discussion about casting the return value of malloc many people have claimed that the implicit declaration of malloc would cause the return value to be converted to int then reconverted back to T* possibly resulting in truncation of the pointer in situations where:

sizeof(int) < sizeof(void*)

这将意味着,编译器将执行以下操作:

This would imply that the compiler does the following:


  1. 链接,并调用正确的对象code定义的malloc

  2. 产生目标code将返回值转换为短整型

  3. 产生目标code转换回更大的目标指针类型

有人能真正证明了这种情况?与64位Linux的一些示例code说呢?

Could someone actually prove that this happens? Say with some example code on 64bit Linux?

我会做我自己,但我没有获得一个64位的机器。

I'd do it myself, but I don't have access to a 64 bit machine.

推荐答案

你的什么情况是在步骤2和一个隐含的声明中描述的问题,在调用现场code不转化该函数的返回值,真的。

The problem with your description of what happens is in step 2. With an implicit declaration, the code at the calling site doesn't "convert" the return value of the function, really.

什么情况是,在调用现场code。通过的提取返回值(通常由寄存器,或关闭堆栈)假设的,它的类型为INT的。要做到这一点的方法是不同的操作系统和编译器不同,并且通常由一个ABI文件指定。

What happens is that the code at the calling site extracts the return value (typically from a register, or off the stack) by assuming that it's of type "int". The procedure to do this is different for different OSes and compilers, and is typically specified by an ABI document.

对于最常见的ABI,返回地点和int和void *的尺寸都是一样的,所以你实际上不会有这样做的任何问题,即使它是不正确。这是真实的支持Linux,Windows和Mac OS X上的<击> 32位和64位平台上,我相信 32位平台。

For the most common ABIs, the return location and sizes of int and void* are the same, so you actually won't have any problem doing this, even though it's incorrect. This is true for Linux, Windows, and Mac OS X on both 32- and 64-bit platforms, I believe 32-bit platforms.

在64位平台上,它是长和无效*是相同的大小,因此,如果你有一个malloc的隐式声明()的返回值将被截断更为常见。有几个流行的64位编程模型,虽然

On 64-bit platforms, it's more common for "long" and "void *" to be the same size, so if you have an implicit declaration for malloc(), the return value will be truncated. There are several popular 64-bit programming models, though.

早在DOS下开发的好日子,有可能创建一个模式下运行,其中INT是16位程序,和指针是32位(实际上是24)。在这种情况下,调用malloc()有一个隐含的原型将截断返回的值。

Back in the "good old days" of DOS development, it was possible to create programs that ran in a mode where "int" was 16 bits, and pointers were 32 bits (actually, 24). In those cases, calling malloc() with an implicit prototype would truncate the returned value.

请注意,即使在返回值被截断的情况下,你可能仍然不会有问题,运行时间,这取决于价值是否确实超出int的有效范围。

Note that even in the cases where the return value is truncated, you still might not have a runtime problem, depending on the whether the value is actually outside the valid range of an int.


在Mac OS X上,在64位模式下,该code:

On Mac OS X, in 64-bit mode, this code:

#include <stdio.h>

int main (int argc, const char * argv[]) {
    int x = malloc(128);
    void *p = malloc(128);
    printf("Hello, World!\nsizeof(int)=%d,sizeof(void*)=%d,x=0x%xd,p=%p\n", sizeof(int), sizeof(void *), x, p);
    return 0;
}

打印:

你好,世界!
  的sizeof(INT)= 4的sizeof(无效*)= 8,X = 0x1001c0d,P = 0x100100240

Hello, World! sizeof(int)=4,sizeof(void*)=8,x=0x1001c0d,p=0x100100240

请注意,x的值比P值位数越少,其直接丢弃值的最显著32位。在这两个调用malloc的实际装配code是这样的:

Note that the "x" value has fewer digits than the "p" value, having silently dropped the most-significant 32 bits of the value. The actual assembly code at the two calls to malloc looks like this:

LM2:
    movl	$128, %edi
    call	_malloc
    movl	%eax, -12(%rbp)
LM3:
    movl	$128, %edi
    call	_malloc
    movq	%rax, -8(%rbp)

所以,正确的价值正在由malloc(以%RAX)返回,但MOVL指令截断它,因为它是被搬进变量X。

So, the right value is being returned by malloc (in %rax), but the movl instruction truncates it as it's being moved into variable "x".

这篇关于不要用C隐函数声明实际生成对象code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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