由于您指定了 Visual C 和断言,我将假设您可以使用调试版本。在这种情况下,您可以利用此特定编译器放置的用于内存检查的栅栏:
#define IS_POINTER_TO_STACK(vp) (*((int*)(vp)-1)==0xCCCCCCCC)
在调试版本中的所有这些情况下都能正常工作:
#define ASSERT(v) printf("assert: %d\n", v); //so it doesn't really quit
int g_n = 0;
void test_indirectly(void* vp) {
ASSERT(IS_POINTER_TO_STACK(vp));
}
void F() {
int *pA = &g_n;
ASSERT(IS_POINTER_TO_STACK(pA)); //0
int i = 0;
int j = 0;
int *pB = &i;
ASSERT(IS_POINTER_TO_STACK(pB)); //1
ASSERT(IS_POINTER_TO_STACK(&j)); //1
int *pC = (int*)malloc(sizeof(int));
ASSERT(IS_POINTER_TO_STACK(pC)); //0
free(pC);
ASSERT(IS_POINTER_TO_STACK(pC)); //0
pC = new int;
ASSERT(IS_POINTER_TO_STACK(pC)); //0
delete pC;
char* s = "HelloSO";
char w[6];
ASSERT(IS_POINTER_TO_STACK("CONSTANT")); //0
ASSERT(IS_POINTER_TO_STACK(s)); //0
ASSERT(IS_POINTER_TO_STACK(&w[0])); //1
test_indirectly(&s); //1
int* pD; //uninit
ASSERT(IS_POINTER_TO_STACK(pD)); //runtime error check
}
(除了最后一个由于未初始化的内存而导致运行时错误 - 但这仍然可以用于验证您的指针。)
这仅适用于 Debug 构建 - Release 构建对所有这些都报告错误。