H
hari
Hi all,
Is it legal to return a local variable from function.
Regards
Hari
Is it legal to return a local variable from function.
Regards
Hari
hari said:Is it legal to return a local variable from function.
hari said:Hi all,
Is it legal to return a local variable from function.
Yes. Returning a pointer to it however is dangerous unless the said
local is static.
In this case it is save to return *str. It is unsafe though to a) cast thearunmib said:Well, I have a doubt. If I allocate memory for a local pointer and
return the address of the pointer to the calling function. Now will
this be dangerous. For eg:
int main()
{
char *test(int i);
char *tmp = NULL;
int i = 10;
tmp = test(i);
printf("%s\n", tmp);
free(tmp);
return 0;
}
char *test(int i)
{
char *str = (char*)malloc(20);
sprintf(str, "i value is: %d", i);
return str;
}
Well, I have a doubt. If I allocate memory for a local pointer and
return the address of the pointer to the calling function. Now will
this be dangerous. For eg:
int main()
{
char *test(int i);
char *tmp = NULL;
int i = 10;
tmp = test(i);
printf("%s\n", tmp);
free(tmp);
return 0;
}
char *test(int i)
{
char *str = (char*)malloc(20);
sprintf(str, "i value is: %d", i);
return str;
}
"arunmib" <[email protected]> schrieb im Newsbeitrag
In this case it is save to return *str.
???Richard Heathfield said:Joachim Schmitz said:
No, it isn't.
arunmib said:Well, I have a doubt. If I allocate memory for a local pointer and
return the address of the pointer to the calling function. Now will
this be dangerous. For eg:
int main()
{
char *test(int i);
char *tmp = NULL;
int i = 10;
tmp = test(i);
printf("%s\n", tmp);
free(tmp);
return 0;
}
char *test(int i)
{
char *str = (char*)malloc(20);
sprintf(str, "i value is: %d", i);
return str;
arunmib said:.... snip ...
Well, I have a doubt. If I allocate memory for a local pointer and
return the address of the pointer to the calling function. Now will
this be dangerous. For eg:
int main() {
char *test(int i);
char *tmp = NULL;
int i = 10;
tmp = test(i);
printf("%s\n", tmp);
free(tmp);
return 0;
}
char *test(int i) {
char *str = (char*)malloc(20);
sprintf(str, "i value is: %d", i);
return str;
}
Thanks for all your replies.
The code I gave was only to make the understanding of my question's
concept and thats it, nothing more nothing less. No intention for
compiling or doing any other thing with the code. yeah, i know you
need to do #includes and about casting of 'malloc' return,
use snprintf instead of sprintf etc..
arunmib said:Thanks for all your replies.
The code I gave was only to make the understanding of my question's
concept and thats it, nothing more nothing less. No intention for
compiling or doing any other thing with the code.
In this case it is save to return *str.
It is unsafe though to a) cast the result of the malloc,
7? How's that? More than 19, yes, or negative more than 18. That wouldb) not check whether it succeededs and then writing to
/ read from the unchecked result,
Yes.
c) pass an int to that function that
requires more than 7 characters in decimal
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.