In what cases do I use malloc and/or new?

前端 未结 19 1626
北恋
北恋 2020-11-21 17:41

I see in C++ there are multiple ways to allocate and free data and I understand that when you call malloc you should call free and when you use the

19条回答
  •  旧巷少年郎
    2020-11-21 18:01

    new will initialise the default values of the struct and correctly links the references in it to itself.

    E.g.

    struct test_s {
        int some_strange_name = 1;
        int &easy = some_strange_name;
    }
    

    So new struct test_s will return an initialised structure with a working reference, while the malloc'ed version has no default values and the intern references aren't initialised.

提交回复
热议问题