c - Entab 程序意外错误(读访问冲突)

标签 c access-violation

我使用调试器来查找我的错误。我能够找到错误是什么。但我不知道为什么会发生。错误在于,在完成 1 次 push 后,linep 变成了一些垃圾地址(添加了 1 个空格)。因此,在第二次push时,它在这一行给了我一个错误:tmp1 = tmp2 = *x;因为linep是一些垃圾地址(我猜测)。我不知道为什么 linep 变成垃圾地址。我希望有人能解释一下。这是代码:

注意:该程序的目的是将每个制表符替换为 4 个空格。

#include <stdio.h>

#define MAXLINESIZE 1000
#define TABSPACES 4

void push(char *x, char val, int index);
int mgetline(char *s, int lim);

int main(void) {
    char line[MAXLINESIZE];
    char *linep = line;
    while (mgetline(line, MAXLINESIZE) > 0) {
        while (*linep) {
            if (*linep == '\t') {
                *linep = ' ';
                for (int i = 1; i <= TABSPACES - 1; i++) {
                    push(line, ' ', linep - line + 1);
                    linep++;
                }
            }
            linep++;
        }
        printf("%s", line);
        linep = line;
    }
    return 0;
}

void push(char *x, char val, int index) {
    char tmp1, tmp2, cnt;
    char *ptc;
    x += index - 1;
    ptc = x;
    tmp1 = tmp2 = *x;
    cnt = 0;
    while (tmp2) {
        tmp2 = *x;
        *x = tmp1;
        tmp1 = *(x + 1);
        *(x + 1) = tmp2;
        x += 2;
        cnt++;
    }
    *ptc = val;
}

int mgetline(char *s, int lim)
{
    int c;
    char *t = s;

    while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
        *s++ = c;
    if (c == '\n')
        *s++ = c;

    *s = '\0';

    return s - t;
}

注意:在输入中,. 等于制表符(09 十六进制 ASCII 代码),* 等于空格(20 十六进制 ASCII 代码)。

输入:

a.b.c.d*e.f**.g****h.j*.k**l

最佳答案

您需要设置循环以将指针正确复制到新的缓冲区中,该缓冲区将允许将空格放入字符串中而不覆盖实际输入。您需要确保多余的空格不会溢出该行。您还需要确保 EOL 已投入使用。

假设推送有效,那么您实际上是将线推到右侧,这可能会超出线阵列的最大大小。您也不返回新指针,这意味着您将测试额外的空格以及每次移动行数组的全部内容。您最好将其分别作为输入和输出线处理。

如果您坚持使用推送代码,请使行足够长,以便插入额外的空格,并在循环内添加linep = line;。计算原始行大小(使用 strlen()),将其增加所添加的空格总数,并进行测试以确保它不会溢出数组(将设置为大于 MAXLINESIZE)。从push返回新指针(在空格之后),这样您就不会检查在循环中添加的所有空格。

我建议不要尝试“修复”代码,而是重做它以提高效率。在我展示我的建议后,我将展示您的原始代码,并附上评论指出错误。

int main(void) {
    char line[MAXLINESIZE];
    char *linep = line;
    char newline[MAXLINESIZE];
    char *newlinep = newline;
    while (mgetline(line, MAXLINESIZE) > 0) {
        linep = line;
        newlinep = newline;
        while (*linep) {
            if (*linep == '\t') {
                for (int i = 0; i <= TABSPACES - 1; i++) {
                    *newlinep = ' ';
                    newlinep++;
                }
                if ((newlinep - &newline) >= MAXLINESIZE) {
                    printf("Line filled, need to stop");
                    newlinep = &newline[MAXLINESIZE-1];
                    break;
            }
            else {
                *newlinep = *linep;
                newlinep++;
            }
            linep++;
        }
        *newlinep = '\0'
        printf("%s", newline);
    }
    return 0;
}

这是您的原始代码,其中包含我对需要更改的内容的评论

char *push(char *x, char val, int index);
int mgetline(char *s, int lim);

int main(void) {
    // Make line big enough to hold the extra spaces
    char line[3*MAXLINESIZE];
    char *linep = line;
    while (mgetline(line, MAXLINESIZE) > 0) {
        // You need to reset the line pointer here
        linep = line;
        while (*linep) {
            if (*linep == '\t') {
                *linep = ' ';
                for (int i = 1; i <= TABSPACES - 1; i++) {
                    // Return the pointer to put after the spaces
                    // You also need to test to avoid overrun
                    linep = push(line, ' ', linep - line + 1);
                }
            }
            linep++;
        }
        printf("%s", line);
        linep = line;
    }
    return 0;
}

char * push(char *x, char val, int index) {
    // You need to check to make sure you do not overrun x.
    char tmp1, tmp2, cnt;
    char *ptc;
    x += index - 1;
    ptc = x;
    tmp1 = tmp2 = *x;
    cnt = 0;
    while (tmp2) {
        tmp2 = *x;
        *x = tmp1;
        tmp1 = *(x + 1);
        *(x + 1) = tmp2;
        x += 2;
        cnt++;
    }
    *ptc = val;
    return ptc;
}

关于c - Entab 程序意外错误(读访问冲突),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41620558/

相关文章:

在 C 中清除 char 数组

c - 进程返回 -1073741819 fscanf

c++ - 调用 glBufferData() 时随机 "unhandled exception"

在 Bison 中找不到 'syntax error' 消息的原因

c - 递归 C mergesort 在使用管道/叉读取时挂起

检查浮点变量是否存在

c - 获取段错误

JavaFX EXCEPTION_ACCESS_VIOLATION

c# - 为非托管代码提供托管控制句柄 - 访问冲突

python 非托管 dll 调用在 win32 FindWindow api 上失败并出现访问冲突