简体   繁体   English

免费的字符串数组给SIGABRT

[英]free an array of strings gives SIGABRT

I am trying to free an array of strings in this manner: 我正在尝试以这种方式释放字符串数组:

unsigned char **strings = words.strings;
for (int i = 0; i < noOfWords; ++i) {
    free(strings + i);
}

but I get a SIGABRT (error code 134) with the message: 但是我收到一条消息SIGABRT (错误代码134):

free(): invalid pointer: 0x0000000001c69018 free():无效的指针:0x0000000001c69018

However if I do this: 但是,如果我这样做:

unsigned char **strings = words.strings;
for (int i = 0; i < noOfWords; ++i) {
    free(strings[i]);
}

then everything works just fine. 然后一切正常。

Can someone point me the difference? 有人可以指出我的不同吗? Shouldn't these two forms be equivalent? 这两种形式不应该等效吗?

In your first snippet, you missed to dereference the pointer. 在第一个代码段中,您错过了对指针的取消引用。

free(strings + i);

should be 应该

free(*(strings + i));

In case of free(strings + i); free(strings + i);情况下free(strings + i); you are trying to increment the pointer-to-pointer, ie, the string itself. 您正在尝试增加指针到指针,即string本身。 Actually, what you want is to free each valid element pointed by string which are given by string[i] . 其实,你想要的是,以释放指出每个有效的元素string它们由给定的string[i] So, string[i] is basically de-referencing the pointer string to get all the elements pointed by it. 因此, string[i]基本上是取消引用指针string以获取其指向的所有元素。

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

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