scanf&scanf_s difference

本文介绍了一种通过使用带有_s后缀的安全函数来提高软件安全性的方法,这些函数通过增加额外参数来验证内存操作的有效性,从而避免潜在的安全漏洞。

PS: 很多带“_s”后缀的函数是为了让原版函数更安全,传入一个和参数有关的大小值,避免引用到不存在的元素,防止hacker利用原版的不安全性(漏洞)黑掉系统。

 

转载于:https://www.cnblogs.com/Marigolci/p/9447651.html

#include <stdio.h> #include <stdlib.h> #include "aip_common.h" #include "Num1.h" static U1 u1_sp_graphics_canvas[(U2)GRAPHICS_HEIGHT][(U2)GRAPHICS_WIDTH]; U4 main() { U2 u2_t_graphics_x1; U2 u2_t_graphics_y1; U2 u2_t_graphics_x2; U2 u2_t_graphics_y2; u2_t_graphics_x1 = u2_s_graphics_getCoordinate("Please enter the x-coordinates of point A"); u2_t_graphics_y1 = u2_s_graphics_getCoordinate("Please enter the y-coordinate of point A"); u2_t_graphics_x2 = u2_s_graphics_getCoordinate("Please enter the x-coordinate of point B"); u2_t_graphics_y2 = u2_s_graphics_getCoordinate("Please enter the y-coordinate of point B"); /* Call function to draw a line */ u2_s_graphics_drawLine(u2_t_graphics_x1, u2_t_graphics_y1, u2_t_graphics_x2, u2_t_graphics_y2); return (U2)DATA_MIN; } /*============================================================*/ /* Drawpixel: Light up the pixels */ /*-------------------------------------------------------------*/ /* Arguments: u2_t_graphics_x */ /* u2_t_graphics_y */ /*=============================================================*/ VP Drawpixel(U2 u2_t_graphics_x, U2 u2_t_graphics_y) { /* Check if the current coordinates are within the canvas */ if (u2_t_graphics_x < (U2)GRAPHICS_WIDTH && u2_t_graphics_y < (U2)GRAPHICS_HEIGHT) { u1_sp_graphics_canvas[u2_t_graphics_y][u2_t_graphics_x] = '*'; /*Light up the pixels*/ } else { /*Do nothing*/ } } /*================================================================*/ /* u2_s_graphics_getCoordinate: Get input and check legitimacy */ /*--------------------------------------------------------------*/ /* Arguments: u1p_a_prompt */ /* ---------------------------------------------------------- */ /* Return: u2_t_graphics_value : Coordinate value */ /*=========================================================*/ U2 u2_s_graphics_getCoordinate(U1* u1p_a_prompt) { U2 u2_t_graphics_value; /*Used to store user-entered coordinate values*/ U1 u1_t_check; /*Used to check for illegal characters or decimal points*/ S4 s4_t_state; s4_t_state = (U2)DATA_MIN; /* Loop until the user enters a legitimate coordinate value */ while (TRUE) { switch (s4_t_state) { /* User input */ case (U2)USER_INPUT: printf("%s (range: %u to %u): ", u1p_a_prompt, (U2)COORD_MIN, (U2)COORD_MAX); s4_t_state = (U2)INPUT_CHECK; break; /* Read the input and check if the data is a number */ case (U2)INPUT_CHECK: /*Expect the user to enter a number and a carriage enter, where Buffer_Size is 1 and VALID_INPUT is 2*/ if (scanf_s("%hu%c", &u2_t_graphics_value, &u1_t_check, (U2)BUFFER_SIZE) != (U2)VALID_INPUT) { printf("Invalid input, please enter a number.\n"); while (getchar() != '\n'); s4_t_state = (U2)USER_INPUT; } /*Check if extra characters are entered, if %c is not entered with a carriage enter symbol (e.g., 123abc)*/ else if (u1_t_check != '\n') { printf("Wrong input, coordinates must be integers, please re-enter.\n"); while (getchar() != '\n'); s4_t_state = (U2)USER_INPUT; } else { s4_t_state = (U2)INPUT_RANGE; } break; /* Check if the input is within the legal range */ case (U2)INPUT_RANGE: if (u2_t_graphics_value >= (U2)COORD_MIN && u2_t_graphics_value <= (U2)COORD_MAX) { return u2_t_graphics_value; /* Enter legal, return value */ } else { printf("The input value is out of range, please re-enter it.\n"); s4_t_state = (U2)USER_INPUT; } break; default: /* Defensive programming to handle unexpected states*/ exit(EXIT_FAILURE); /* Exit Procedure*/ } } } /*=================================================================================*/ /* u2_s_graphics_drawLine: Straight lines are drawn using the Bresenham algorithm */ /*---------------------------------------------------------------------------------*/ /* Arguments: u2_a_graphics_x1 */ /* u2_a_graphics_y1 */ /* u2_a_graphics_x2 */ /* u2_a_graphics_y2 */ /*=================================================================================*/ VP u2_s_graphics_drawLine(U2 u2_a_graphics_x1, U2 u2_a_graphics_y1, U2 u2_a_graphics_x2, U2 u2_a_graphics_y2) { /* Store the current coordinates */ U2 u2_t_graphics_y; U2 u2_t_graphics_x; /* Store the difference between horizontal and vertical directions */ S2 s2_t_graphics_dx; S2 s2_t_graphics_dy; /* Storage Direction */ S2 s2_t_graphics_sx; S2 s2_t_graphics_sy; /* Store error values */ S2 s2_t_graphics_err; /* Define cyclic variables */ U1 u1_t_countj; U1 u1_t_counti; /* Temporary value of the storage error */ S2 s2_t_temp_err; /* Initialize the canvas to unlit */ for (u1_t_counti = (U2)COORD_MIN; u1_t_counti < (U2)GRAPHICS_HEIGHT; u1_t_counti++) { for (u1_t_countj = (U2)COORD_MIN; u1_t_countj < (U2)GRAPHICS_WIDTH; u1_t_countj++) { u1_sp_graphics_canvas[u1_t_counti][u1_t_countj] = ' '; } } /*Manually calculate direction x*/ if (u2_a_graphics_x1 < u2_a_graphics_x2) { s2_t_graphics_sx = (S2)POSITIVE_DIRECTION; } else { s2_t_graphics_sx = (S2)NEGATIVE_DIRECTION; } /*Manually calculate the direction y*/ if (u2_a_graphics_y1 < u2_a_graphics_y2) { s2_t_graphics_sy = (S2)POSITIVE_DIRECTION; } else { s2_t_graphics_sy = (S2)NEGATIVE_DIRECTION; } /* Calculating the absolute value of the horizontal difference */ if (((S2)u2_a_graphics_x2 - (S2)u2_a_graphics_x1) >= 0) { s2_t_graphics_dx = (S2)u2_a_graphics_x2 - (S2)u2_a_graphics_x1; } else { s2_t_graphics_dx = (S2)u2_a_graphics_x1 - (S2)u2_a_graphics_x2; } /* Calculate the absolute value of the vertical difference */ if (((S2)u2_a_graphics_y2 - (S2)u2_a_graphics_y1) >= 0) { s2_t_graphics_dy = (S2)u2_a_graphics_y2 - (S2)u2_a_graphics_y1; } else { s2_t_graphics_dy = (S2)u2_a_graphics_y1 - (S2)u2_a_graphics_y2; } /* Initialization error value */ s2_t_graphics_err = s2_t_graphics_dx - s2_t_graphics_dy; /* Initialize the current coordinates as the starting point */ u2_t_graphics_x = u2_a_graphics_x1; u2_t_graphics_y = u2_a_graphics_y1; printf("\nThe coordinate point through which the line segment passes:\n"); while (TRUE) { Drawpixel(u2_t_graphics_x, u2_t_graphics_y); /* Print current coordinate point */ printf("(%u, %u)\n", u2_t_graphics_x, u2_t_graphics_y); /* Exit the loop if you reach the end */ if (u2_t_graphics_x == u2_a_graphics_x2 && u2_t_graphics_y == u2_a_graphics_y2) { break; } else { /*Do nothing*/ } /* Twice the value of the calculation error */ s2_t_temp_err = (U2)DATA_DOUBLE * s2_t_graphics_err; /* If the error value is greater than the negative vertical difference */ if (s2_t_temp_err > -s2_t_graphics_dy) { s2_t_graphics_err -= s2_t_graphics_dy;/* Update error value */ u2_t_graphics_x += s2_t_graphics_sx;/* Update x coordinates based on direction */ } else { /*Do nothing*/ } /* If the error value is less than the horizontal difference value */ if (s2_t_temp_err < s2_t_graphics_dx) { s2_t_graphics_err += s2_t_graphics_dx; u2_t_graphics_y += s2_t_graphics_sy; } else { /*Do nothing*/ } } vd_s_graphics_display(u1_sp_graphics_canvas);/* Show canvas contents */ } /*=================================================================================*/ /* vd_s_graphics_display:Show the entire canvas */ /*---------------------------------------------------------------------------------*/ /* Arguments: u1_ap_graphics_canvas[(U2)GRAPHICS_HEIGHT][(U2)GRAPHICS_WIDTH] */ /*=================================================================================*/ VP vd_s_graphics_display(U1 u1_ap_graphics_canvas[(U2)GRAPHICS_HEIGHT][(U2)GRAPHICS_WIDTH]) { S2 s2_t_graphics_hight; U2 u2_t_graphics_width; U2 u2_t_graphics_x; printf("\nQuadrant 1 coordinate system:\n"); /* Print canvas contents line by line from top to bottom */ for (s2_t_graphics_hight = (U2)GRAPHICS_HEIGHT - (U2)DATA_CHANGE; s2_t_graphics_hight > (U2)COORD_MIN; s2_t_graphics_hight--) { printf(" | ");/* Print y-axis frame */ /* Traverse all pixels of the current row */ for (u2_t_graphics_width = (U2)COORD_MIN; u2_t_graphics_width < (U2)GRAPHICS_WIDTH; u2_t_graphics_width++) { printf("%c", u1_ap_graphics_canvas[s2_t_graphics_hight][u2_t_graphics_width]);/* Print the contents of the current pixel */ } printf("\n"); } /* Print x-axis frame */ printf(" +"); for (u2_t_graphics_x = (U2)COORD_MIN; u2_t_graphics_x < (U2)GRAPHICS_WIDTH; u2_t_graphics_x++) { printf("-"); } printf("\n"); }5、vd_s_graphics_display(U1 u1_ap_graphics_canvas[(U2)GRAPHICS_HEIGHT][(U2)GRAPHICS_WIDTH]) 形参不能直接用数组,另外这个函数使用的输入既然是全局变量,那直接把全局变量放函数即可,不需要通过形参输入
07-25
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值