简体   繁体   English

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

[英]Run-Time Check Failure #2 - Stack around the variable 'tempID' was corrupted

I recently started coding in c, and during coding I encountered this run-time error which I can't find the solution to. 我最近开始用c语言进行编码,在编码过程中遇到了无法找到解决方案的运行时错误。 It either displays this runtime error, or else stops after the scanf in the case where the customer is found, you choose what you want to edit, and then enter the new information. 它要么显示此运行时错误,要么在找到客户的情况下在scanf之后停止,您选择要编辑的内容,然后输入新信息。

Example: 例:

  • inputted ID:322993 输入的ID:322993
  • FOUND 发现
  • Pressed [1] to edit the ID 按[1]编辑ID
  • Input the new ID 输入新的ID
  • program gets stuck 程序被卡住

Here is the code: 这是代码:

void modifyCustomer(){
    int counter=0;
    long int tempID=0;
    flag found = false;
    fflush(stdin);
    printf("Enter Customer ID\n");
    scanf("%lld", &tempID);
    do{
        char option_str[200];
        int option = 0;
        char *not_valid;
        if(tempID == customers[counter].customerID){
            printf("Customer found!\n");
            found = true;
            do{
                fflush(stdin);
                printf("Choose what to modify:\n 1. ID\n 2. Name\n 3. Surname\n 4. Address\n 5. Mobile\nOption: ");
                scanf("%s", &option_str);
                option = strtol(option_str, &not_valid, 10);
                fflush(stdin);
                if (*not_valid != '\0') {
                    printf("%s is not valid.\n", not_valid);
                } else{
                    switch(option){
                    case 1:
                        printf("Enter new ID:\n");
                        scanf("%d\n", &customers[counter].customerID);
                        printf("Customer Modified Successfully!\n");
                        break;
                    case 2:
                        printf("Enter new Name:\n");
                        scanf("%s\n", &customers[counter].customerName);
                        printf("Customer Modified Successfully!\n");
                        break;
                    case 3:
                        printf("Enter new Surname:\n");
                        scanf("%s\n", &customers[counter].customerSurname);
                        printf("Customer Modified Successfully!\n");
                        break;
                    case 4:
                        printf("Enter new Address:\n");
                        scanf("%s\n", &customers[counter].customerAddress);
                        printf("Customer Modified Successfully!\n");
                        break;
                    case 5:
                        printf("Enter new Mobile:\n");
                        scanf("%lld\n", &customers[counter].customerMobile);
                        printf("Customer Modified Successfully!\n");
                        break;
                    default:
                        printf("You did not enter a valid Number. Please re-enter your Input \n");
                        break;
                    }
                }
            }while((option <1) || (option > 5));
        }
        else{
            counter++;
        }
    }while((found != true) && (counter < (custNum-1)));
    if (found == false)
        printf("Customer not found!\n");
}

Why does this happen? 为什么会这样?

The %lld format specifier is for a long long . %lld格式说明符很long long You've declared a long int so may be trying to write to a larger type than you have storage for. 您已声明一个long int因此可能正在尝试写入比您拥有的存储更大的类型。 The effects of this are undefined but writing over the next stack variable is very likely if sizeof(long int) != sizeof(long long) on your platform. 其效果是不确定的,但如果平台上的sizeof(long int) != sizeof(long long) ,则很有可能覆盖下一个堆栈变量。

You can fix this by either changing tempID to be of type long int or by changing the format specifier you use to %ld . 您可以通过将tempID更改为long int类型或将您使用的格式说明符更改为%ld来解决此问题。

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

相关问题 运行时检查失败#2-变量&#39;check&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'check' was corrupted 运行时检查失败#2-变量&#39;input&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'input' was corrupted 运行时检查失败#2-变量&#39;indices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted 运行时检查失败#2-变量&#39;name&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'name' was corrupted 运行时检查失败-变量周围的堆栈已损坏 - Run-Time Check Failure - Stack around variable 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 运行时检查失败 #2 - 变量“数组”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'array' was corrupted 运行时检查失败#2-变量&#39;d&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'd' was corrupted 运行时检查失败#2-变量“ z”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable ''z" was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM