Memory management in allocating 2-D array

后端 未结 2 1053
耶瑟儿~
耶瑟儿~ 2020-12-22 04:21

I have allocated a two dimensional array using the following code:

// Dynamic allocation
        int **matrix=new int*[n];
        for(int i=0;i

        
2条回答
  •  盖世英雄少女心
    2020-12-22 04:32

    for(int i=0;i

    You need to delete each of the internal arrays and then delete the array.

    As has been suggested in a comment. A safer way of doing this would be to use standard containers such as std::vector. You could do something like this then:

    std::vector> matrix;
    

    This would give you a two dimensional array but you would not have to handle the cleanup.

提交回复
热议问题