#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
const char* dog = "german shepard";
NSFileManager* fileManager = [NSFileManager defaultManager];
NSString *doglistPath = @"/Users/doglover/doglist.plist";
const char* doglistPath_cString = [doglistPath UTF8String];
const char** doglist;
if ([fileManager fileExistsAtPath:doglistPath]) {
NSArray *doglistPlist = [NSArray arrayWithContentsOfFile:doglistPath];
NSUInteger doglistPlistCount = [doglistPlist count];
const char* doglist2[blacklistPlistCount];
memset(doglist2, 0, sizeof doglist2);
for (int index = 0; index < doglistPlistCount; index++) {
const char *doglistName = [doglistPlist[index][@"type"] UTF8String];
doglist2[index] = doglistName;
}
doglist = doglist2;
}
while(true) {
NSLog(@"%s", *doglist);
doglist++;
}
return 0;
}
列表中有20个项目。
[每当我运行此代码时,它仅打印4项并给出线程1:EXC_BAD_ACCESS错误。
当我检查内存时,当运行时离开if语句时,doglist内存中的指针从:变为:
B1 78 10 03 01 00 00 00
51 7B 10 03 01 00 00 00
01 7D 10 03 01 00 00 00
A1 7E 10 03 01 00 00 00
91 80 10 03 01 00 00 00
F8 74 10 03 01 00 00 00
11 83 10 03 01 00 00 00
C1 84 10 03 01 00 00 00
to
B1 78 10 03 01 00 00 00
51 7B 10 03 01 00 00 00
01 7D 10 03 01 00 00 00
A1 7E 10 03 01 00 00 00
40 00 00 00 00 00 00 00
90 ED BF EF FE 7F 00 00
0E 00 B6 D1 69 DA B9 2C
00 00 00 00 00 00 00 00
在更改的内存中,前四个指针确实包含这些项目,但是在0x40处发生错误。
为什么退出if语句后内存发生变化?
一些注意事项: