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

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.