我的代码给出了segfault错误:我不明白,调试器说错误来自于打印来自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]);
}发布于 2011-12-21 13:26:43
首先,sizeof(char)总是1,你不需要乘以它。
其次,当你为一个字符串分配空间时,你必须使用:
malloc (strlen (string) + 1);换句话说,您需要为末尾的空字节留出空间。
第三,你似乎混淆了字符指针和字符指针。stored_是一个单一的字符块,而stored_[1]只比stored_[0]大一个字节,这意味着您将没有足够的空间来存储字符串。
stored_[n], n=: 0 1 2 3
+---+---+---+---+
| | | | |...
+---+---+---+---+
each of these cells is a single byte.您必须自己管理单个字符块,为每个元素留出足够的空间(通过使用稀疏索引),或者使用索引为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);
}这就是问题的核心,字符指针数组的分配与构成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;
}正如预期的那样,输出是:
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|使用这种方法,每个单元格都是一个指向单独分配的字符数组的指针(它保存C字符串):
stored_[n], n=: 0 1 2 3
+---+---+---+---+
| | | | |...
+---+---+---+---+
| | | | +----------------------+
| | | +---> | character array here |
| | | +----------------------+
| | | +----------------------+
| | +-------> | character array here |
| | +----------------------+
| | +----------------------+
| +-----------> | character array here |
| +----------------------+
| +----------------------+
+---------------> | character array here |
+----------------------+发布于 2011-12-21 13:30:33
您似乎没有正确计算stored_的字符串长度。
每次循环都要将testMessage赋值给&stored_[loopindex]。我不确定这是否是有意为之的行为,但这是您正在做的,所以我希望您的第10次迭代会给出字符串"MMMMMMMMMessage::9\n"。
无论如何,testMessage总是相同的字符数,因此stored_所需的存储空间可以计算为:
strlen(testMessage) // length of str to place at &stored_[a]
+ a // the loop index, where you're inserting testMessage
+ 1 // important! extra char to hold the null terminator不要忘记+1,C中的每个字符串都必须为null terminator留出空间。
https://stackoverflow.com/questions/8585380
复制相似问题