Vishal Sharma
Vishal Sharma

Reputation: 1750

What happens if a pointer returned by a function is not stored?

There's a function which returns a pointer(any type), if I don't store the pointer when I call the function, what happens? Will the function still return a pointer in this case? If yes, then will there be a memory leak because I'm not freeing up the allocated memory?

Consider the below code as an example:

int * testfunc()
{
int * a=new int();
return(a);
}

int main()
{
testfunc();
return(0);
}

Upvotes: 4

Views: 648

Answers (4)

Neil Steyn
Neil Steyn

Reputation: 11

Yes, it does leak memory.

The pointer variable in function scope will get destroyed when the function exit, but the data the pointer has allocated will remain in memory. The function return the address of where the data is located.

If this is really what your function is intended to do, then you can still delete the data with the returned address

int *pi = testfunc();
delete pi;

Then there won't be a memory leak when your program exit, but yes there is a memory leak in the function as you have asked.

Upvotes: 1

Vishal Sharma
Vishal Sharma

Reputation: 1750

I ran valgrind on the code given in the question(after compiling it with '-g' option) using the statement(valgrind --leak-check=full --show-reachable=yes --track-origins=yes ./test) Below is the output

==59142== Memcheck, a memory error detector
==59142== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==59142== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==59142== Command: ./test
==59142==
==59142==
==59142== HEAP SUMMARY:
==59142==     in use at exit: 4 bytes in 1 blocks
==59142==   total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==59142==
==59142== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==59142==    at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298)
==59142==    by 0x4006D3: testfunc() (test.cpp:7)
==59142==    by 0x4006EF: main (test.cpp:13)
==59142==
==59142== LEAK SUMMARY:
==59142==    definitely lost: 4 bytes in 1 blocks
==59142==    indirectly lost: 0 bytes in 0 blocks
==59142==      possibly lost: 0 bytes in 0 blocks
==59142==    still reachable: 0 bytes in 0 blocks
==59142==         suppressed: 0 bytes in 0 blocks
==59142==
==59142== For counts of detected and suppressed errors, rerun with: -v
==59142== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)

This clearly demonstrates that there is a memory leak.

Upvotes: -3

lorisleitner
lorisleitner

Reputation: 698

Yes, you have to manually free every block of memory allocated with new, new[], malloc() and calloc(). The method will still return a pointer and it's pointing to valid memory but you can't use or free it. In C++ you should nearly always return by value and move semantics will take care of dynamic memory.

Upvotes: 3

Bathsheba
Bathsheba

Reputation: 234645

There absolutely will be a memory leak. You need to balance all calls to new with a delete on the returned pointer.

C++ gives you some class to help you manage that delete: see std::unique_ptr. Essentially the destructor of std::unique_ptr calls delete which, more often than not, is extremely useful.

Upvotes: 7

Related Questions