strncpy 段错误
我一直无法让这部分代码正常工作。我试图获取要复制的字符数组,以便可以计算有多少令牌可以动态分配和保存它们以检查环境变量。但是,当它尝试 strncpy 原始字符串时,我不断出现段错误。
void echo(char *str1)
{
char *token, *temp;
char *saveptr1;
int j, i, k, counter;
char *copy;
strncpy(copy, str1, 80);
const char *delim = " ";
i = strlen(copy);
for(j = 0; j < i; j++, copy = NULL)
{
token = strtok_r(copy, delim, &saveptr1);
counter++;
if(token == NULL)
{
counter--;
break;
}
}
// initialize token array for echo
char *tokAr[counter];
for(j = 0; j < counter; j++)
tokAr[j] = malloc(80*sizeof(char));
for(j = 0, k = 0; j < i; j++, str1 = NULL)
{
tokAr[k] = strtok_r(str1, delim, &saveptr1);
if( tokAr[k] != NULL)
{
if(strchr(tokAr[k], 36) != NULL)
{
temp = enviro(tokAr[k]);
printf("%s ", temp);
}
else
printf("%s ", tokAr[k]);
}
else
break;
}
for(k = 0; k < counter; k++)
free(tokAr[k]);
}
char* enviro(char *ret)
{
char *copy, *expand, *saveptr;
const char *delim = "$";
strcpy(copy, ret);
expand = strtok_r(copy, delim, &saveptr);
return getenv(expand);
}
我知道这与我如何复制传入的 str1 字符数组有关,但我无法从 gdb 中找出它。非常感谢任何帮助
I've been having trouble getting this section of code to work. I'm trying to get a character array to be copied so I can get a count of how many tokens there are to dynamically allocate and save them to be examined for environment variables. However, I keep segfaulting when it tries to strncpy the original string.
void echo(char *str1)
{
char *token, *temp;
char *saveptr1;
int j, i, k, counter;
char *copy;
strncpy(copy, str1, 80);
const char *delim = " ";
i = strlen(copy);
for(j = 0; j < i; j++, copy = NULL)
{
token = strtok_r(copy, delim, &saveptr1);
counter++;
if(token == NULL)
{
counter--;
break;
}
}
// initialize token array for echo
char *tokAr[counter];
for(j = 0; j < counter; j++)
tokAr[j] = malloc(80*sizeof(char));
for(j = 0, k = 0; j < i; j++, str1 = NULL)
{
tokAr[k] = strtok_r(str1, delim, &saveptr1);
if( tokAr[k] != NULL)
{
if(strchr(tokAr[k], 36) != NULL)
{
temp = enviro(tokAr[k]);
printf("%s ", temp);
}
else
printf("%s ", tokAr[k]);
}
else
break;
}
for(k = 0; k < counter; k++)
free(tokAr[k]);
}
char* enviro(char *ret)
{
char *copy, *expand, *saveptr;
const char *delim = "$";
strcpy(copy, ret);
expand = strtok_r(copy, delim, &saveptr);
return getenv(expand);
}
I know it has something to do with how I copy the passed in str1 character array but I can't figure it out from gdb. Any help is greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尚未为
复制
分配内存。如果不需要完整的 81 个字符,请尝试
malloc
或strdup
。You haven't allocated memory for
copy
.Try
malloc
orstrdup
if you don't need the full 81 characters.copy
不包含有效的分配地址。在使用copy
之前,请使用malloc
分配足够的内存。还请记住在使用完毕后释放copy
,以阻止较大程序中的内存泄漏。copy
does not contain a valid allocated address. Please allocate enough memory withmalloc
before usingcopy
. Also remember to freecopy
after you have completed using it to stop memory leak in larger programs.我认为在函数 echo 中,您尚未初始化变量计数器并尝试递增和递减它。
尝试这样做。
I think in function echo, you haven't initialized the variable counter and trying to incrementing and decrementing it.
Try to do that.