Re: Returning Char array/pointer? Continuing of thread I am confused with these concepts.

rockdale wrote:
Sorry, Did not see your reply before I post my last test.

Now I got it. pass the reference of the pointer so that in the
function when I do the new char[], it will allocate memory and change
the pointer's value to this newly allocated memory address.

But one more question:
When you pass by reference, you are indeed calling malloc (or new)
on the original pointer.
So the c++ implicitly malloc memory to my pointer when I pass it by
reference, how does c++ know how many byte memory this pointer point
to needed? OR c++ does not know until I call new in the function?

Thanks for your explanation, I am very new to this passing reference
of pointer thing.


Your question has nothing to do with pass by reference. When you pass any variable (pointer or non-pointer) by reference, anything you do in the called function is just as if you had done it to the original variable in the calling program.

When you use new[], the C++ language does not provide a way for you to recover the length (you must remember it if you need it). But the compiler knows (at least for a non-POD type) because it knows how many destructors to call when you call delete[]. There was a discussion of this issue in this group just recently (see "Why 'delete []' for array?").

This is one reason it is much better to use std::vector<char> or std::string. These classes have a size() member function that tells you what the length of the character string is.

David Wilkinson
Visual C++ MVP