32bit gcc 中 int 与 long的区别

69 views
Skip to first unread message

erin

unread,
Apr 18, 2012, 11:37:10 PM4/18/12
to shlug
32位系统下gcc/g++给long和int定的长度都是4 byte,16位上没机会试,不知道,64位long是8 byte,是int 的2倍。

那么这两种类型在32位系统上使用上有什么区别?虽然C++标准(C不清楚)只是给出各种类型的长度范围,供编译器自定,但我还是奇怪为什么32位的gcc把这两种类型的长度定成一样的。

Michael Ding

unread,
Apr 18, 2012, 11:41:48 PM4/18/12
to sh...@googlegroups.com
我的理解是(纯属个人意见):short, long, long long这些是方便你在意byte长度的场景中用
而int可能是方便在你不是很在意到底多少byte的场景中使用

2012/4/19 erin <erin....@gmail.com>

32位系统下gcc/g++给long和int定的长度都是4 byte,16位上没机会试,不知道,64位long是8 byte,是int 的2倍。

那么这两种类型在32位系统上使用上有什么区别?虽然C++标准(C不清楚)只是给出各种类型的长度范围,供编译器自定,但我还是奇怪为什么32位的gcc把这两种类型的长度定成一样的。



--

Michael DING


Main Founder of Geakit < http://geakit.com >

We need open source even we're in China

Twitter:  @geakit

Weibo:  @geakit


Ma Xiaojun

unread,
Apr 18, 2012, 11:42:35 PM4/18/12
to sh...@googlegroups.com

姚飞

unread,
Apr 18, 2012, 11:47:49 PM4/18/12
to sh...@googlegroups.com
关键词 LP32 LLP64

发自我的 iPhone

lxd

unread,
Apr 18, 2012, 11:48:11 PM4/18/12
to sh...@googlegroups.com
64bit上GCC short和int也是一个长度……
有没有体会到对称美……

erin

unread,
Apr 18, 2012, 11:53:01 PM4/18/12
to sh...@googlegroups.com
你用的是啥系统啥版本的GCC?咋我这short还是2byte呢?我用的fedora 16, gcc 4.6.3

2012/4/19 lxd <davl...@gmail.com>

lxd

unread,
Apr 18, 2012, 11:55:59 PM4/18/12
to sh...@googlegroups.com
uname -a
Linux localhost.localdomain 2.6.32-308-ec2 #16-Ubuntu SMP Thu Sep 16 15:25:39 UTC 2010 x86_64 GNU/Linux

gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)

(BTW xiang...@sina.com 是谁的邮箱?自动回复了……)

lxd

unread,
Apr 19, 2012, 12:01:04 AM4/19/12
to sh...@googlegroups.com
不好意思代码写错了,short确实是2bytes~
331.gif

姚 飞

unread,
Apr 19, 2012, 3:32:08 AM4/19/12
to sh...@googlegroups.com
很早以前DOS下Turbo C开发,还有我现在用的 MIL-STD-1750A, 是16位体系,int就是16位的。

32位系统下,一般是LP32模型:int 32位,long和指针void *是32位,short是16位。double和long long 是64位

64位系统下,大部分UNIX类系统是LP64模型:int还是32位,long 和 void *是64位。

但是微软跟人不一样,用LLP64模型,long和int还是32位,long long 和 void * 才是64位

none_nobody

unread,
Apr 19, 2012, 3:57:09 AM4/19/12
to sh...@googlegroups.com
我是来晃点的。

造个人计算机牛郎星的时代,要么是Intel4004开发之后,(经查 其实4004处理只能处理4位数据,但内部指令是8位的)发现8008还是用8位寄存器方便,低8位、全16位寄存器比较妥帖AX BX CX DX SI DI CS SS  BP SP FLAG都是 16位。到了CPU 有了保护模式能力开始的时候,就附带出现32位寄存器,到了MMX的时候就用64位寄存器不过这些都无关紧要了。

要紧的是ANSI 的人有远见,定义了ASCII码表,就有了字节长度。后来的其他码表都弱爆了。
开发gcc的人也顺带紧跟ANSI的脚步. 定义了类型的字节长度,就成为标准了。好像gcc是最早的符合标准的可运行的C环境。

历史上曾有 IBM 709 3tag-15bits address;还有个撒的24位宽。

liyaoshi

unread,
Apr 19, 2012, 5:23:57 AM4/19/12
to sh...@googlegroups.com
arm表示26位操作模式 

匿了

2012/4/19 none_nobody <lyx...@gmail.com>

依云

unread,
Apr 19, 2012, 7:58:32 AM4/19/12
to sh...@googlegroups.com
On Thu, Apr 19, 2012 at 11:55:59AM +0800, lxd wrote:
> (BTW xiang...@sina.com 是谁的邮箱?自动回复了……)

我也另一个线索也收到了。这位比 CPyUG 里的 check...@foxmail.com 态度好
多了呢。下次再收到时再加 procmailrc 里去。

--
Best regards,
lilydjwg

Linux Vim Python 我的博客:
http://lilydjwg.is-programmer.com/
--
A: Because it obfuscates the reading.
Q: Why is top posting so bad?

erin

unread,
Apr 21, 2012, 7:03:36 AM4/21/12
to sh...@googlegroups.com
感谢各位的指教,知道了data model的概念。

似乎data model跟CPU架构和OS都相关,由它们决定用哪种data model,compiler就是跟着cpu 和OS 走。

我的理解,既然32bit gcc用了ILP32,如果完全不考虑移植性的话,int 和 long 是可以随便用的。 

YF Lau

unread,
Apr 23, 2012, 12:59:34 AM4/23/12
to sh...@googlegroups.com
为啥不考虑宏定义这行类型呢?

2012/4/21 erin <erin....@gmail.com>

感谢各位的指教,知道了data model的概念。

似乎data model跟CPU架构和OS都相关,由它们决定用哪种data model,compiler就是跟着cpu 和OS 走。

我的理解,既然32bit gcc用了ILP32,如果完全不考虑移植性的话,int 和 long 是可以随便用的。 



--
Best regards to you and your family

Xunzhen Quan

unread,
Apr 23, 2012, 1:01:48 AM4/23/12
to sh...@googlegroups.com
引用 <stdint.h> 里面就有跨平台一致的类型定义了

2012/4/23 YF Lau <anr...@gmail.com>

YF Lau

unread,
Apr 23, 2012, 1:10:44 AM4/23/12
to sh...@googlegroups.com
stdint.h 被MS Visual Studio 支持吗?

2012/4/23 Xunzhen Quan <quanx...@gmail.com>

Xiangfu Liu

unread,
Apr 23, 2012, 1:12:08 AM4/23/12
to sh...@googlegroups.com, YF Lau
On 04/23/2012 01:10 PM, YF Lau wrote:
> MS Visual Studio

if it support POSIX :-)

Xiangfu

Xunzhen Quan

unread,
Apr 23, 2012, 5:59:54 AM4/23/12
to sh...@googlegroups.com
网络上可以下到给 VS 用的 stdint.h

2012/4/23 YF Lau <anr...@gmail.com>
Reply all
Reply to author
Forward
0 new messages