c - 为什么在 C 中的 char 指针之间传输字符串时我的程序会崩溃?

标签 c algorithm pointers char memcpy

我现在在 c 语言中遇到 memcpy() 问题,希望有人能提供帮助。

我的程序允许用户将字符串输入到字符指针中,然后计算所有可能的排列。当生成排列时(用户输入指针更改为排列),排列通过 memcpy 复制到第二个 char 指针中。它工作得很好,除非字符串有两个或多个不同的重复字符(例如“CCBB”或“AADD”)。如果用户输入类似的内容,memcpy(甚至 strcpy)会导致程序崩溃。

void Permute(char * word, char ** printPerm, int start, int end)
{   
    if (start == end)
    {
        memcpy(printPerm[permIndex], word, strlen(word) + 1);
        ++permIndex;
    }
    else
    {
        for (int i = start; i <= end; ++i)
        {
            Swap((word + start), (word + i));
            Permute(word, printPerm, start + 1, end);
            Swap((word + start), (word + i));
        }
    }
}

void Swap(char *a, char *b)
{
    char temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

我尝试为两个指针分配更多内存,但事实证明这是徒劳的。除了这个之外,其他一切都有效。

因为我在 Windows (MinGW) 上使用 gcc,所以不会显示崩溃的详细信息。它只是说“perm.exe 已停止工作”。我使用了一系列 printf() 语句,发现程序在 memcpy() 行崩溃。

有关代码的一些细节:

“word”字符指针保存用户的输入。它将被程序转变为排列,并且其内容将被转储到“printPerm”中。 “printPerm”是保存排列的字符指针数组,稍后将用于在按字母顺序排序并且删除任何重复条目时打印排列。 “permIndex”是“printPerm”的索引,每次将排列添加到“printPerm”时都会迭代。

抱歉,我没有更多详细信息,但使用文本编辑器和 gcc 意味着我没有太多调试器。看来只有当字符串包含两个或多个不同的重复字符时,在指针之间传输数据的任何方法都会使程序崩溃。

最佳答案

你很幸运:我的 Crystal 球刚刚修好回来!让我们看看它现在是否有效:

// ALL CHECKS OMMITTED!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int permIndex;

// place the two functions you published here

// Uhmmm...well...but we need one
int factorial(int n)
{
  int f = 1;
  do {
    f *= n;
  } while (--n);

  return f;
}

int main(int argc, char **argv)
{
  int f,i;
  char **pperm;
  char *word;
  size_t length;

  if (argc < 2) {
    fprintf(stderr, "Usage: %s string\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  // work on copy
  length = strlen(argv[1]);
  word = malloc(length + 1);
  strcpy(word, argv[1]);

  // You either allocate memory as you need it but as you compute
  // all combinations first, you need the whole memory for them
  // at once. That means the amount of memory needed is known in
  // advance and can be allocated at once

  f = factorial((int) length);
  // allocate memory for an array of (length)! pointers to char
  pperm = malloc(f * sizeof(char *));
  for (i = 0; i < f; i++) {
    // allocate memory for the n characters plus '\0'
    pperm[i] = malloc(length + 1);
  }

  Permute(word, pperm, 0, length - 1);

  // do something with the list

  // print it
  for (i = 0; i < f; i++) {
    printf("%s\n", pperm[i]);
    // we don't need the memory anymore: free it
    free(pperm[i]);
  }
  // free that array of pointers mjentioned above
  free(pperm);
  // free the memory for the input 
  free(word);

  exit(EXIT_SUCCESS);
}

这是实现此目的的几种方法之一。

关于c - 为什么在 C 中的 char 指针之间传输字符串时我的程序会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38882141/

相关文章:

c - 从服务器向客户端发送消息

arrays - 在所有元素来自集合 {1, 2, …, n} 的数组中,哪个数组的反转次数最多?它有多少个反转?

algorithm - 购买策略的动态规划

c - 无法将字符指针传递到函数中

c - 多维数组C

c - Sonar C 插件——包含文件中的解析器错误

algorithm - "dynamic"编程与 "normal"编程有何不同?

c - 访问数组元素时指针的索引

c++ - 使用双指针时发生访问冲突

c - 在动态分配的内存上使用指针算法的未定义行为