简体   繁体   English

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

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

This is an FCFC simulator program made using C. I am getting this certain error "Run-Time Check Failure #2 - Stack around the variable 'd' was corrupted." 这是使用C编写的FCFC仿真器程序。我收到此错误“运行时检查失败#2-变量'd'周围的堆栈已损坏”。 after I am finish entering all inputs for each process. 输入完每个过程的所有输入后, What am I doing wrong? 我究竟做错了什么?

void getdata()
{   
    char d;
    printf("Enter the number of process: ");
    scanf("%d",&n);

    printf("\nDo you need to enter the arrival time of process [Y/N]: ");
    scanf("%s",&d);
    printf("\n");

    for(int i=0; i<n; i++)
    {
        printf("*******************************************\n");
        printf("Enter the %d process burst time: ",i+1);
        scanf("%d",&b[i]);
        if(d=='y' || d=='Y')
        {
                printf("Enter the %d process arrival time: ",i+1);
                scanf("%d",&a[i]);
        }
        else
        {
                a[i]=0;
        }
    }
}

This is an error: 这是一个错误:

char d;
scanf("%s",&d);

as scanf("%s") will write to its argument until it locates whitespace and then appends a null terminator. 因为scanf("%s")将写入其参数,直到找到空白,然后追加一个空终止符。 d only has room for one character, meaning if there is at least one character then scanf() will be writing to memory it should not be causing some form of corruption. d仅可容纳一个字符,这意味着如果至少有一个字符,则scanf()将写入内存,这不应引起某种形式的损坏。 Use an array and limit the number of characters that can be read to prevent buffer overrun. 使用数组并限制可读取的字符数,以防止缓冲区溢出。 For example: 例如:

char d[128];
scanf("%127s", d); /* One less than size to leave room for null terminator. */

Check the return value of scanf() (and all functions that returns a value indicating failure or success) to ensure d has actually been populated. 检查scanf()的返回值(以及所有返回指示失败或成功的值的函数),以确保d已被实际填充。


To avoid duplicating (essentially) the sizeof the array d in the scanf() format specifier, which is a maintenance overhead and error-prone, build the format specifier for reading d instead: 为了避免(基本上)在scanf()格式说明符中复制数组dsizeof scanf()这是维护开销并且容易出错),请改用格式说明符来读取d

char d[128];
char d_format[32];
if (snprintf(d_format, sizeof(d_format), "%%%lus", sizeof(d) - 1) > 0)
{
    if (1 == scanf(d_format, d))
    {
    }
}

With this approach, if the sizeof() the array d is changed the format specifier for reading d will be automatically updated to the correct width (see demo @ http://ideone.com/9IsmgH ). 使用这种方法,如果更改了数组dsizeof() ,则用于读取d的格式说明符将自动更新为正确的宽度(请参见demo @ http://ideone.com/9IsmgH )。

声明:本站的技术帖子网页,遵循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;tempID&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'tempID' 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-变量“ z”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable ''z" was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM