Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is it legal to return a local variable from function.

Reply
Thread Tools

Is it legal to return a local variable from function.

 
 
hari
Guest
Posts: n/a
 
      11-19-2007
Hi all,
Is it legal to return a local variable from function.

Regards
Hari

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      11-19-2007
In article <(E-Mail Removed)>,
hari <(E-Mail Removed)> wrote:

>Is it legal to return a local variable from function.


You'll have to be more specific. You can't return variables, only
values. You can certainly return the value of a local variable. You
can't safely return a pointer to a local variable, because it won't
exist after the return.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      11-19-2007
In article
<(E-Mail Removed)>,
hari <(E-Mail Removed)> wrote on Monday 19 Nov 2007 4:57 pm:

> 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.

 
Reply With Quote
 
arunmib
Guest
Posts: n/a
 
      11-19-2007
On Nov 19, 4:34 pm, santosh <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>,
> hari <(E-Mail Removed)> wrote on Monday 19 Nov 2007 4:57 pm:
>
> > 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.


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;
}
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      11-19-2007
"arunmib" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On Nov 19, 4:34 pm, santosh <(E-Mail Removed)> wrote:
>> In article
>> <(E-Mail Removed)>,
>> hari <(E-Mail Removed)> wrote on Monday 19 Nov 2007 4:57 pm:
>>
>> > 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.

>
> 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;
> }

In this case it is save to return *str. It is unsafe though to a) cast the
result of the malloc, b) not check whether it succeededs and then writing to
/ read from the unchecked result, c) pass an int to that function that
requires more than 7 characters in decimal

Bye, Jojo


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-19-2007
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;
> }


First, the bugs. You forgot to #include <stdlib.h> and <stdio.h> - your
compiler would certainly have warned you that something was amiss, if only
you hadn't silenced it with a useless cast. You assume without
justification that the memory allocation request succeeded.

Okay, now to your question.

You haven't allocated any memory "for a local pointer". You've allocated
some memory, full stop. When you do so, you are given a value, which you
can use for referring to that memory. It's a pointer value (which points
to the beginning of the memory you've allocated). You have stored that
pointer value into a pointer object, which is a very right and proper
thing to do.

You have then returned that value, the value of the pointer object, to the
calling function. You have *not* returned the str object, merely its
value, which - if you will recall - points to the beginning of the memory
you allocated. That's absolutely fine, because that memory you allocated
via malloc (if indeed it succeeded at all) will continue to exist until
you explicitly release it by passing that pointer value to free().

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-19-2007
Joachim Schmitz said:

> "arunmib" <(E-Mail Removed)> schrieb im Newsbeitrag

<snip>
>>
>> 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;
>> }

> In this case it is save to return *str.


No, it isn't.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      11-19-2007
"Richard Heathfield" <(E-Mail Removed)> schrieb im Newsbeitrag
news(E-Mail Removed)...
> Joachim Schmitz said:
>
>> "arunmib" <(E-Mail Removed)> schrieb im Newsbeitrag

> <snip>
>>>
>>> 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;
>>> }

>> In this case it is save to return *str.

>
> No, it isn't.

???
Of course I meant 'return str'

Bye, Jojo


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-19-2007
In article
<(E-Mail Removed)>,
arunmib <(E-Mail Removed)> wrote on Monday 19 Nov 2007 5:19 pm:

> On Nov 19, 4:34 pm, santosh <(E-Mail Removed)> wrote:
>> In article
>> <(E-Mail Removed)>,
>> hari <(E-Mail Removed)> wrote on Monday 19 Nov 2007 4:57 pm:
>>
>> > 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.

>
> 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);


Generally it's better to place declarations outside functions.
Specifically, it is needed (not strictly speaking, but calling a
function without a prototype is bad coding for most purposes), if a
function other than main() wants to invoke test().

> 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);


The cast is not recommended practise in C. It can prevent the compiler
from issuing a diagnostic if a prototype for malloc() is not in scope,
as is the case here. That in turn can cause subtle problems, since the
compiler incorrectly assumes that malloc() returns an int, while,
actually, it returns a void *.

Include stdio.h and stdlib.h before the code for this program.

> sprintf(str, "i value is: %d", i);


If the value held in 'i' requires more than seven characters to
represent in decimal this sprintf() call will overwrite memory. For
such cases use snprintf().

> return str;


This is fine since what is being returned is the _value_ in 'str',
not 'str' itself, which ceases to exist after this return. The value
however is copied and assigned to 'tmp' in main().

> }


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-20-2007
arunmib wrote:
>

.... 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;
> }


You didn't return the address of the pointer, you returned the
pointer. All is well (except for the foolish cast on the return
value of malloc, and the lack of #includes, and the missing void in
the main parameter list).

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
return reference to local variable, good code? George2 C++ 15 12-17-2007 11:19 PM
return type of a function that returns a local variable Jess C++ 2 03-22-2007 12:30 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
return reference to local variable ? romerun C++ 6 09-03-2004 03:03 AM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments