H
happyvalley
Hi,
basically, the test function get a char pointer, and assigned a string
to it. then the string is passed back by the call-by-reference
mechanism. in test(), I reallocate some memory for the pointer, the
size is not fixed.
I remember, all new statement should be followed by a delete statement,
is there some memory leak here?
void test(char* &str)
{
// don't know how long the string might be
char *astring = "a string to return after thinking, not
fixed length";
str = new char[strlen(astring)+1];
strcpy(str, astring );
cout<<"test "<<str<<endl;
}
void main(void)
{
char *astr = "";
test(astr);
cout<<astr<<endl;
}
basically, the test function get a char pointer, and assigned a string
to it. then the string is passed back by the call-by-reference
mechanism. in test(), I reallocate some memory for the pointer, the
size is not fixed.
I remember, all new statement should be followed by a delete statement,
is there some memory leak here?
void test(char* &str)
{
// don't know how long the string might be
char *astring = "a string to return after thinking, not
fixed length";
str = new char[strlen(astring)+1];
strcpy(str, astring );
cout<<"test "<<str<<endl;
}
void main(void)
{
char *astr = "";
test(astr);
cout<<astr<<endl;
}