字符串动态数组成长 [英] Dynamically grown array of strings
问题描述
我试图建立一个字符串的动态数组成长。无论是串数,也不每个字符串的长度是在编译时已知。这里的code,我想出了到目前为止(这只是我的语法扮演):
I'm trying to build a dynamically grown array of strings. Neither the number of strings nor the length of each string is known at compile time. Here's the code I came up with so far (this is just me playing with the syntax):
char **result = NULL;
char *temp = NULL;
result = (char **)realloc (result,sizeof(char *) * 1);
temp= (char *)realloc(temp,5 * sizeof(char));
strcat(temp,"hello");
temp= (char *)realloc(temp,10 * sizeof(char));
strcat(temp," world");
printf ("%s \n", temp);
result[0]=temp;
free(temp);
printf ("%s \n", result[0]);
result = (char **)realloc (result, sizeof(char *) * 2);
temp= (char *)realloc(temp,10 * sizeof(char));
strcat(temp,"0123456789");
temp= (char *)realloc(temp,15 * sizeof(char));
strcat(temp,"asdfg");
printf ("%s \n", temp);
result[1]=temp;
free(temp);
printf ("%s \n", result[0]);
printf ("%s \n", result[1]);)
现在,当我打印结果[0],或导致[1],它只是一个空字符串,为什么不会导致[1] =温度;工作?
Now, when I print result[0] or result[1], its just an empty string, why doesn't result[1]=temp; work?
下面就是我刚才试过了,但它没有工作,我一直在使用最后一行的realloc()时得到无效的尺寸错误:
Here's what I tried earlier, but it didn't work, I kept getting "invalid size" errors when using realloc() on that last line:
char **result = NULL;
result = (char **)realloc (result,sizeof(char *) * 1);
result[0]= (char *)realloc(result[0],5 * sizeof(char));
strcat(result[0],"hello");
printf ("%s \n", result[0]);
result[0]= (char *)realloc(result[0],10 * sizeof(char));
strcat(result[0]," world");
printf ("%s \n", result[0]);
result = (char **)realloc (result, sizeof(char *) * 2);
result[1]= (char *)realloc(result[1],10 * sizeof(char));
strcat(result[0],"0123456789");
result[0]= (char *)realloc(result[1],15 * sizeof(char));
strcat(result[0],"asdfg");
如果有人可以帮助我得到任一版本的工作,我将非常感激。
If anybody could help me get either version working, I would be very grateful.
更新:好吧,我得到了code工作的两个版本。现在,当我尝试在我的实际程序中使用此相同的格式,我得到的错误,如
UPDATE: Ok, I got both versions of the code working. Now when I try to use this same format in my actual program, I get errors such as
*** glibc detected *** ./uvicfmt3: realloc(): invalid next size: 0x08ed3170 ***
现在在我的计划中,结果被声明为一个全局变量(用我的code的第二版),和realloc函数被调用不同的子程序。那是什么导致这个问题?我怎么能解决这个得到什么?
Now in my program, the "result" is declared as a global variable (using the second version of my code), and the realloc functions are called in different subroutines. Is that what's causing the problem? How could I get around this?
推荐答案
- 在第一个例子中,你使用一个字符串后
释放
是 - 在您
strcat的
ING未分配的内存中的第二个例子(加上没有受过教育的的realloc(结果[0]
是没有意义的)
- In the first example you are using a string after
freeing
it - In the second example you are
strcat
ing to unallocated memory (plus the uneducatedrealloc(result[0]
makes no sense)
您可以试试这个:
char **result = NULL;
result = realloc(result, sizeof(char *) * 1);
result[0] = strdup("hello");
/* ... */
result = realloc(result, sizeof(char *) * 2);
result[1] = strdup(" world");
现在的strdup
不是标准,但它并不难偷/捏造事实。这里有一个尝试:
Now strdup
isn't standard but it's not hard to steal it / fake it. Here's one attempt:
char *strdup2(const char *str)
{
size_t len;
char *rval = NULL;
len = strlen(str); /* We should probably check this. */
rval = malloc(len + 1); /* And this. */
memcpy(rval, str, len);
rval[len] = 0;
return rval;
}
修改
我以后要修改字符串(可能是错误的)IM pression下。如果不是这种情况,只需将它们存储(无的strdup
)就足够了:
result[0] = "hello";
这篇关于字符串动态数组成长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!