Mat*_* M. 12
不,不是一般的.
你知道gcc -fsplit-stack
吗?
由实现决定是分配连续堆栈还是堆栈,其中块与内存中的堆块交错.祝你好运,确定在分割后是否为堆或堆栈分配了一个块.
注意:无论如何,这是一个无用的信息......
如果您正在构建将堆栈存储在比堆大的地址上的体系结构,则可以将变量地址与堆栈的底部进行比较.使用pthread
线程API,此比较将如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <inttypes.h>
int is_stack(void *ptr)
{
pthread_t self = pthread_self();
pthread_attr_t attr;
void *stack;
size_t stacksize;
pthread_getattr_np(self, &attr);
pthread_attr_getstack(&attr, &stack, &stacksize);
return ((uintptr_t) ptr >= (uintptr_t) stack
&& (uintptr_t) ptr < (uintptr_t) stack + stacksize);
}
Run Code Online (Sandbox Code Playgroud)
考试:
int main()
{
int x;
int *p1 = malloc(sizeof(int));
int *p2 = &x;
printf("%d %d\n", is_stack(p1), is_stack(p2));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
... 0 1
按预期打印.
上面的代码不会检测其他线程中的堆栈存储.为此,代码需要跟踪所有创建的线程.
归档时间: |
|
查看次数: |
9355 次 |
最近记录: |