关于 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.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值