c - 尝试交换字符串内容时出现段错误 11

标签 c string char segmentation-fault swap

所以我试图创建一个文本缓冲区,它将给定文本的行存储到列表中。我目前正在编写一个交换函数,但是如果我尝试调整某些行,则会出现段错误。

这是我当前使用的交换函数:

void swapTB (TB tb, int pos1, int pos2) {

if (tb == NULL) {
    abort();
} 

if (pos1 < 0 || pos1>tb->size || pos2<0 || pos2>tb->size) {
    printf("error: lines are out of range!\n");
    abort();
}

link line1 = findLine(tb, pos1);
link line2 = findLine(tb, pos2);

char *temp = (char *)malloc((strlen(line1->line) + 1) * sizeof(char));
strcpy(temp,line1->line);
strcpy(line1->line, line2->line);
strcpy(line2->line,temp);

free(temp);

}

如果我尝试输入此文本:

char text[] = "0. She walks in beauty, like the night\n"
              "1. Of cloudless climes and starry skies;\n"
              "2. And all that’s best of dark and bright\n"
              "3. Meet in her aspect and her eyes;\n"
              "4. Thus mellowed to that tender light\n"
              "5. Which heaven to gaudy day denies.\n"
              "6. One shade the more, one ray the less,\n"
              "7. Had half impaired the nameless grace\n"
              "8. Which waves in every raven tress,\n"
              "9. Or softly lightens o’er her face;\n"
              "10. Where thoughts serenely sweet express,\n";

并进行以下交换:

swapTB(tb, 4, 7);
swapTB(tb, 2, 9);
swapTB(tb, 2, 10);

它工作正常,我得到这个输出:

0. She walks in beauty, like the night
1. Of cloudless climes and starry skies;
10. Where thoughts serenely sweet express,
3. Meet in her aspect and her eyes;
7. Had half impaired the nameless grace
5. Which heaven to gaudy day denies.
6. One shade the more, one ray the less,
4. Thus mellowed to that tender light
8. Which waves in every raven tress,
2. And all that’s best of dark and bright
9. Or softly lightens o’er her face;

但是,如果我稍微更改一下文本,例如缩短第 9 行:

char text[] = "0. She walks in beauty, like the night\n"
              "1. Of cloudless climes and starry skies;\n"
              "2. And all that’s best of dark and bright\n"
              "3. Meet in her aspect and her eyes;\n"
              "4. Thus mellowed to that tender light\n"
              "5. Which heaven to gaudy day denies.\n"
              "6. One shade the more, one ray the less,\n"
              "7. Had half impaired the nameless grace\n"
              "8. Which waves in every raven tress,\n"
              "9. Or softly lightens o’er\n"
              "10. Where thoughts serenely sweet express,\n";

我收到段错误 11。

这种情况一直在发生,有时工作正常,有时出现段错误,有时运行但最终输出有重复 - 这一切都取决于我为函数提供的文本。

顺便说一句,文本存储在这样的结构中:

typedef struct textNode *link;

struct textNode {
    char *line;
    link next;
};

struct textbuffer{
    link head;
    link last;
    int size;
}; 

编辑:

分配换行符的函数:

link newLine(char text[], int start, int i) {

    link newLine = malloc(sizeof(*newLine));
    assert(newLine != NULL);

    newLine->line = extract(text, start, i);
    //printf("newline is %s\n", newLine->line);
    newLine->next = NULL;

    return newLine;

}

提取函数

char* extract(const char* src, int start, int end) {

    return strndup(src + start, (end - start)+1);
}

findLine函数

link findLine(TB tb, int pos) {

    link curr = tb->head;
    int index = 0;

    while (index != pos) {
        curr = curr->next;
        index++;
    }

    return curr;
}

最佳答案

您必须正确编写交换操作。而不是这段代码

char *temp = (char *)malloc((strlen(line1->line) + 1) * sizeof(char));
strcpy(temp,line1->line);
strcpy(line1->line, line2->line);
strcpy(line2->line,temp);

应该有

char *temp  = line1->line;
lin1->line  = line2->line;
line2->line = temp;

如您所见,无需复制字符串本身。交换指针就足够了。

考虑到将数据成员 size 定义为类型 int 是没有任何意义的,然后每次都检查它是否小于零。如果将 size 定义为 size_t

类型,那就更好了

关于c - 尝试交换字符串内容时出现段错误 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27578610/

相关文章:

c - C 中随机名称按字母顺序排序

c - 如何添加两个字符串?

javascript - 在 JavaScript 中从 chrome.storage.local 中删除以字符串开头的变量

c - strlen() 如何计算当前函数中未定义的字符串的长度?

c++ - 将字符转换为宽字符

c++ - 函数返回 void 的无序函数评估

c - 管道中等待的字节

string - 如何从 Rust 中的另一个字符串中删除单个尾随字符串?

java - 如何遍历二维字符数组在java中搜索单词?

c - 如何复制具有 const unsigned char * 类型的值?