0

Hi, I've wondered this for some time now.

Say function A() returns a char* which points to a string.. obviously

Say function B(char* szString) takes a char* as its argument.

If I call B(A()).. is the string automatically deleted or will it create a memory leak?


Thanks

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Narue
0

>Say function A() returns a char* which points to a string.. obviously
Not obviously. While char* usually denotes a C-style string, there's nothing stopping you from returning a pointer to char where a null character isn't used as a terminator. Keep in mind that C-style strings are defined by the presence of a null character, not the character type.

>is the string automatically deleted or will it create a memory leak?
Assuming A is dynamically allocating memory to the pointer it returns and assuming B doesn't release that memory and doesn't copy the pointer elsewhere, yes, it creates a memory leak because you lose the only reference you have to that block of memory.

However, to properly answer your question I need to see the definition of both A and B, as well as the calling code. All of these things work together to create or plug leaks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.