删除警告C6001

发布于 2025-02-13 20:59:52 字数 1264 浏览 2 评论 0原文

我正在使用VS2017,并且不明白为什么我会在catch块中使用(values!= null)在线上获得“警告C6001”使用非初始化的内存'值'”。

#include <windows.h>

typedef enum 
{        
   VALUE_STATE_NOT_AVAILABLE = 1,
   VALUE_STATE_ERROR         = 2,
   VALUE_STATE_VALID         = 3,
   VALUE_STATE_UNKNOWN       = 4
} XyzValueState_t;
    
class XyzValue
{
    private:    XyzValueState_t     _valueState;
    protected:  XyzValue( XyzValueState_t valueState )  {
                    _valueState = valueState;
                }
}

typedef XyzValue* xyzValuePtr_t;

main(){
    bool flag=true;
    xyzValuePtr_t* values = NULL;
    unsigned int _argument=2;
    if(flag==true)  {
        values = new PCLValuePtr_t[2]{ NULL,NULL }; 
        try     {
            values[0] = new PCLUnsignedInteger(_argument);
            values[1] = new PCLUnsignedInteger(_argument);
            xyz(values);    // passing the value to third party function which returns void
        }
        catch(...)  {
            if(values!= NULL){
                for(int k = 0; k < 1; k++)  {
                   delete values[k];
                   values[k] = NULL;      
                }
                delete [] values;
                values = NULL;
            }
        }
    }
}

预先感谢您的帮助和指导

I am using VS2017 and do not understand why I am getting compiler "Warning C6001 Using uninitialized memory 'values'", on line if(values!= NULL) in catch block.

#include <windows.h>

typedef enum 
{        
   VALUE_STATE_NOT_AVAILABLE = 1,
   VALUE_STATE_ERROR         = 2,
   VALUE_STATE_VALID         = 3,
   VALUE_STATE_UNKNOWN       = 4
} XyzValueState_t;
    
class XyzValue
{
    private:    XyzValueState_t     _valueState;
    protected:  XyzValue( XyzValueState_t valueState )  {
                    _valueState = valueState;
                }
}

typedef XyzValue* xyzValuePtr_t;

main(){
    bool flag=true;
    xyzValuePtr_t* values = NULL;
    unsigned int _argument=2;
    if(flag==true)  {
        values = new PCLValuePtr_t[2]{ NULL,NULL }; 
        try     {
            values[0] = new PCLUnsignedInteger(_argument);
            values[1] = new PCLUnsignedInteger(_argument);
            xyz(values);    // passing the value to third party function which returns void
        }
        catch(...)  {
            if(values!= NULL){
                for(int k = 0; k < 1; k++)  {
                   delete values[k];
                   values[k] = NULL;      
                }
                delete [] values;
                values = NULL;
            }
        }
    }
}

Thank you in advance for your help and guidance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2025-02-20 20:59:52

不太确定为什么您的编译器认为这可能是一致的。

但是,在C ++中,我认为您使用new构建数组的方式是不必要的复杂且容易发生的。看起来像1993年的某人尝试编写C ++ 11。您有初始化列表,但您不使用RAII!

因此,请执行C ++操作,并使用C ++的确定对象寿命来管理内存。对于一系列对象,这是由std :: vector优雅地处理的:

#include <vector>
class XyzValue;

main(){
    bool flag=true;
    unsigned int _argument=2;
    if(flag==true)  {
        std::vector<XyzValue> values(2);  // default initialization for two XyzValues.
        try     {
            xyz(values.data());    // if you need the raw contiguous memory. **You probably don't.**
        }
        catch(...)  {
// all the manual deletion not necessary anymore, because at end of scope, things are deconstructed automatically, so this catch clause now is empty.
        }
    }
}

请参阅如何更短得多,更好可读性,具有相同的功能,但是无需手动删除任何内容吗?这就是为什么我们写C ++而不是C。

not quite sure why your compiler thinks this might be unitialized.

But, in C++, I'd argue that the way you're building your array with new is unnecessarily complicated and error prone. This look like someone from 1993 tried to write C++11. You have initializer lists, but you don't use RAII!

so, do the C++ thing and use C++'s deterministic object lifetime to manage memory. For an array of objects, this is elegantly handled by std::vector:

#include <vector>
class XyzValue;

main(){
    bool flag=true;
    unsigned int _argument=2;
    if(flag==true)  {
        std::vector<XyzValue> values(2);  // default initialization for two XyzValues.
        try     {
            xyz(values.data());    // if you need the raw contiguous memory. **You probably don't.**
        }
        catch(...)  {
// all the manual deletion not necessary anymore, because at end of scope, things are deconstructed automatically, so this catch clause now is empty.
        }
    }
}

See how this is much shorter, better readable, has the same functionality, but none of the need to manually delete anything? That's why we write C++ instead of C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文