scanf_s警告?跳过用户输入(主题:Runge-Kutta,流行病模拟) [英] Scanf_s warning? Skips User Inputs (topics: Runge-Kutta, Epidemic Simulation)

查看:127
本文介绍了scanf_s警告?跳过用户输入(主题:Runge-Kutta,流行病模拟)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,我不得不承认,我在编程方面很糟糕.我是班上努力工作的那个家伙,但似乎永远无法像其他同学一样掌握编程.因此,请保持友好,我将尝试在下面解释我的问题.

This is my first post and I have to admit, I am terrible at programming. I am that guy in the class that works his tail off, but can never seem to grasp programming as well as the rest of my classmates. So please be nice, I will try to explain my problem below.

我有以下代码(删除了注释),但是当我运行它时,我得到的警告类似于下面列出的警告.同样,当我运行程序时,允许输入第一个用户输入的值,但是突然之间,它跳到程序的末尾,不允许我输入其他变量的值(例如,变量"beta" ).我有一个输出图像( http://i.stack.imgur.com/yc3jq.jpg ),您会看到我输入了alpha,但是程序运行到最后.有什么想法吗?

I have the following code (comments removed), but when I run it I get a warning similar to the one listed below. Also, when I run the program, the first user inputted value is allowed, but then all of the sudden, it jumps to the end of the program, not allowing me to input the values for the other variables (e.g. the variable "beta"). I have an image of the output (http://i.stack.imgur.com/yc3jq.jpg) and you can see that I enter alpha, but then the program runs to the end. Any thoughts?

非常感谢您的帮助! -喷枪

Thank You so very much for your help! -Spencer

--------------------------------- CODE ----------------

-----------------------------CODE----------------

    #include<stdio.h>
#include<stdlib.h>
#include<math.h>

float alpha, beta, h; 
float slope_k (float, float, float, float); 
float slope_q (float, float, float, float); 
float slope_p (float, float, float, float); 

int main (void)
{

float t0=0, tf, h, S0, I0, R0, k1, k2, k3, k4, q1, q2, q3, q4, p1, p2, p3, p4;
int N;
char sim_file[1000];  
FILE *out_file;
float *time_out, *s_out, *i_out, *r_out;

printf("Enter the value of the rate constant for infection (alpha) \n");
scanf("&f", &alpha);

printf("Enter the value of the rate constant for recovery or death (beta) \n");
scanf("&f", &beta);

printf("Enter the value of number of persons susceptible to the given contagion [S] at the initial  time zero [i.e. S(t)=S(0) = ? ] \n");
scanf("&f", &S0);

printf("Enter the value of the number of persons infected [I] at the intial time zero [i.e. I(t) = I(0) = ?] \n");
scanf("&f", &I0);

printf("Enter the value of the number of persons that have already been infected but have recovered [or died] [R] at the initial time zero [i.e. R(t) = R(0) = ?] \n");
scanf("&f", &R0); 

printf("Enter the final time for solution \n");
scanf("&f", &tf);

printf("Enter the solution step size (H) \n");
scanf("&f", &h);

N = (int)(tf/h);

printf("Enter file solution to store solution to simulation \n");
scanf("&s", sim_file);

out_file = fopen(sim_file, "w");

time_out = (float *)calloc(sizeof(float), N);
s_out = (float *)calloc(sizeof(float), N);
i_out = (float *)calloc(sizeof(float), N);
r_out = (float *)calloc(sizeof(float), N);


time_out[0]= 0; 
s_out[0] = S0;
i_out[0] = I0;
r_out[0] = R0;

for(int i = 0; i < N; ++i);
{
int i = 0;
time_out[i+1] = (i+1)*h;

k1 = h*slope_k(time_out[i], s_out[i], i_out[i], r_out[i]);
q1 = h*slope_q(time_out[i], s_out[i], i_out[i], r_out[i]);
p1 = h*slope_p(time_out[i], s_out[i], i_out[i], r_out[i]);

k2 = h*slope_k(time_out[i]+(h/2), s_out[i]+(k1/2), i_out[i]+(q1/2), r_out[i]+(p1/2));
q2 = h*slope_q(time_out[i]+(h/2), s_out[i]+(k1/2), i_out[i]+(q1/2), r_out[i]+(p1/2));
p2 = h*slope_p(time_out[i]+(h/2), s_out[i]+(k1/2), i_out[i]+(q1/2), r_out[i]+(p1/2));

k3 = h*slope_k(time_out[i]+(h/2), s_out[i]+(k2/2), i_out[i]+(q2/2), r_out[i]+(p2/2));
q3 = h*slope_q(time_out[i]+(h/2), s_out[i]+(k2/2), i_out[i]+(q2/2), r_out[i]+(p2/2));
p3 = h*slope_p(time_out[i]+(h/2), s_out[i]+(k2/2), i_out[i]+(q2/2), r_out[i]+(p2/2));

k4 = h*slope_k((time_out[i] + h), (s_out[i]+k3), (i_out[i]+q3), (r_out[i]+p3));
q4 = h*slope_q((time_out[i] + h), (s_out[i]+k3), (i_out[i]+q3), (r_out[i]+p3));
p4 = h*slope_p((time_out[i] + h), (s_out[i]+k3), (i_out[i]+q3), (r_out[i]+p3));

s_out[i+1] = s_out[i] + (1.0/6)*(k1 + (2*k2) + (2*k3) + k4);
i_out[i+1] = i_out[i] + (1.0/6)*(q1 + (2*q2) + (2*q3) + q4);
r_out[i+1] = r_out[i] + (1.0/6)*(p1 + (2*p2) + (2*p3) + p4);

}

return 0;
}

float slope_k(float t, float s, float i, float r)
{
float slope_k_out;
slope_k_out = -alpha*s*i;
return slope_k_out;
}

float slope_q(float t, float s, float i, float r)
{
float slope_q_out;
slope_q_out = (alpha*s*i)-(beta*i);
return slope_q_out;
}

float slope_p(float t, float s, float i, float r)
{
float slope_p_out;
slope_p_out = beta*i;
return slope_p_out;
}


警告示例:


Example warning:

warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

推荐答案

编译器在这里告诉您的是函数scanf是不安全的. scanf有一个错误,如果被利用,它会导致系统受到威胁(称为缓冲区溢出攻击).简而言之,该错误是没有告诉scanf要读取多少字节的输入.因此,scanf将一直读取,直到认为"读取输入完成为止.在char数组中,此结尾通常是空字符'\0'.但是,如果从字符串中离开'\0',则scanf将继续读取,直到找到该字节为止-通常,scanf将到达其自己的虚拟内存空间之外的内存位置.此操作将导致操作系统向您的程序发送分段错误(例如段错误),从而最终终止程序的存在.

What the compiler is telling you here is that the function scanf is not safe. scanf has a bug that, if exploited, can cause a system to become compromised (called a buffer overflow attack). In brief, the bug is that one does not tell scanf how many bytes to read for input. Thus scanf will read until it "believes" it is done reading the input. In a char array, this end is usually the null character '\0'. However, if one leaves off '\0' from a string, scanf will continue reading until it finds that byte -- usually, scanf will reach a memory location that is outside of its own virtual memory space. This action will cause the OS to send your program a segmentation fault (seg fault) which will summarily end your program's existence.

较新的功能scanf_s(为了安全起见,_s)可让您确定输入的最大大小,可用于更有效地防止缓冲区溢出攻击.如果这是用于硬件分配(看起来像这样),则可以将scanf保留在那里.但是,要摆脱比较容易的警告并尝试成为更好的程序员,请修复它!使用sscanf_s并具有一个确定最大输入大小(例如int SCANF_INPUT_SIZE = 1000)的全局变量(或其他内容).

The newer function, scanf_s,(_s for secure), lets you determine the max size of the input, which you can use to more effectively prevent buffer overflow attacks. If this is for a HW assignment, which it looks like it is, you can leave scanf there. However, to get rid of the compilier warning AND try and become a better programmer, fix it! Use sscanf_s and have a global variable (or something...) that determines maximum input size (e.g. int SCANF_INPUT_SIZE = 1000 ).

祝你好运!

编辑-将那些"&f"更改为"%f",这是错误的!

EDIT -- Change those "&f" to "%f" that's the error!

这篇关于scanf_s警告?跳过用户输入(主题:Runge-Kutta,流行病模拟)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆