任何避免警告C6386的方法,而无需完全禁用它或代码分析 [英] Any way to avoid warning C6386, without disabling it or Code Analysis altogether
问题描述
默认情况下,Visual Studio 2019开始将代码分析警告显示为编辑器内的绿色花键.这些对于学习C编程的学生可能非常有用,因为它们会捕获经典错误,例如被一个数组访问关闭.
不幸的是,误报可能会完全破坏学习体验,并且我担心我将不得不要求学生禁用该功能,以避免让他们担心不存在的问题.
此简短代码段不会引起任何警告:
#include< stdlib.h>int main(无效){size_t n = 6;int * v = malloc(n * sizeof(int));如果(v == NULL){返回1;}对于(size_t i = 0; i< n; ++ i){v [i] = i;}免费(v);返回0;}
不幸的是,如果您在函数中移动分配,如下所示:
#include< stdlib.h>int * test(size_t n){int * v = malloc(n * sizeof(int));如果(v == NULL){返回NULL;}对于(size_t i = 0; i< n; ++ i){v [i] = i;}返回v;}int main(无效){size_t n = 6;int * v = test(n);免费(v);返回0;}
您得到一个警告C6386:写入'v'时缓冲区溢出:可写大小为'n * sizeof(int)'字节,但可能会写入'8'字节.
>
即使阅读Stack Overflow,我也不知道'8'
的来源,但更重要的是,为什么它无法识别 i
永远不会超出范围.
所以问题是:有没有办法以不会产生警告的方式编写此类代码?
我知道我可以转到工具>选项>文字编辑器>C/C ++实验性的代码分析
,并将<代码>禁用代码分析代码设置为 True
,或使用 #pragma警告(disable:6386)
,但我宁愿避免这样做,也一定要避免向我的学生建议后者.
我真的很感谢大家的贡献,我同意 Adrian Mole 因此,在告诉学生这是一个错误以及如何关闭代码分析曲线图或禁用警告之后,我会选择 也可以解释为:不允许分配0个元素. Visual Studio 2019 started showing Code Analysis warnings as in-editor green squiggles by default. These may be extremely useful for students learning C programming, because they catch classical mistakes, such as off by one array accesses. Unfortunately false positives may completely ruin the learning experience and I fear that I will have to ask the students to disable the feature in order to avoid having them worry on non existing problems. This short snippet doesn't cause any warning: Unfortunately, if you move the allocation in a function, like this: you get a Even reading on Stack Overflow, I don't get where the So the question is: is there a way to write this type of code in a way that will not generate the warning? I know that I can go to I really want to thank everybody for their contributions and I agree that it is a bug in the Code Analyzer (by looking on Microsoft web sites it has been "Closed - Lower Priority" two years ago...). Adrian Mole So, after telling the students that it's a bug and how to turn off the Code Analysis squiggles or disable the warning, I'd go with Which may also be interpreted as: don't allow 0 elements allocation. 这篇关于任何避免警告C6386的方法,而无需完全禁用它或代码分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! max(n,0)
技巧指出了一种应对代码中警告的方法,即检查 n
是否大于零.有趣的是,对于应该使用的 n
,您仍然可以使用该零.正如约翰·博林格(John Bollinger)指出的那样,尽管这个想法可以用于有经验的程序员(这可能会禁用警告),但它不适用于学生.
int * test(size_t n){如果(n == 0){返回NULL;}int * v = malloc(n * sizeof(int));如果(v == NULL){返回NULL;}对于(size_t i = 0; i< n; ++ i){v [i] = i;}返回v;}
#include <stdlib.h>
int main(void)
{
size_t n = 6;
int *v = malloc(n * sizeof(int));
if (v == NULL) {
return 1;
}
for (size_t i = 0; i < n; ++i) {
v[i] = i;
}
free(v);
return 0;
}
#include <stdlib.h>
int *test(size_t n)
{
int *v = malloc(n * sizeof(int));
if (v == NULL) {
return NULL;
}
for (size_t i = 0; i < n; ++i) {
v[i] = i;
}
return v;
}
int main(void)
{
size_t n = 6;
int *v = test(n);
free(v);
return 0;
}
warning C6386: Buffer overrun while writing to 'v': the writable size is 'n*sizeof(int)' bytes, but '8' bytes might be written.
'8'
comes from, but, more importantly, why it fails to recognize that i
will never be out of range.Tools > Options > Text Editor > C/C++ > Experimental > Code Analysis
and set Disable Code Analysis Squiggles
to True
, or use a #pragma warning(disable:6386)
, but I'd rather avoid it, and certainly avoid suggesting my students the latter.max(n, 0)
trick points to a way for coping with the warning in code, that is checking that n
is greater than zero. The funny thing is that you can still use that zero for what n
was supposed to be used. While the idea could be used for experienced programmers (that would probably disable the warning), as John Bollinger points out, it's not for students.int *test(size_t n)
{
if (n == 0) {
return NULL;
}
int *v = malloc(n * sizeof(int));
if (v == NULL) {
return NULL;
}
for (size_t i = 0; i < n; ++i) {
v[i] = i;
}
return v;
}