Re: Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- From: David Wilkinson <no-reply@xxxxxxxxxxxx>
- Date: Tue, 12 Feb 2008 08:41:50 -0500
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
.
- Follow-Ups:
- References:
- Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- From: rockdale
- Re: Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- From: David Wilkinson
- Re: Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- From: rockdale
- Re: Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- From: David Wilkinson
- Re: Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- From: rockdale
- Re: Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- From: rockdale
- Re: Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- From: David Wilkinson
- Re: Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- From: rockdale
- Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- Prev by Date: extern variable assignment
- Next by Date: building dynamic runtime library through command line.
- Previous by thread: Re: Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- Next by thread: Re: Returning Char array/pointer? Continuing of thread I am confused with these concepts.
- Index(es):
Relevant Pages
|