重新分配后,我得到一个断点。为什么?我该怎么做呢?

时间:2018-12-22 10:15:59

标签: c dynamic breakpoints realloc

该计划的重点是用我的名字代替每 2个或更多人声。 如何使重新分配成功?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main() {
    char vocale[] = "AEIOUaeiou", 
            sir[] = "Aana are muaulte meiree.";
    char *src = (char*)malloc(strlen(sir) + 1);
    src = sir;
    char name[] = "Marian";
    int count = 0, 
            i = 0;
    while (i < strlen(src)) {
        if (strchr(vocale, src[i])) {
            count++;
            i++;
        }
        else {
            if (count >= 2) {
                src = (char*)realloc(src, strlen(src) + strlen(name) + 1);
                insereaza(src, count, name, i);
                i = i + strlen(name);
                count = 0;
            }
            else {
                count = 0;
                i++;
            }
        }
    }
    puts(src);
    _getch();
}

1 个答案:

答案 0 :(得分:0)

这是一个有一些更改的建议,很难,因为我不知道insereaza的作用

void main() {
  const char * vocale = "AEIOUaeiou";
  char * src = strdup("Aana are muaulte meiree."); /* must be in the heap for realloc */
  const char * name = "Marian";
  int srcLen  = strlen(src);
  int nameLen = strlen(name);
  int count = 0, i = 0;

  while (i < srcLen) {
    if (strchr(vocale, src[i])) {
      count++;
      i++;
    }
    else {
      if (count >= 2) {
        src = (char*) realloc(src, srcLen + nameLen + 1); /* perhaps too large */
        insereaza(src, count, name, i);
        srcLen += nameLen; /* or srcLen += nameLen - count + 1 ? */
        i += nameLen; /* or i += nameLen - count + 1 ? */
      }
      else {
        i++;
      }
      count = 0;
    }
  }
  puts(src);
  _getch();
}