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.

Rockdale:

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
.



Relevant Pages

  • Re: Question on LSP
    ... Sure, the developer must keep track of which type has been assigned to each pointer to manage complexity in creating the design, which is why the 'T' is in T*. ... Subtyping is a relation which does not ... Those object types are completely orthogonal to the semantics of being an object reference. ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)
  • Re: Question on LSP
    ... Because construction paradigms have different goals. ... But that has nothing to do with what a pointer type ... But that does not mean that the semantics of an object reference is ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)
  • Re: Modifying Class Object
    ... Before I start, note we're talking about semantics, not ... For example, as I used as reference in my first posting, the Java language spec. ... A pointer stores a reference to something. ...
    (comp.lang.python)
  • Re: Question on LSP
    ... equivalent addresses in the same context. ... Note that the language allows us to use a name like 'T' on the reference as a mnemonic so that the developer can keep track of what is happening with the indirection. ... Modern languages have proper pointer types. ...
    (comp.object)
  • Re: Nothing Keyword Destories Objects rather than just resetting the variable to an empty variable
    ... there is no difference between using ByVal or ByRef. ... as the C1 pointer was the sole reference to the class object. ... Yes, an object variable is a pointer to an object, but that is the only ... the keyword "NOTHING" should only destroy the ...
    (microsoft.public.excel.programming)