strncpy 段错误

发布于 2024-12-06 06:06:16 字数 1469 浏览 0 评论 0原文

我一直无法让这部分代码正常工作。我试图获取要复制的字符数组,以便可以计算有多少令牌可以动态分配和保存它们以检查环境变量。但是,当它尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深缘浅 2024-12-13 06:06:16

您尚未为复制分配内存。

char *copy;
strncpy(copy, str1, 80);

如果不需要完整的 81 个字符,请尝试 mallocstrdup

copy = malloc(81);
strncpy(copy, str1, 80);

/* Or strdup. */
copy = strdup(str1);

You haven't allocated memory for copy.

char *copy;
strncpy(copy, str1, 80);

Try malloc or strdup if you don't need the full 81 characters.

copy = malloc(81);
strncpy(copy, str1, 80);

/* Or strdup. */
copy = strdup(str1);
小ぇ时光︴ 2024-12-13 06:06:16

copy 不包含有效的分配地址。在使用copy之前,请使用malloc分配足够的内存。还请记住在使用完毕后释放copy,以阻止较大程序中的内存泄漏。

copy does not contain a valid allocated address. Please allocate enough memory with malloc before using copy. Also remember to free copy after you have completed using it to stop memory leak in larger programs.

握住我的手 2024-12-13 06:06:16

我认为在函数 echo 中,您尚未初始化变量计数器并尝试递增和递减它。
尝试这样做。

I think in function echo, you haven't initialized the variable counter and trying to incrementing and decrementing it.
Try to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文