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

前端 未结 3 998
[愿得一人]
[愿得一人] 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:12

    If it is C++ (I'm confused here because you use free :-))

    struct SimpleXY
    {
        double x;
        double y;
    
    };
    
    struct SimpleXyLink
    {   
        SimpleXyLink() : simpleXyList( new SimpleXY ) { }
        ~SimpleXyLink() { delete simpleXyList; }
    
        int num_xy;
        SimpleXY *simpleXyList;
    };
    
    int main() 
    {
        SimpleXyLink* pXYLink = new SimpleXyLink();
    
        delete pXYLink;
    }
    

提交回复
热议问题