[英]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: 例:
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, ¬_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.