Realloc和strcpy
我的代码告诉我segfault错误:我不明白,调试器说错误来自打印存储的值Realloc和strcpy,c,segmentation-fault,realloc,C,Segmentation Fault,Realloc,我的代码告诉我segfault错误:我不明白,调试器说错误来自打印存储的值 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,strl
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]);
}
char*storaged_uuz=NULL;
char testMessage[15];
//strcpy(存储、测试消息);
对于(int a=0;a<10;a++)
{
sprintf(testMessage,“Message::%i\n”,a);
printf(“字符串是:%s;长度是%i\n”,testMessage,strlen(testMessage));
存储的=(char*)realloc(存储的=,sizeof(char)*(strlen(testMessage)*(a+1));
strcpy(&storaged[a],testMessage);
}
对于(int b=0;b<10;b++)
{
printf(“内部:|%s |\n”,存储在[b]);
}
首先,sizeof(char)
始终为1,您不需要乘以它
其次,在为字符串分配空间时,必须使用:
malloc (strlen (string) + 1);
换句话说,您需要为结尾的空字节留出空间
第三,您似乎混淆了字符指针和字符指针<代码>存储的字符是单个字符块,存储的字符[1]
仅超出存储的字符[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字符串):
首先,sizeof(char)
始终为1,您不需要乘以它
其次,在为字符串分配空间时,必须使用:
malloc (strlen (string) + 1);
换句话说,您需要为结尾的空字节留出空间
第三,您似乎混淆了字符指针和字符指针<代码>存储的字符是单个字符块,存储的字符[1]
仅超出存储的字符[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字符串):
您似乎没有正确计算存储的
的字符串长度
将testMessage
分配给&存储的每个循环
。我不确定这是否是预期的行为,但这是您正在做的,因此我希望您的第十次迭代会给出字符串“mmmmmmm essage::9\n”
无论如何,testMessage
始终具有相同的字符数,因此storaged\uuu
所需的存储空间可以计算为:
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中的每个字符串都必须有用于的空间。您似乎没有正确计算存储的的字符串长度
将testMessage
分配给&存储的每个循环
。我不确定这是否是预期的行为,但这是您正在做的,因此我希望您的第十次迭代会给出字符串“mmmmmmm essage::9\n”
无论如何,testMessage
始终具有相同的字符数,因此storaged\uuu
所需的存储空间可以计算为:
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中的每个字符串都必须有空格。segfault出现在哪一行?您需要为结尾处的空终止字符('\0')添加额外的空格。segfault出现在哪一行?您需要为结尾处的空终止字符('\0')添加额外的空格。第四,它是%d,不是%i:)实际上,d
和i
至少在C99下同样有效。第四,它是%d,不是%i:)实际上,d和i
至少在C99下同样有效。