作者ric2k1 (Ric)
看板EE_DSnP
標題Re: [問題] 關於作業七
時間Sun Dec 9 18:30:47 2007
※ 引述《Knossos ()》之銘言:
: ※ 引述《ric2k1 (Ric)》之銘言:
: : Yes.
: 既然size_t就是非負整數,那它跟unsigned有什麼差別?為什麼要用它?
Different architectures or platforms may have different difinitions on the
sizes of different data types. It's difficult to set a single standard for
everyone to follow. Therefore, in ISO/IEC 9899:1990, they left the definition
of the short int, the int, the long int, and the pointer deliberately vague
to avoid artificially constraining hardware architectures.
The only constraint:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(size_t)
Usually sizeof(size_t) is defined as the same as sizeof(void *) to match
the size of pointer variable (i.e number of bytes to store a memory address).
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.121.134.54
1F:推 Knossos:sizeof(void *)不是4嗎?怎麼會比sizeof(long)大? 12/09 21:31
2F:→ ric2k1:sizeof(long) = sizeof(long int) = 4 12/09 22:32
3F:→ ric2k1:sizeof(long long) 才 = 8... 12/09 22:32
4F:推 timrau:我查到的是sizeof(long) <= sizeof(long long)耶.... 12/09 22:32
5F:→ ric2k1:其實 int 分為 short int and long int, 一般在用的 int 12/09 22:33
6F:→ ric2k1:其實就是 long int... 而 short int 就是 short. 12/09 22:34
7F:推 timrau:For example in Turbo C++, sizeof(long)=4 while 12/09 22:40
8F:→ timrau:sizeof(size_t)=2 12/09 22:40
9F:→ ric2k1:說實在的 Turbo C++ 這樣蠻奇怪的, 雖然我後來查到 12/10 00:39
10F:→ ric2k1:ISO/IEC 9899:1990 was withdrawn... 不過 size_t 應該通常 12/10 00:40
11F:→ ric2k1:是用來存 number of objects (如 array size), 12/10 00:41
12F:→ ric2k1:so it must be big enough to hold the "maximum number 12/10 00:42
13F:→ ric2k1:of objects" that can be hold. 2^16 = 65536 也太少了吧? 12/10 00:42
14F:推 lionel20002:dev-C++sizeof(size_t)是4 compiler有差?? 12/10 06:06