A
ali
Hi,
When I pass a pointer as an argument of a method, is it safe if I
change the data pointed by the argument and return it upon
completion ?
For example:
Object* someFunction(Object* ob)
{
//do some manipulation and return
return ob;
}
Are there things I need to be aware/cautious when doing this? I tried
with int pointer and got the results as expected (no error or garbage
output). I had thought it would give me garbage result because to my
undestanding, ob would be a local variable to someFunction, and should
have been destroyed upon return?
I know that if within the body of the function above, I created a
pointer and assigned memory with new operator, I should not return the
pointer because it will be destroyed upon exit and I might be getting
garbage.
I know i'm missing some simple concept. Will appreciate your help.
Thanks,
Ali
When I pass a pointer as an argument of a method, is it safe if I
change the data pointed by the argument and return it upon
completion ?
For example:
Object* someFunction(Object* ob)
{
//do some manipulation and return
return ob;
}
Are there things I need to be aware/cautious when doing this? I tried
with int pointer and got the results as expected (no error or garbage
output). I had thought it would give me garbage result because to my
undestanding, ob would be a local variable to someFunction, and should
have been destroyed upon return?
I know that if within the body of the function above, I created a
pointer and assigned memory with new operator, I should not return the
pointer because it will be destroyed upon exit and I might be getting
garbage.
I know i'm missing some simple concept. Will appreciate your help.
Thanks,
Ali