Le samedi 2 juillet 2005 à 23:26:13,
ambar.shome@gmail.com a écrit dans
comp.lang.c++*:
[color=blue]
> i have a function as listed below:
>
> char* ltoa(char* chr)
> {
> char* myChr=new char[100];
> strcpy(myChr,chr);
> return myChr;
> }
>
> in the above code i am returning a reference or pointer to a local
> variable.[/color]
No, you're not. The only local variable is 'myChr' and you're not
returning a reference nor a pointer to 'myChr'; you're returning the
content of 'myChr', that is a pointer to some dynamically allocated
memory.
You'll need to call delete[] on that pointer at some point to avoid
memory leak:
char *p = ltoa("123");
// do something with p...
delete[] p;
--
___________ 03/07/2005 09:36:06
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763