0

In many cases we declare a pointer dynamically in a function, and sometimes we don't want to free that memory at the return of the function, because we need that memory later.

We can return that dynamic pointer and later free it. I found some way to track that memory. Is that a good thing:

#include <iostream>

int foo()
{
    int* pInt = new int(77);
    int x = (int)pInt;
    std::cout << std::hex << x << std::endl; // 3831d8
    return x;
}


int main()
{

    int* pLostMem = (int*)foo();

    std::cout <<  pLostMem << std::endl; // 003831D8
    std::cout << std::dec << *pLostMem << std::endl; // 77 

    if(pLostMem)
    {
        delete pLostMem;
        pLostMem = NULL;
    }

    std::cout << std::endl;
    return 0;
}
almcd
  • 1,069
  • 2
  • 16
  • 29
  • What specifically is your question? – Carcigenicate Oct 06 '16 at 13:15
  • No there's no way in standard C++ to track memory allocations, other than to actually keep track of them yourself. If you allocate memory that you need later then ***return a pointer*** to that memory. Don't return an `int` which might not be able to hold a pointer. – Some programmer dude Oct 06 '16 at 13:16
  • 2
    That's what [smart pointers](http://en.cppreference.com/w/cpp/memory) from the c++ standard library are for. – πάντα ῥεῖ Oct 06 '16 at 13:17
  • Return a `std::unique_ptr` which handles this for you. – tkausl Oct 06 '16 at 13:17
  • @πάνταῥεῖ is that good thing? I made it on my own. because I am still learning and I didn't reach "smart pointers" –  Oct 06 '16 at 13:18
  • @Syfu_H Of course I read it. _"is that good thing?"_ Yes, it is. Its unlikely you will come up with a better implementation. – πάντα ῥεῖ Oct 06 '16 at 13:18
  • @πάνταῥεῖ sorry! the question wasn't for you. "hove you read the question" –  Oct 06 '16 at 13:19
  • @Carcigenicate have you read the question –  Oct 06 '16 at 13:19
  • 1
    @Syfu_H Yes. "is that good thing" is rather generic for a site for specific questions, and that's the closest thing to a question here. – Carcigenicate Oct 06 '16 at 13:20
  • 2
    If you're learning about pointers, then wait with the smart pointers. Just learn the basics of pointers, including that you should not cast them to a non-pointer type. If you have a pointer then continue to have a pointer. Once you learned how to handle pointers correctly then learn about smart pointers, and the [standard containers](http://en.cppreference.com/w/cpp/container) which often should be used instead of allocating memory dynamically. – Some programmer dude Oct 06 '16 at 13:25
  • 2
    Also, for single primitive types (like `int` or `double` etc.), there's almost never any need to allocate such single entities dynamically. – Some programmer dude Oct 06 '16 at 13:26

1 Answers1

0

It's not entirely clear in your question, but if you just want to add cout statements to your code to show the value of a pointer, you can do that without casting to int. Pointers can be printed just fine:

#include <iostream>

int *foo()
{
  int* pInt = new int(77);
  std::cout << pInt << std::endl; // pointers can be output just fine
  return pInt;
}

int main()
{
  int* pLostMem = foo();

  std::cout << pLostMem << std::endl; // e.g. 0x16c2010
  std::cout << *pLostMem << std::endl; // 77

  delete pLostMem;
  pLostMem = NULL;

  std::cout << std::endl;
  return 0;
}

Live example here.

You also don't need the if (pLostMem==NULL) check before deleting.

Community
  • 1
  • 1
mindriot
  • 5,413
  • 1
  • 25
  • 34