How to ignore uninitialized variable error in MSVC

自古美人都是妖i 提交于 2020-12-03 05:04:46

问题


I'm having problems with MSVS compiler, I have the following code:

if (!list) {
    *type = raw_data[*i++];
    ptr = (char*) &size;
    ptr[1] = raw_data[*i++];
    ptr[0] = raw_data[*i++];
    name = new char[size+1];
    memcpy (name, raw_data+*i, size);
    name[size] = '\0';
    *i += size;
}

And later:

if (!list) {
    s->name = name;
    s->name_size = size;
}

The value of list doesn't change in this function, however I can't compile the code because of this error:

Error 1 error C4703: potentially uninitialized local pointer variable 'name' used

I find it incredibly annoying that this isn't a warning but an error. Changing the bool to const bool doesn't help either. Does someone know how to ignore this specific error in Visual Studio, but still show other errors/warnings?


回答1:


I suspect you're passing the compiler the /sdl option, which tells it to treat a 4703 (and various other things) as an error rather than a warning.

In the context of being paranoid about security it makes sense to treat this as an error - the compiler can't prove that what you're doing is safe, so it won't let the code through. If you don't want that, turn off /sdl.




回答2:


There is checkbox for SDL checks when you create new project (wizard) in visual Studio 2015. If this is enabled, Visual Studio will report uninitialized variables as warning as part of potential other things, more info here.

If you already have a project with SDL checks on, you can disable it from project properties like in screen shot below. This way you don't have to deal with changing any command line arguments.



来源:https://stackoverflow.com/questions/18559425/how-to-ignore-uninitialized-variable-error-in-msvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!