任意数组索引范围? [英] arbitrary array index range?

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

问题描述

一本关于C的书解释了一种使用任意数组索引范围的技术。


int * p = malloc(sizeof(int)* 10) - 1;


这样,分配的数组可以用索引范围1~10来访问,

不是0~9。你当然不能使用p [0]而且内存应该是

免费发布(p + 1)。

它与gcc和MSVC合作,但我不确定是否有意义采取

数组的初始元素之前的地址。有没有人有任何明确答案?


-

ES Kim

"A Book on C" explains a technique to use an arbitrary array index range.

int* p = malloc(sizeof(int) * 10) - 1;

This way, the allocated array can be accessed with index range 1 ~ 10,
not 0 ~ 9. You cannot use p[0], of course, and the memory should be
released with free(p + 1).
It worked with gcc and MSVC, but I''m not sure if it makes sense to take
the address before the initial element of an array. Does anyone have
any definite answer?

--
ES Kim

推荐答案

" ES Kim" < no@spam.mail>在消息新闻中写道:ck ********** @ news1.kornet.net ...
"ES Kim" <no@spam.mail> wrote in message news:ck**********@news1.kornet.net...

int * p = malloc(sizeof(int)* 10) - 1;

int* p = malloc(sizeof(int) * 10) - 1;




更正:int * p =(int *)malloc(sizeof(int)* 10) - 1;


-

ES Kim



correction: int* p = (int*)malloc(sizeof(int) * 10) - 1;

--
ES Kim


ES Kim< no@spam.mail>写道:
ES Kim <no@spam.mail> wrote:
" A book on C"解释了使用任意数组索引范围的技术。
int * p = malloc(sizeof(int)* 10) - 1;


坏书。

这样,分配的数组可以被索引范围1~10访问,不是0~9。你不能当然,使用p [0]并且应该释放内存(p + 1)。


索引从零开始有什么问题?我发现它是最自然的方式,因为我退出Pascal。


如果你真的需要从1开始索引,那么浪费一个元素:

int * p = malloc(sizeof(int)*(10 + 1));

它适用于gcc和MSVC,但我不确定它是否适用在数组的初始元素之前采用
地址是有意义的。有没有人有任何明确的答案?
"A Book on C" explains a technique to use an arbitrary array index range. int* p = malloc(sizeof(int) * 10) - 1;
Bad book.
This way, the allocated array can be accessed with index range 1 ~ 10,
not 0 ~ 9. You cannot use p[0], of course, and the memory should be
released with free(p + 1).
Whats wrong with indexes starting from zero? I find it the most
natural way ever, since I quit Pascal.

If you really-really need to index from 1, then waste one element:
int* p = malloc(sizeof(int) * (10 + 1));
It worked with gcc and MSVC, but I''m not sure if it makes sense to take
the address before the initial element of an array. Does anyone have
any definite answer?




纯粹的巧合。未定义的行为。


指针可以指向一个数组对象,也可以指向一个结束对象。


-

Stan Tobias

sed's / [AZ] // g''发送电子邮件



Pure coincidence. Undefined behaviour.

A pointer can point into an array object or one past the end.

--
Stan Tobias
sed ''s/[A-Z]//g'' to email


" ES Kim" < no@spam.mail>写道:
"ES Kim" <no@spam.mail> wrote:
" ES Kim" < no@spam.mail>在消息新闻中写道:ck ********** @ news1.kornet.net ...
"ES Kim" <no@spam.mail> wrote in message news:ck**********@news1.kornet.net...

int * p = malloc(sizeof(int)* 10) - 1;

int* p = malloc(sizeof(int) * 10) - 1;



更正:int * p =(int *)malloc(sizeof(int)* 10) - 1;



correction: int* p = (int*)malloc(sizeof(int) * 10) - 1;




不仅原来是错的,校正也是如此。你不能在对象开始之前设置一个指向该位置的指针。

上面的两行都可能导致分段错误,或者如果你是/>
少运气,未明确行为的不明显症状。


Richard



Not only is the original wrong, so is the correction. You cannot
portably set a pointer to the location before the start of an object.
Both of your lines above could cause segmentation faults, or if you''re
less lucky, more unobvious symptoms of undefined behaviour.

Richard


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

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