pointer to smart pointer

How do i convert a pointer to a smart pointer.

My program is suffering from memory leak, and seem to be caused by my pointers, after analyzing the issue using valgrind.

So my solution would consist of converting my pointers to smart pointers.. but how come?, and would it be a good idea?

There are several function.. but most of them is just different overloads... here is one ex.


1
2
3
4
5
6
int** get_map()
{
    int** map = set_map();
    // Do something
    return map;
}


it seems that all the int** map creates a memory leak, as they do not get deleted.. Would smart pointers help here.. or how?..
map should contain a 2d array which is initialized by set_map..
map should contain a 2d array which is initialized by set_map..

Have you considered std::vector instead of the arrays?

t seems that all the int** map creates a memory leak, as they do not get deleted..

Why aren't they getting deleted? You should always delete what you new.
Well array has to be used here, as it required by the other functions..

but yeah.. I know that new is always followed by delete.. But how do I do it here..
I return the element.. but i can't delete before i return it.. The only possible solution i see is to use smart pointer, and let it get deleted as it goes out of scope... but how do i apply it here??

Set map, just initializes it as a 2d array..
The only possible solution i see is to use smart pointer, and let it get deleted as it goes out of scope... but how do i apply it here??
If you use new it is up to you to use delete when that array is no longer necessary.
Using a smart pointer is the best idea, but it is not the only option. There are several other options, using one of the standard containers, encapsulating these pointers in a class of your own, etc.
And remember you'll probably also want to keep track of the sizes of these "arrays" as well.

Well array has to be used here, as it required by the other functions..

Why can't you change those other functions as well?
even if I used vector instead of the dynamic arrays.. I still have to delete it, or remove somehow, before i return it? Or would it do it itself?
I still have to delete it, or remove somehow, before i return it?

What? You don't new a std::vector so why would you need to delete it? A std::vector will destruct it's self when it goes out of scope when properly used. Plus a vector knows it's size and can be easily passed to and from functions, you can even easily return a std::vector using the return statement.
Topic archived. No new replies allowed.