Hi,
Two things confused me and I hope somebody can point out the wrong
part, thanks in advance
char * mytest ( const char src[])
{
char *ret;
ret = new char;
strncpy(ret, src, 5);
return ret;
}
in main( ), I use:
char *y = mytest("abcdef" );
The code works but I think there are problems. I used 'new' to allocate
some memory, how to release them?
Another one:
char * mytest ( const char src[])
{
char ret[10];
strncpy(ret, src, 5);
return ret;
}
in main ( ) : char *y = mytest("abcdef" );
After we use 'char rec[10]', I think the it is in stack and 'rec' will
be released after the function 'mytest' finishes. So I suppose I cannot
get anything in 'y', but why did I get 'abcde' in y?
Dan
Two things confused me and I hope somebody can point out the wrong
part, thanks in advance
char * mytest ( const char src[])
{
char *ret;
ret = new char;
strncpy(ret, src, 5);
return ret;
}
in main( ), I use:
char *y = mytest("abcdef" );
The code works but I think there are problems. I used 'new' to allocate
some memory, how to release them?
Another one:
char * mytest ( const char src[])
{
char ret[10];
strncpy(ret, src, 5);
return ret;
}
in main ( ) : char *y = mytest("abcdef" );
After we use 'char rec[10]', I think the it is in stack and 'rec' will
be released after the function 'mytest' finishes. So I suppose I cannot
get anything in 'y', but why did I get 'abcde' in y?
Dan
Comment