标题:[新手]结束时报错-Run-Time Check Failure #2 #0
只看楼主
随风飘荡
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:208
专家分:598
注 册:2011-9-9
结帖率:100%
已结贴  问题点数:20 回复次数:5 
[新手]结束时报错-Run-Time Check Failure #2 #0
这两天看了指针和结构体之后弄的

求解一下,程序结束时的报错:Run-Time Check Failure #2 Stack around the variable 'list' was corrupted.是指栈被破坏吗,应该如何修改?看名字是list的问题,但我看了半天也找不出哪里的问题,为什么不会影响这个程序的运行,我百度Google了半天没找到。
还有一个:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
程序代码:
#include <stdio.h>

void input (struct student *,int *);
void output (struct student);
struct student
{
    char name[20];
    int age;
    int score;
    char ach;
    int stdid;
};

int main(void)
{
    struct student list[1];
    int stdidall = 0;

    input(&list[0],&stdidall);
    printf("Complete\n");
    input(&list[1],&stdidall);
    printf("Complete\n");

    printf("--------------------------\n");
    output(list[0]);
    output(list[1]);
    return 0;
}

void input (struct student * temp , int * tempall)
{
    printf("Enter Student Name:");
    scanf("%s",temp->name);
    printf("Enter Student Age:");
    scanf("%d",&temp->age);
    printf("Enter Student Score:");
    scanf("%d",&temp->score);

    if (90 <= temp->score)
        temp->ach = 'A';
    else if (70 <= temp->score)
        temp->ach = 'B';
    else if (60<= temp->score)
        temp->ach = 'C';
    else if (50<= temp->score)
        temp->ach = 'D';
    else
        temp->ach = 'F';
    *tempall = *tempall + 1;
    temp->stdid = *tempall;

    return;
}

void output (struct student temp)
{
    printf("Name:%s\nAge:%d\nScore:%d\nAchievement:%c\nNo.:%d\n", temp.name , temp.age , temp.score , temp.ach , temp.stdid);
    return;
}

搜索更多相关主题的帖子: 如何 影响 function declared properly 
2011-10-15 17:11
nomify
Rank: 5Rank: 5
等 级:职业侠客
帖 子:79
专家分:366
注 册:2011-10-13
得分:10 
struct student list[1];  //list[2];
2011-10-15 17:19
随风飘荡
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:208
专家分:598
注 册:2011-9-9
得分:0 
这这这.....我.....貌似我一直想着数组从0开始.....

不过这里我定义成1的话不就只有1个元素了吗

那 input(&list[1],&stdidall);又是怎么执行的呢?
2011-10-15 17:27
nomify
Rank: 5Rank: 5
等 级:职业侠客
帖 子:79
专家分:366
注 册:2011-10-13
得分:10 
list 在栈里分配空间,main()进入时会将ebp等一些返回信息压入栈以供程序返回时用。
struct student list[1];这个只分配了一个结构体空间,所以当
input(&list[1],&stdidall);时就会将保存ebp的值覆盖,导致返回时出错。
如果在struct student list[1];前面再加一个 student a;在栈里空出一个空间来也可以正常运行。

像这样
程序代码:
#include <stdio.h>

void input (struct student *,int *);
void output (struct student);
struct student
{
    char name[20];
    int age;
    int score;
    char ach;
    int stdid;
};

int main(void)
{
    student a;
    struct student list[1];
    int stdidall = 0;

    input(&list[0],&stdidall);
    printf("Complete\n");
    input(&list[1],&stdidall);
    printf("Complete\n");

    printf("--------------------------\n");
    output(list[0]);
    output(list[1]);
    return 0;
}

void input (struct student * temp , int * tempall)
{
    printf("Enter Student Name:");
    scanf("%s",temp->name);
    printf("Enter Student Age:");
    scanf("%d",&temp->age);
    printf("Enter Student Score:");
    scanf("%d",&temp->score);

    if (90 <= temp->score)
        temp->ach = 'A';
    else if (70 <= temp->score)
        temp->ach = 'B';
    else if (60<= temp->score)
        temp->ach = 'C';
    else if (50<= temp->score)
        temp->ach = 'D';
    else
        temp->ach = 'F';
    *tempall = *tempall + 1;
    temp->stdid = *tempall;

    return;
}

void output (struct student temp)
{
    printf("Name:%s\nAge:%d\nScore:%d\nAchievement:%c\nNo.:%d\n", temp.name , temp.age , temp.score , temp.ach , temp.stdid);
    return;
}





 

[ 本帖最后由 nomify 于 2011-10-15 18:23 编辑 ]
2011-10-15 18:21
随风飘荡
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:208
专家分:598
注 册:2011-9-9
得分:0 
感谢!
2011-10-15 18:37
AnonymousJDK
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2021-8-7
得分:0 
66666666666666666666666666终于找到原因!感谢大神
修改前:
程序代码:
#include<stdio.h>
void main() {
    int a = 0, b = 0, num = 0;
    char c = '0';
    printf("Please input a +(- * / ) b:");
    scanf_s("%d", &a);
    scanf_s("%c", &c);
    scanf_s("%d", &b);

    printf("=");
    switch (c) {
    case '+':
        printf("%d", (a + b) );
        break;
    case '-':
        printf("%d", (a - b));
        break;
    case '*':
        printf("%d", (a * b));
        break;
    case '/':
        printf("%d", (a / b));
    }
    printf("over!");
}


修改后为:
程序代码:
        
#include<stdio.h>
void main() {
    int a=0, b=0,num=0;
    char c='0';
    printf("Please input a +(- * / ) b:");
    scanf_s("%d",&a);
    scanf_s("%c",&c);
    scanf_s("%d",&b);

    printf("=");
    switch (c) {
    case '+':
        num=(a + b) ;
        break;
    case '-':
        num=(a - b);
        break;
    case '*':
        num=(a*b);
        break;
    case '/':
        num=(a / b);
    }
    printf("%d",num);

    printf("over!");
}
2021-09-05 18:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-352537-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037291 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved