在C(不带组件)便携式函数,返回其堆栈帧的大小 [英] Portable function in C (with no assembly) that returns the size of its stack frame

查看:198
本文介绍了在C(不带组件)便携式函数,返回其堆栈帧的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C编写的便携功能(没有组装)返回其堆栈帧的大小

  INT stackframe_size()
{}

尝试如下解决它 - 这个函数返回228个字节时VS 2010编译有没有办法来验证它的正确性?

  INT stackframe_size(INT运行)
    {
        INT I;        如果(!RUN)
        {
            回报((INT)(安培; I) - stackframe_size(++运行));        }
        回报(INT)(安培; I)    }

援引为:

  INT的main()
    {
        的printf(stackframe_size的\\ n大小()是:%d字节,stackframe_size(0));
        返回0;
    }


解决方案

没有这样的移动的功能是可能的。

您尝试大概是接近你可以得到的,但指针减法是未定义行为。更一般地, P1 - P0 ,其中 P0 P1 被指向不同的对象,是未定义的行为。

除了你的code减去至 INT 值是从地址转换的结果。指针直接减法更容易的工作 - 和指针应该是类型的char * 无符号字符* 。有许多的实现,其中 INT 是太小,无法容纳转换的指针,一些地方的指针有一个重新presentation复杂得多,你承担,并将其转换为连一个足够大的整数类型并不一定给你一个有意义的结果。

有没有在其中栈帧被压入和弹出一个连续的内存区域的感觉用一个堆栈真实世界的C实现。 (必须有一个先入后出的数据结构的意义上的栈,但堆叠的实现方式是完全未指定的。)一些IBM主机实现中,例如,对于一个功能分配存储器通过像堆调用,以便有两个这样的调用中的局部变量的地址之间没有明确关系。

您可以写大概在纯C(无汇编语言)功能,让你一个堆栈帧的的大小特定实施的。但由于C语言本身没有一个堆栈帧(标准甚至不使用这个词堆栈)的概念,它不能做的可移植性

Write a portable function in C (with no assembly) that returns the size of its stack frame

int stackframe_size()
{

}

Attempted solving it as below - This function returns 228 bytes when compiled with VS 2010. Is there a way to verify its correctness?

    int stackframe_size(int run)
    {
        int i ;

        if(!run)
        {
            return ((int)(&i) - stackframe_size(++run));

        }
        return (int)(&i);

    }

Invoked as:

    int main()
    {
        printf("\nSize of stackframe_size() is: %d bytes",stackframe_size(0)) ;
        return 0;
    }

解决方案

No such portable function is possible.

Your attempt is probably about as close as you can get, but the pointer subtraction has undefined behavior. More generally, p1 - p0, where p0 and p1 are pointers to distinct objects, has undefined behavior.

Except that your code subtracts to int values that are the result of conversions from addresses. Direct subtraction of the pointers is more likely to work -- and the pointers should be of type char* or unsigned char*. There are many implementations where int is too small to hold a converted pointer, and some where pointers have a more complicated representation than you assume, and converting them to even a sufficiently large integer type will not necessarily give you a meaningful result.

There are real-world C implementations that don't use a "stack" in the sense of a contiguous region of memory on which "stack frames" are pushed and popped. (There has to be a "stack" in the sense of a first-in last-out data structure, but the way that "stack" is implemented is entirely unspecified.) Some IBM mainframe implementations, for example, allocate memory for a function call via something like a heap, so that there is no defined relationship between the addresses of the local variables for two such calls.

You can probably write a function in pure C (without assembly language) that gives you the size of a stack frame for a particular implementation. But since the C language itself has no concept of a "stack frame" (the standard doesn't even use the word "stack"), it cannot be done portably.

这篇关于在C(不带组件)便携式函数,返回其堆栈帧的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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