C - 进程退出,错误代码为 3221225477

标签 c windows shift-reduce

我正在为我们的编译器设计主题做一个 shift-reduce 算法。这是代码。

void shiftReduce(char str[MAX_CHAR], int prodNum, int line)
{
    int limit = 5, y=0;
    int substrFlag = 1; //0 true 1 false
    int ctr,x, counter;
    int match, next;
    char stack[MAX_CHAR];
    clearString(stack);
    OUTER:while ((strcmp(stack, prod[0].left) != 0) && (y < limit))
    {
        addChar(stack, str[0]);
        strcpy(str, dequeue(str));
        printf("Stack = %s\nQueue = %s\n", stack, str);
        for (ctr = 0; ctr < prodNum; ctr++)
        {
            if (strstr(stack, prod[ctr].right) != NULL)
            { //substring found
                substrFlag = 0;
                strcpy(stack, replace(stack, prod[ctr].right, prod[ctr].left));
                goto OUTER;
            }
        }
        if ((str[0] == '\n') || (str[0] == '\0'))
            y++;
    }
    if (strcmp(stack, prod[0].left) == 0)
        ;//printf("%s - Accepted.\n", stack);
    else
        printf("Syntax error on line %i\n", line);
}

当我评论 printf("Stack = %s\nQueue = %s\n", stack, str); 行时,它运行良好。但是当我取消注释时,它返回代码 3221225477

顺便说一句。这是出列函数:

char * dequeue (char str[MAX_CHAR])
{
    int x = 0; char temp;
    for (x = 0; x < length(str); x++)
    {
        if ((x+1) < length(str))
            str[x] = str[x+1];
    }
    return str;
}

和 addChar 函数:

void addChar (char * str, char letter)
{
    int a = 0;
    while (str[a] != '\0')
        a++;
    str[a] = letter;
    str[a+1] = '\0';
    return;
}

最后替换函数。

char * replace (char orig[MAX_CHAR], char substr[MAX_CHAR], char rep[MAX_CHAR])
{
    int match, end=0, next=0;
    int flag = 0; //0 true 1 false
    char temp [MAX_CHAR];
    char store[MAX_CHAR];
    if (strstr(orig, substr) == NULL)
        return NULL;
    int x,y;
    for (x = 0; x < length(orig); x++)
    { 
        if (orig[x] == substr[0]) //if current character  is equal to first character of substring
        {
            match = x;
            for (y = 0; y < length(substr); y++)
            { 
                if (orig[match+y] != substr[y])
                {
                    flag = 1;
                    break;  
                }
            }
            if (flag == 0)
            {
                next = match + length(substr);
                for (y = 0; y < length(rep); y++)
                { 
                    temp[match+y] = rep[y]; 
                    end = (match+y);
                }
                for (y = next; y < length(orig); y++)
                { 
                    temp[y] = orig[next+(y-next)];
                }
                return temp;
            }
        }
        else
        {
            addChar(temp, orig[x]);
        }
    }
    return temp;
}

附言。 prod 数组:

struct RULES
{
    char left[MAX_CHAR];
    char right[MAX_CHAR];
} RULES;
struct RULES prod[MAX_RULES];

最佳答案

When I comment the printf("Stack = %s\nQueue = %s\n", stack, str); line, it works well. But when I uncomment it, it returns the code 3221225477.

那么很可能 stackstr 尚未被 0 终止或指向无效内存。

关于C - 进程退出,错误代码为 3221225477,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38419321/

相关文章:

objective-c - 在什么情况下我们需要 double、float 和 long double 的函数?

c - 在什么情况下会在 for 循环终止条件中使用位移运算符?

c - Blowfish CBC 模式和 C 中的缓冲区溢出

windows - 检查是否启用或禁用本地用户

parsing - Shift-reduce:什么时候停止减少?

c - 检查目录是否存在的便携方法 [Windows/Linux, C]

windows - 获取文件名前的部分字符串作为目录

windows - R 脚本自动化时的不同结果

parser-generator - 在一个小的柠檬语法中解决解析冲突