关于 keil C51 warning C182: Pointer to Different Objects 解决.

帮助文档里的解释是: 


"Warning C182 : Pointer to Different Objects"


这里的警告一般都是犯的类型不匹配的错误, 例如int 型与int *类型互相赋值. 等等.

我这里犯的错误是, 我使用了函数指针, 而且参数超过了三个, 由于 keil 编译器的参数并不经过栈来传递参数, 超过三个就会报错或者警告.

按照帮助文档的解决方法, 要加 reentrant 关键字来告诉编译器对函数进行可重入和可递归处理.

例如我在结构体内的函数指针后面加入 reentrant 关键字, 如果要将某个函数赋值给这个函数指针, 那么这个函数在定义和声明的时候都要加入 reentrant 关键字.

具体如下:

unsigned char (* pFunc) (unsigned char *a, unsigned char *b, unsigned int len) reentrant;

声明时:

unsigned char  Test_Reentrant_Fun(unsigned char *a,  unsigned char *b,  unsigned int len) reentrant;

定义时:

unsigned char  Test_Reentrant_Fun(unsigned char *a,  unsigned char *b,  unsigned int len) reentrant
{
        // your code
}

不然 

pFunc = Test_Reentrant_Fun;

就会出现 C182 警告. 


关于 reentrant function 可以参考 Cx51 帮助文档. 

这里摘录一部分内容:

A reentrant function may be shared by several processes at the same time. When a reentrant function is executing, another process can interrupt the execution and then begin to execute that same reentrant function.

Normally, functions in Cx51 cannot be called recursively or in a fashion which causes reentrancy. The reason for this limitation is that function arguments and local variables are stored in fixed memory locations. Recursive calls to the function use the same memory locations. And, in this case, arguments and locals would get corrupted.

The reentrant function attribute allows you to declare functions that may be reentrant and, therefore, may be called recursively. For example:

int calc (char i, int b) reentrant  {
        int  x;
        x = table [i];
        return (x * b);
}

Reentrant functions can be called recursively and can be called simultaneously by two or more processes. Reentrant functions are often required in real-time applications or in situations where interrupt code and non-interrupt code must share a function.





你可能感兴趣的:(开发工具,C/C++)