How to Free Memory of A Structure with Pointers to another Structure

前端 未结 3 1000
[愿得一人]
[愿得一人] 2021-02-11 09:54

I have two structures

struct SimpleXY
{
    double x;
    double y;

};

    struct SimpleXyLink
    {
            int num_xy;
            SimpleXY *simpleXyList         


        
3条回答
  •  死守一世寂寞
    2021-02-11 10:10

    This entirely depends on how you allocated the memory. Freeing memory always has to echo the allocation.

    That said, free is almost certainly wrong in C++. Use new/delete instead of malloc/free.

    Furthermore, it seems as though you’re allocating memory for several elements her (at least the name …List implies this) so you will probably be better off using a C++ container structure, such as vector or list.

提交回复
热议问题