简体   繁体   English

通过指针为类内部的struct内部的C ++动态内存分配分配了nullptr“有时”

[英]C++ dynamic memory allocation of struct inside class via pointers is assigned nullptr “sometimes”

I have a simple example. 我有一个简单的例子。 I have a class, inside of it I have a struct that includes a pointer of type double. 我有一个类,在其中有一个结构,其中包含一个double类型的指针。 Inside that class, using a function therein, I intend to allocate dynamic memory using pointers. 在该类内部,使用其中的函数,我打算使用指针分配动态内存。 Out of curiosity, why does when I compile sometimes (50% of the time I compile) these pointers are initialized to be null and sometimes not? 出于好奇,为什么有时在我编译(占编译时间的50%)时将这些指针初始化为null,有时却不为null?

A minimally-working example starting with the header file could be: 从头文件开始的最小工作示例可能是:

class house{

    struct room{
        double *roomPtr;
    } roomObj;

    room *myRoom = &roomObj;

public:
    void startPainting();

private:
    void paintRoom();
};

As for my cpp file, then it may be summarized (via a minimal example, as above) as: 至于我的cpp文件,则可以将其总结为(通过上面的一个最小示例)为:

void house::paintRoom(){
    if (myRoom->roomPtr != nullptr){
        myRoom->roomPtr = new double[someNbr];
    }
    else{
        cout << "Allocation failed !" << endl;
    }
}

For convenience sake, consider that in this class the public function's job is to simply allocate the memory (never mind freeing for now) -- ie 为了方便起见,请考虑在此类中,公共功能的工作是简单地分配内存(暂时不要释放内存),即

void house::startPainting(){
    paintRoom();
}

Anyway, when compiling the main (by simply creating an object of the class and running the public function...), sometimes (almost every other time) I get "Allocation failed!" 无论如何,在编译main时(通过简单地创建类的对象并运行public函数...),有时(几乎每隔一次)我会收到“分配失败!” -- ie the pointer was null. -即指针为空。 Is this typical, or am I doing something wrong? 这是典型的,还是我做错了什么?

Also, I understand that I may instead of using the above approach to allocate memory, I can per se do the following without the additional pointer(s): 另外,我知道我可以不使用上面的方法来分配内存,而可以自己执行以下操作而无需其他指针:

roomObj.roomPtr = new double[someNbr]; 

This always works, but then again I am just curious why the above doesn't always work. 这总是有效的,但是我再次好奇为什么上面的方法并不总是有效。 May you please shed some insight on my shortcoming(s) ? 您能否对我的缺点有所了解?

Thanks ! 谢谢 !

You need to assign the pointer roomPtr to a default value, such as in class' constructor. 您需要将指针roomPtr分配给默认值,例如在类的构造函数中。 The memory in these objects are initialized in an implementation-defined way, so you may randomly get it assigned to a null value but it is not guaranteed. 这些对象中的内存以实现定义的方式初始化,因此您可以随机将其分配为空值,但不能保证。

For example, this modification would be better and provide consistent behavior: 例如,此修改会更好,并提供一致的行为:

house::house() : roomObj(), myRoom(&roomObj) {
   myRoom->roomPtr = nullptr;
}

As a side note, it doesn't fully make sense to me why you have a second member variable that is just pointing to some memory within the same class when you have a separate member variable describing it. 顺便提一句,对于我来说,为什么当您有一个单独的成员变量来描述它时,为什么第二个成员变量仅指向同一类中的某些内存,这对我来说并没有什么意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM