数组索引的类型? [英] type of array index?

查看:102
本文介绍了数组索引的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为了获得最大的可移植性,数组索引的类型应该是什么?

可以安全使用任何整数类型吗?或者我应该只使用无符号类型?

或者什么?


如果我使用指针访问数组元素为*(mptr + k)我已经宣布

宣布


MYTYPE * mptr;


应该是什么类型'' K ''?它应该是ptrdiff_t吗?


OT:接近因为我可以告诉我gcc的实现没有

< stddef.h>定义了ptrdiff_t的文件。我忽略了什么吗?


-

解决方案

>什么应该是''k'的类型?应该是ptrdiff_t吗?


它应该是任何类型的整数。


Jon

----

学习使用Linux汇编语言进行编程
http ://www.cafeshops.com/bartlettpublish.8640017


如果你真的想要ptrdiff_t,它似乎在malloc.h中。 stdint.h

有其限制。有趣的是,obstack.h认为它也出现在

stddef.h中。也许有人应该向维护者报告。

使用gcc 3.3.1。


Jon

----

学习使用Linux汇编语言进行编程
http:/ /www.cafeshops.com/bartlettpublish.8640017


sh ******** @ ticnet.com 写道:

为了获得最大的可移植性,数组索引的类型应该是什么?可以安全地使用任何整数类型吗?或者我应该只使用无符号类型?
或者什么?


基本上,有两种选择:

1.只要有足够的时候使用int,必要时使用更宽的类型。

足够对标准的最低要求来说足够了



2.总是使用size_t。

size_t足以满足所有阵列的需求索引所有存储你

可以分配(按字节顺序)。


就个人而言,我赞成第二种方法,但我不信教

关于它。缺点:由于索引类型固有的

非负性,你必须更加小心,例如

for(i = MAX-1; i> = 0; i- - )

operation_on(array [i]);

必须变为

for(i = MAX; i> 0; i- - )

operation_on(array [i-1]);



for(i = MAX-1; i!= - 1 ; i--)

operation_on(array [i]);

我更喜欢。

至于int:有可能int索引是更快的。作为int通常

是自然的系统的整数类型,但如果确实如此,那么仍然可以在必要时进行优化。未定义的行为

由于忘记了老式行为16位整数和结果

奇怪的错误不太好。


然而,有很多关于此的讨论。

Do如你所愿,并根据需要寻找范围/健全性检查(在

的情况下)。


如果我使用指针访问数组元素*(mptr + k)我已经宣布了

MYTYPE * mptr;

什么应该是''k'?它应该是ptrdiff_t吗?


任何满足您的范围要求的整数类型。

我会在适当的时候使用ptrdiff_t。


如果ssize_t已经成为标准,我会建议。


OT:接近因为我可以告诉我gcc的实现没有
< ; STDDEF.H>定义了ptrdiff_t的文件。我忽略了什么?




你在头文件中找不到它并不意味着

它不存在一些魔法(或者只是从另一个

包含的文件进来。


试试吧:

如果


#include< stddef.h>

int main(void){int a [3]; ptrdiff_t b =(a + 2) - (a + 1);返回0; } $ / $

无法编译,提交错误报告。

如果没有#include进行编译,请执行相同操作。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



For maximum portability what should the type of an array index be? Can
any integer type be used safely? Or should I only use an unsigned type?
Or what?

If I''m using pointers to access array elements as *(mptr+k) where I''ve
declared

MYTYPE *mptr;

what should be the type of ''k''? Should it be ptrdiff_t?

OT: near as I can tell my implementation of gcc doesn''t have an
<stddef.h> file with ptrdiff_t defined. Am I overlooking something?

--

解决方案

> what should be the type of ''k''? Should it be ptrdiff_t?

It should be any type of integer.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017


If you really want ptrdiff_t, it appears to be in malloc.h. stdint.h
has its limits. Interestingly, obstack.h thinks that it appears in
stddef.h, too. Perhaps someone should report that to the maintainers.
Using gcc 3.3.1.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017


sh********@ticnet.com wrote:

For maximum portability what should the type of an array index be? Can
any integer type be used safely? Or should I only use an unsigned type?
Or what?
Essentially, there are two choices:
1. Use int whenever sufficient, use a wider type when necessary.
Sufficient means sufficient with respect to the minimal requirements
from the standard.
2. Always use size_t.
size_t is sufficient for all arrays and for indexing all storage you
can allocate (bytewise).

Personally, I favor the second approach but I am not religious
about it. Drawbacks: You have to be more careful due to the inherent
nonnegativity of the index type, e.g.
for (i=MAX-1; i>=0; i--)
operation_on(array[i]);
either has to become
for (i=MAX; i>0; i--)
operation_on(array[i-1]);
or
for (i=MAX-1; i!=-1; i--)
operation_on(array[i]);
which I like better.
As for int: It is possible that int indices are "faster" as int usually
is the "natural" integer type of the system but if this is really
the case, one can still optimise where necessary. Undefined behaviour
due to forgetting about "old-fashioned" 16 bit ints and resulting
strange errors are less nice.

However, there have been many discussions about that.
Do as you like and look for range/sanity checks as necessary (in both
cases).

If I''m using pointers to access array elements as *(mptr+k) where I''ve
declared

MYTYPE *mptr;

what should be the type of ''k''? Should it be ptrdiff_t?
Any integer type which suffices for your range requirements.
I would use ptrdiff_t only where appropriate.

If ssize_t had made it into the standard, I''d suggest that.

OT: near as I can tell my implementation of gcc doesn''t have an
<stddef.h> file with ptrdiff_t defined. Am I overlooking something?



That you cannot find it in the header file does not mean that
it is not there by some magic (or simply comes in from another
included file).

Just try it out:
If

#include <stddef.h>
int main (void) { int a[3]; ptrdiff_t b = (a+2)-(a+1); return 0; }

does not compile, file a bug report.
If it does compile without the #include, do the same.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于数组索引的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆