size_t的秘密
2011-06-23 16:37
148 查看
今天在看一个经典的双链表的时候看到了一个类型size_t,这个类型在此之前也经常见到,但是并没有太在意,一直都把他当成整形在用,但是在分析双链表的代码时发现了一个问题,跟我理解的有些出入,但是运行代码他的写法却是对的,于是就开始找我理解出错的地方,从头看了一遍,发现唯一的问题就是size_t类型,上网搜索了一下,讲解的并不多,一般都说把他当int用,于是自己写了一个代码测试下,代码如下:
#include <stdio.h>
int main()
{
size_t a=-1;
size_t b=90;
if (a<b)
printf("a bao chi bu bian/n");
else
printf("a bei bian yi wei bu ma/n");
}
运行结果为:
[root@localhost dry]# ./cc
a bei bian yi wei bu ma
调试看了下结果如下:
[root@localhost dry]# gdb cc
GNU gdb Fedora (6.8-29.fc10)
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
(gdb) list
1 #include <stdio.h>
2 int main()
3 {
4 size_t a=-1;
5 size_t b=90;
6 if (a<b)
7 printf("a bao chi bu bian/n");
8 else
9 printf("a bei bian yi wei bu ma/n");
10 }
(gdb) b 6
Breakpoint 1 at 0x80483d3: file cc.c, line 6.
(gdb) run
Starting program: /home/spexamples/1/5/dry/cc
Breakpoint 1, main () at cc.c:6
6 if (a<b)
Missing separate debuginfos, use: debuginfo-install glibc-2.9-2.i686
(gdb) print a
$1 = 4294967295
(gdb) print b
$2 = 90
(gdb) s
9 printf("a bei bian yi wei bu ma/n");
(gdb) s
a bei bian yi wei bu ma
10 }
由此可以看出,在编译的过程中size_t类型的a值会被编译他的补码。所以在使用size_t类型数据的过程中尤其要注意,特别是在逻辑表达式中使用到该类型,稍不注意可能带来很严重的后果。
注:正数的补码:与原码相同;负数的补码:符号位为1,其余位为该数绝对值的原码按位取反,然后整个数加1
#include <stdio.h>
int main()
{
size_t a=-1;
size_t b=90;
if (a<b)
printf("a bao chi bu bian/n");
else
printf("a bei bian yi wei bu ma/n");
}
运行结果为:
[root@localhost dry]# ./cc
a bei bian yi wei bu ma
调试看了下结果如下:
[root@localhost dry]# gdb cc
GNU gdb Fedora (6.8-29.fc10)
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
(gdb) list
1 #include <stdio.h>
2 int main()
3 {
4 size_t a=-1;
5 size_t b=90;
6 if (a<b)
7 printf("a bao chi bu bian/n");
8 else
9 printf("a bei bian yi wei bu ma/n");
10 }
(gdb) b 6
Breakpoint 1 at 0x80483d3: file cc.c, line 6.
(gdb) run
Starting program: /home/spexamples/1/5/dry/cc
Breakpoint 1, main () at cc.c:6
6 if (a<b)
Missing separate debuginfos, use: debuginfo-install glibc-2.9-2.i686
(gdb) print a
$1 = 4294967295
(gdb) print b
$2 = 90
(gdb) s
9 printf("a bei bian yi wei bu ma/n");
(gdb) s
a bei bian yi wei bu ma
10 }
由此可以看出,在编译的过程中size_t类型的a值会被编译他的补码。所以在使用size_t类型数据的过程中尤其要注意,特别是在逻辑表达式中使用到该类型,稍不注意可能带来很严重的后果。
注:正数的补码:与原码相同;负数的补码:符号位为1,其余位为该数绝对值的原码按位取反,然后整个数加1
相关文章推荐
- C语言的那些小秘密——链表二(转bigloomy)
- cocos2dx关于size的知识
- IOS 获取图片的size
- Use DIffent Font and Size.
- Minimum Size Subarray Sum
- list!=null和list.size()>0的区别
- pagespeed 摘要 - Minimize payload size
- ios storyboadr(sizeClass)开发常用技巧及错误
- 人工智能与前端技术不能说的秘密
- mysql中key_buffer_size,query_cache_size,table_cache的合理设置
- mysql innodb_log_buffer_size和 innodb_buffer_pool_size设置
- JeeSite 中同是管理员确不能修改角色的秘密
- 你不知道的5个Web技术秘密!
- 原创---tcp udp: send一次发送数据长度大于recv size_t len时,windows、linux下不同返回
- 【重磅】AI击败顶级德扑玩家的秘密!德扑AI创造者现身reddit,全面解答34个提问,详解Libratus的现状和未来
- Java的秘密 使用全屏幕模式
- C++中string的size,length,capacity三者到底有何区别求解啦?
- Sql server Insert执行的秘密(上)一个最简单的INSERT分析
- 如何成为“天才”一员: 苹果内部秘密培训手册曝光
- Xcode提示“expression is not assignable” self.view.frame.size.height = 100f无法赋值