简体   繁体   English

运行时检查失败-变量周围的堆栈已损坏

[英]Run-Time Check Failure - Stack around variable was corrupted

I am getting the error 我收到错误

Run-Time Check Failure #2 - Stack around the variable 'IDU' was corrupted. 运行时检查失败#2-变量'IDU'周围的堆栈已损坏。

I have googled and tried everything and I have read that I need to increase the array size. 我已经用Google搜索并尝试了所有方法,并且已经阅读到我需要增加数组大小。 However, for this specific task I am not allowed to do that. 但是,对于此特定任务,我不允许这样做。 I also tried changing my condition and it seemed to work for the one variable 'newsal' and now is showing this error for 'IDU'. 我还尝试更改条件,它似乎适用于一个变量“ newsal”,现在显示“ IDU”的此错误。
Please help me out here. 请帮我在这里。

#define _CRT_SECURE_NO_WARNINGS
#define SIZE 4
#include <stdio.h>

// Define Number of Employees "SIZE" to be 2


// Declare Struct Employee 
struct employee {
    int ID;
    int age;
    double salary;

};

/* main program */
int main(void) {

    int option = 0;
    int count = 0, count2 = 0, valid = 0;
    int IDU;
    double newsal = 0;

    struct employee emp[SIZE] = { { 0 } };

    // Declare a struct Employee array "emp" with SIZE elements 
    // and initialize all elements to zero


    printf("---=== EMPLOYEE DATA ===---\n\n");

    do {
        // Print the option list
        printf("1. Display Employee Information\n");
        printf("2. Add Employee\n");
        printf("3. Update Employee Salary\n");
        printf("4. Remove Employee\n");
        printf("0. Exit\n\n");
        printf("Please select from the above options: ");

        // Capture input to option variable
        scanf("%d", &option);
        printf("\n");

        switch (option) {
        case 0: // Exit the program
            printf("Exiting Employee Data Program. Good Bye!!!\n");
            break;
        case 1: // Display Employee Data
                // @IN-LAB
            printf("EMP ID  EMP AGE EMP SALARY\n");
            printf("======  ======= ==========\n");

            for (count = 0; count < count2; count++) {
                if (emp[count].ID > 0) {
                    printf("%6d%9d%11.2lf", emp[count].ID, emp[count].age, emp[count].salary);
                    printf("\n");
                }
            }
            printf("\n");

            // Use "%6d%9d%11.2lf" formatting in a   
            // printf statement to display
            // employee id, age and salary of 
            // all  employees using a loop construct 

            // The loop construct will be run for SIZE times 
            // and will only display Employee data 
            // where the EmployeeID is > 0

            break;
        case 2: // Adding Employee
                // @IN-LAB
            printf("Adding Employee\n");
            printf("===============\n");
            if (valid == SIZE) {
                printf("ERROR!!! Maximum Number of Employees Reached\n");
                printf("\n");
                break;
            }

            printf("Enter Employee ID: ");
            scanf("%d", &emp[count2].ID);

            printf("Enter Employee Age: ");
            scanf("%d", &emp[count2].age);

            printf("Enter Employee Salary: ");
            scanf("%lf", &emp[count2].salary);
            valid++;
            count2++;
            printf("\n");


            // Check for limits on the array and add employee 
            // data accordingly. 



            break;
        case 3:
            printf("Update Employee Salary\n");
            printf("===============\n");

            do {
                printf("Enter Employee ID: ");
                scanf("%d", &IDU);
                for (count2 = 0; count2 <= SIZE; count2++) {
                    if (IDU == emp[count2].ID) {
                        printf("The current salary is %.2lf\n", emp[count2].salary);
                        printf("Enter Employee New Salary: ");
                        scanf("%lf", &newsal);
                        emp[count2].salary = newsal;
                    }
                }
            } while (IDU == emp[count2].ID);


                break;
        case 4:
            printf("Remove Employee\n");
            printf("===============\n");
            do {
                printf("Enter Employee ID: ");
                scanf("%d", &IDU);
                for (count2 = 0; count2 <= SIZE; count2++) {
                    if (IDU == emp[count2].ID) {
                        printf("Employee %d will be removed\n", emp[count2].ID);
                        emp[count2].ID = 0;
                        emp[count2].age = 0;
                        emp[count2].salary = 0;
                        printf("\n");
                        valid--;
                    }
                }
            } while (IDU == emp[count2].ID);
                break;
        default:
            printf("ERROR: Incorrect Option: Try Again\n\n");
        }

    } while (option != 0);


    return 0;
}




//PROGRAM OUTPUT IS SHOW BELOW

/*
---=== EMPLOYEE DATA ===---

1. Display Employee Information
2. Add Employee
0. Exit

Please select from the above options: 2

Adding Employee
===============
Enter Employee ID: 111
Enter Employee Age: 34
Enter Employee Salary: 78980.88

1. Display Employee Information
2. Add Employee
0. Exit

Please select from the above options: 2

Adding Employee
===============
Enter Employee ID: 112
Enter Employee Age: 41
Enter Employee Salary: 65000

1. Display Employee Information
2. Add Employee
0. Exit

Please select from the above options: 2

Adding Employee
===============
ERROR!!! Maximum Number of Employees Reached

1. Display Employee Information
2. Add Employee
0. Exit

Please select from the above options: 1

EMP ID  EMP AGE EMP SALARY
======  ======= ==========
111       34   78980.88
112       41   65000.00

1. Display Employee Information
2. Add Employee
0. Exit

Please select from the above options: 0

Exiting Employee Data Program. Good Bye!!!

*/

You loop like this 你这样循环

for (count2 = 0; count2 <= SIZE; count2++)

so count2 is up to SIZE inclusive. 因此count2的上限为SIZE。
Then you access like this 然后你这样访问

emp[count2].ID

ie

emp[SIZE].ID

And emp is 而emp是

struct employee emp[SIZE];

So you have access beyond the array, nothing more is needed to corrupt the stack. 因此,您可以访问数组之外​​的任何内容,不需要破坏堆栈。

My favorite attempt to solve this would be to loop like this instead: 我最喜欢的解决方法是改为像这样循环:

    for (count2 = 0; count2 < SIZE; count2++)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时检查失败 #2 - 变量“IDNumber”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'IDNumber' was corrupted 运行时检查失败#2-变量&#39;indices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted 运行时检查失败#2-变量&#39;result&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted 运行时检查失败#2-变量&#39;numberchoices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted 运行时检查失败#2-变量&#39;ex&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'ex' was corrupted 运行时检查失败#2-变量&#39;NearID&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'NearID' was corrupted 运行时检查失败#2-变量&#39;s&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 's' was corrupted 运行时检查失败#2 - 变量&#39;B&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'B' was corrupted 运行时检查失败#2-变量周围的堆栈-已损坏 - Run-Time Check Failure #2 - Stack around the variable — was corrupted 运行时检查失败 #2 - 变量“newRow”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'newRow' was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM