0

我有一个令人沮丧的问题,我找不到答案。

我有这个功能:

// Append character to the end of a string
void str_AppendChar(char* s, const char* ch)
{
  // +2 because of 2x '\0'
  char* buff = malloc(strlen(s)+strlen(ch)+2);
  memset(buff, 0, sizeof(buff));

  // Copy the whole string in buff
  strcpy(buff, s);

  // Append ch at the end of buff
  int len = strlen(buff);
  strcpy(buff+len, ch);

  // Set end of the string
  *(buff+strlen(buff)-3) = '\0';

  strcpy(s, buff);

  free(buff);
}

由于某种原因,我的程序最后尝试执行两次 free 。

我使用 AppendChar() 的代码是:(有点难看,但请耐心等待)

void it_GatherCmd(cmd_Details* lineptr[], char* cmd)
{
  // Used to count number of rows in lineptr
  int nlines;

  Detailptr p;
  char ch;
  char* word = (char*)malloc(sizeof(char)+256);
  memset(word, 0, sizeof(word));

  nlines = 0;
  while ((ch = *cmd++) != '\n')
  {
      if (ch != ' ' && ch != '\0' )
          str_AppendChar(word, &ch);
      else
      {
          int type = dict_CheckWord(word);

          if (type != -1)
          {
              p = it_CopyInfo(word, type);
              lineptr[nlines++] = p;
          }
          memset(word, 0, sizeof(word));
      }
   }
   //EDIT*
   free(word);
}

而我的主要:

int main()
{
   cmd_Details* arrCmd[MAXLINES];
   char* str = "just some string";

   it_GatherCmd(arrCmd, str);

   printf("%s", str);
   return 0;
}

在我创建 it_GetCharCmd() 并在那里使用它之前,AppendChar() 一直没有问题。我在这上面花了大约 3 个小时,但我找不到问题所在。在互联网上进行了一些搜索,但我发现的东西与我的问题并不完全相关。

4

2 回答 2

0

这段代码有一些问题。

首先,如果str_AppendChar实际上附加了一个字符,正如其名称所暗示的那样,为什么要给它一个const char*暗示 C 字符串的字符?在这里传递指针而不是实际对象的增益为零,就像某些结构一样;实际上,您仍然需要将 4 个字节推入堆栈。

其次,正如我在评论中指出的那样,问题在于您没有正确初始化分配的缓冲区-sizeof(buff)返回良好,buff的大小和buffchar*很可能是4。虽然只是更改sizeof(buff)strlen(s)+strlen(ch)+2,即你实际分配了多少内存解决了这个问题(因为sizeof(buff)可能比你实际分配的更多,你写的内存已经过去了),我建议像这样简化函数:

// Append character to the end of a string
void str_AppendChar(char* s, char ch)
{
    size_t sLen = strlen(s);
    char* buff = (char*)malloc(sLen + 2);       // 1 for the appended char, 1 for \0
    //memset(buff, 0, sLen + 2);   //not necessary, we'll overwrite the memory anyway

    // Copy the whole string in buff
    strcpy(buff, s);

    // append our char and null-terminate
    buff[sLen] = ch;
    buff[sLen + 1] = '\0';

    strcpy(s, buff);

    free(buff);
}

请注意,此代码仍然很糟糕;它很高兴地假设 s 将足够大以容纳一个额外的字符,但情况并非总是如此。

还有关于你的 it_gatherCmd 函数;它应该采取 aconst char*因为它不会以任何方式修改它(实际上,你调用它的方式是给它一个 const char*;修改字符串文字是未定义的行为,在 Windows 上你可能会由于违反页面权限而崩溃)。

于 2015-05-16T10:36:13.010 回答
0

据我所知,您在扫描命令时连续构建了一个字符串。附加字符时,绝对不需要复制要附加到两次的字符串。您正在有效地做的是:

void str_AppendChar(char* s, char ch)
{
    int len = strlen(buff);

    s[len++] = ch;
    s[loen] = '\0';
}

请注意,您strlen每次都确定字符串长度,这将遍历整个字符串。更糟糕的是,您没有关于最大可用缓冲区大小的任何信息,因此尽管您进行了所有分配、复制和释放,但原始字符串可能会溢出。

代替

str_AppendChar(word, &ch);

在自动内存中使用本地缓冲区:

char word[20];
int wlen = 0;

并像这样追加:

if (wlen + 1 < sizeof(word)) word[wlen++] = *cmd;

这将使单词缓冲区未终止,因此在使用它之前,附加它:

word[wlen] = '\0';
printf("next word: '%s'\n", word);

(当然,您也可以确保字符串始终以空值结尾。)

当您为新单词重置缓冲区时,不需要memset整个缓冲区;只需重置wlen为零。

于 2015-05-16T10:59:07.983 回答