use*_*003 1 c realloc segmentation-fault
我的代码给了我段错误:我不明白,调试器说错误来自于从stored_打印值
char *stored_ = NULL;
char testMessage[15];
//strcpy(stored_, testMessage);
for (int a = 0;a < 10; a++)
{
sprintf(testMessage,"Message::%i\n",a);
printf("string is:%s;length is %i\n",testMessage,strlen(testMessage));
stored_ = (char*) realloc (stored_, sizeof(char) * (strlen(testMessage) * (a+1) ));
strcpy(&stored_[a], testMessage);
}
for (int b = 0;b < 10; b++)
{
printf("inside:|%s|\n",stored_[b]);
}
Run Code Online (Sandbox Code Playgroud)
拳心向上,sizeof(char)是永远 1,你不需要用它来繁殖.
其次,当你为字符串分配空间时,你必须使用:
malloc (strlen (string) + 1);
Run Code Online (Sandbox Code Playgroud)
换句话说,最后需要空字节.
第三,你似乎在字符指针和字符指针指针之间混淆.stored_是单个字符块,stored_[1]只有一个字节stored_[0],这意味着您没有足够的空间来存储字符串.
stored_[n], n=: 0 1 2 3
+---+---+---+---+
| | | | |...
+---+---+---+---+
each of these cells is a single byte.
Run Code Online (Sandbox Code Playgroud)
你要么必须自己管理单个字符块,为每个元素留下足够的空间(通过使用稀疏索引),或者有一个带有索引0,1,2等的字符指针块,但是你会那么必须分别管理字符串分配.
以下代码显示了如何执行后一个操作:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void) {
// An array of char pointers (C strings).
char **stored_ = NULL;
char testMessage[15];
int i;
// Populate them.
for (i = 0; i < 10; i++) {
sprintf (testMessage,"Message::%i",i);
printf ("string is:%s;length is %i\n",testMessage,strlen(testMessage));
// Reallocate array of char *, allocate room for string, then store it.
stored_ = realloc (stored_,sizeof (char*) * (i + 1));
stored_[i] = malloc (strlen (testMessage) + 1);
strcpy (stored_[i], testMessage);
}
Run Code Online (Sandbox Code Playgroud)
这就是它的核心,字符指针数组的分配与形成C字符串的实际字符数组分开.
然后下面的代码打印并清理.
// Print them.
for (i = 0; i < 10; i++) {
printf("inside:|%s|\n",stored_[i]);
}
// Free all memory and return.
for (i = 0; i < 10; i++) {
free (stored_[i]);
}
free (stored_);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出正如预期的那样:
string is:Message::0;length is 10
string is:Message::1;length is 10
string is:Message::2;length is 10
string is:Message::3;length is 10
string is:Message::4;length is 10
string is:Message::5;length is 10
string is:Message::6;length is 10
string is:Message::7;length is 10
string is:Message::8;length is 10
string is:Message::9;length is 10
inside:|Message::0|
inside:|Message::1|
inside:|Message::2|
inside:|Message::3|
inside:|Message::4|
inside:|Message::5|
inside:|Message::6|
inside:|Message::7|
inside:|Message::8|
inside:|Message::9|
Run Code Online (Sandbox Code Playgroud)
使用此方法,每个单元格都是一个指向字符数组的指针,单独分配(保存C字符串):
stored_[n], n=: 0 1 2 3
+---+---+---+---+
| | | | |...
+---+---+---+---+
| | | | +----------------------+
| | | +---> | character array here |
| | | +----------------------+
| | | +----------------------+
| | +-------> | character array here |
| | +----------------------+
| | +----------------------+
| +-----------> | character array here |
| +----------------------+
| +----------------------+
+---------------> | character array here |
+----------------------+
Run Code Online (Sandbox Code Playgroud)