简体   繁体   English

C数组上的奇怪行为

[英]Strange behaviour on C array

I found really strange behaviour when looping through NSString array. 我遍历NSString数组时发现了非常奇怪的行为。 The thing is it happens only in iOS 32bit environment, but on 64bit works as expected. 事实是,它仅在iOS 32位环境中发生,但在64位环境下可以正常工作。

here is the code I run: 这是我运行的代码:

static NSString * const HexColors[3] = {
    @"FFFFFF",
    @"FF0000",
    @"000000"};

static NSString * const ColorDescription[3] = {
    @"white",
    @"red",
    @"black"};

in implementation file I loop as following 在实现文件中,我如下循环

- (void)loop {
    NSInteger i = 0;
    while (HexColors[i]) {
        NSLog(@"%@", HexColors[i]);
        i++;
    }
}

The result I get: 我得到的结果是:

2014-04-25 09:57:45.374 loopApp[587:60b] FFFFFF
2014-04-25 09:57:45.375 loopApp[587:60b] FF0000
2014-04-25 09:57:45.376 loopApp[587:60b] 000000
2014-04-25 09:57:45.376 loopApp[587:60b] white
2014-04-25 09:57:45.377 loopApp[587:60b] red
2014-04-25 09:57:45.377 loopApp[587:60b] black

And then app throws EXC_BAD_ACCESS on NSLog line 然后应用程序在NSLog行上抛出EXC_BAD_ACCESS

I could use "for" loop but this is not the case 我可以使用“ for”循环,但事实并非如此

Any idea why it happen? 知道为什么会发生吗? Is it the clang issue? 这是c的问题吗?

The line: 该行:

while (HexColors[i]) ...

will only stop at the end of the array if there's some NULL marker there, so you have a couple of options (at least). 如果那里有一些NULL标记,它只会在数组的末尾停止,因此(至少)有几个选择。

First, you can put a NULL marker there, with: 首先,您可以在其中放置一个NULL标记:

static NSString * const HexColors[] = {
    @"FFFFFF",
    @"FF0000",
    @"000000",
    NULL};

Note also there the indeterminate array size [] which creates an array based on the data itself. 还要注意不确定的数组大小[] ,它基于数据本身创建一个数组。 That's often preferred when you supply all the data and don't want to change too much when you add items. 当您提供所有数据并且不想在添加项目时更改太多时,通常会首选这种方法。

Second (without adding the NULL element), you can limit your loop using a better test: 其次(不添加NULL元素),您可以使用更好的测试来限制循环:

for (i = 0; i < sizeof (HexColors) / sizeof (*HexColors); i++) ...

That expression sizeof (HexColors) / sizeof (*HexColors) gives you the number of array elements in HexColors . 表达式sizeof (HexColors) / sizeof (*HexColors)给出了HexColors中数组元素的HexColors


As an aside, the reason you're seeing both arrays output is because of how they're laid out in memory. 顺便说一句,您看到两个数组输出的原因是因为它们在内存中的布局方式。 The ColorDescription array immediately follows HexColors so it's as if that's just one array according to your slightly awry loop. ColorDescription数组紧随HexColors因此根据您稍有偏差的循环,就好像这只是一个数组。

Then, following ColorDescription is a pointer (or arbitrary value interpreted as a pointer) that causes the memory fault. 然后,紧随ColorDescription是导致内存故障的指针(或解释为指针的任意值)。

                 +---------+
HexColors        | pointer | --> "FFFFFF" (all nul-terminated
                 | pointer | --> "FF0000"  character arrays)
                 | pointer | --> "000000"
                 +---------+
ColorDescription | pointer | --> "white"  (ditto)
                 | pointer | --> "red"
                 | pointer | --> "black"
                 +---------+
ArbitraryMemory  | ??????? | --> ???????  (except here, which
                 +---------+               could be anything)

Change your while (HexColors[i]) condition. 更改您的while (HexColors[i])条件。

 while (i < arrayCount)

to avoid index 3 beyond bounds . 避免index 3 beyond bounds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM