c:warning: unsigned long intint format, different type arg (arg 2)

GCC 编程简介_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
GCC 编程简介
上传于||文档简介
&&G​C​C​ ​编​程​简​介
阅读已结束,如果下载本文需要使用5下载券
想免费下载本文?
下载文档到电脑,查找使用更方便
还剩63页未读,继续阅读
你可能喜欢【Linux调试经验】编译时sscanf参数警告不可忽略 - 博客频道 - CSDN.NET
图像、视频、算法、Linux
Ideas worth spreading
分类:Linux调试经验
本人在为一款路由器写一个linux内核模块时遇到如下问题。
从一块内存buf中格式化字符到变量中,发现变量的值总是不正确。
写好的代码编译和链接能通过,运行就可能出错了。以前该模块写成后在交叉编译器mipsel-linux-gcc(版本:V3.4.2)下运行没问题,后来换成V4.6.3版本后编译,运行就不正确了。(其实代码本身就有问题,代码在mipsel-linux-gcc V4.3.2 下正常运行只是侥幸)
相关代码:
int16_t a, b, c,
sscanf(buf, "%d %d %d %d", &a, &b, &c, &d);
//其中buf = "192 168 16 1\n"
编译信息如下:
make -C /root/study/MTK3/MTK_source/linux-2.6.36.x M=/root/study/netfilter3/t1
make[1]: Entering directory `/root/study/MTK3/MTK_source/linux-2.6.36.x'
/root/study/netfilter3/t1/built-in.o
/root/study/netfilter3/t1/flow_monitor.o
/root/study/netfilter3/t1/flow_monitor.c:274: warning: function declaration isn't a prototype
/root/study/netfilter3/t1/flow_monitor.c: In function `read_conf_ip':
/root/study/netfilter3/t1/flow_monitor.c:300: warning: int format, different type arg (arg 3)
/root/study/netfilter3/t1/flow_monitor.c:300: warning: int format, different type arg (arg 4)
/root/study/netfilter3/t1/flow_monitor.c:300: warning: int format, different type arg (arg 5)
/root/study/netfilter3/t1/flow_monitor.c:300: warning: int format, different type arg (arg 6)
/root/study/netfilter3/t1/maclist.o
/root/study/netfilter3/t1/iplist.o
/root/study/netfilter3/t1/flow_proc.o
/root/study/netfilter3/t1/flow_manage.o
Building modules, stage 2.
MODPOST 1 modules
/root/study/netfilter3/t1/flow_manage.mod.o
/root/study/netfilter3/t1/flow_manage.ko
make[1]: Leaving directory `/root/study/MTK3/MTK_source/linux-2.6.36.x'
注意以上警告信息说
flow_monitor.c 文件第300行的函数中第三、四、五、六个参数格式不对。对应行如下:
sscanf(buf, "%d %d %d %d", &a, &b, &c, &d);
其中变量a、b、c、d声明的是uint16_t 。由于编译时我总是忽略该警告,导致后来查找半天才解决。
sscanf等类似函数在格式化字符串时,制定了%d则默认目的地址大小是一个int型所占的空间大小。在我所用的开发板,cpu是32位,也即int类型占用4个字节地址空间,而变量a、b、c、d确是uint16_t 类型的。所以必然导致变量后面两个字节的空间被踩坏,且还因为大小端的问题, 导致读出数据错位且不正确。
原理详解(图文)
由上图可以看出,格式化后,变量a的值为0,这就是问题所在。
排名:第8091名
(14)(26)(8)(3)(10)(2)(5)(2)(0)(7)(5)(7)(1)(4)(0)(0)(0)(1)(2)(1)(1)ubuntu下编程指南_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
ubuntu下编程指南
上传于||文档简介
&&简​洁​的​讲​述​u​b​u​n​t​u​系​统​中​编​程​技​巧​。
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
下载文档到电脑,查找使用更方便
还剩3页未读,继续阅读
你可能喜欢

我要回帖

更多关于 unsigned long int 的文章

 

随机推荐