Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > [newbie] return local variable (or pointer to object) from function

Reply
Thread Tools

[newbie] return local variable (or pointer to object) from function

 
 
Mark
Guest
Posts: n/a
 
      01-28-2011
Hi

#include <stdio.h>int foo1(void){ int p; p = 99; return p;}char
*foo2(void){ char buffer[] = "test_123"; return buffer;}int
*foo3(void){ int t[3] = {1,2,3}; return t;}int main(void){ int *p;
char *s; printf("foo1: %d\n", foo1()); printf("foo2: %s\n", foo2());
printf("foo3: %d, %d, %d\n", p[0], p[1], p[2]); return 0;}Compiling all
three cases with "gcc -ansi -pedantic -W -Wall" and the compiler issues
warning messages for foo2() and foo3() and thus foo2 and foo3 prints out
garbage:

warning: function returns address of local variableI thought it is not
allowed to return a local variable, but foo1() works fine and it seems there
is a huge difference between returning pointer to a local object and the
object itself.

Could anybody shed some light on this issue? Thanks in advance!

--
Mark

 
Reply With Quote
 
 
 
 
Mickey Mouse
Guest
Posts: n/a
 
      01-28-2011
On Fri, 28 Jan 2011 11:45:51 +0900, "Mark"
<> wrote:

>Hi
>
>
> #include <stdio.h>


> int foo1(void)
> {
> int p;
>
> p = 99;
> return p;
> }
>
> char *foo2(void)
> {
> char buffer[] = "test_123";
> return buffer;
> }
>
> int *foo3(void)
> {
> int t[3] = {1,2,3};
> return t;
> }
>
> int main(void)
> {
> int *p;
> char *s;
> printf("foo1: %d\n", foo1());
> printf("foo2: %s\n", foo2());
> printf("foo3: %d, %d, %d\n", p[0], p[1], p[2]);
> return 0;
> }


Compiling all
>three cases with "gcc -ansi -pedantic -W -Wall" and the compiler issues
>warning messages for foo2() and foo3() and thus foo2 and foo3 prints out
>garbage:
>
>warning: function returns address of local variableI thought it is not
>allowed to return a local variable, but foo1() works fine and it seems there
>is a huge difference between returning pointer to a local object and the
>object itself.
>
>Could anybody shed some light on this issue? Thanks in advance!


foo2 and foo3 both have the same problem. A variable declaied in a
function only has scope (only valid) in side the function.

int *p is used un initilized but with the expectation that it will
print the return values for foo3.

Try the functions below.



char *foo2(void)
{
char* buffer = malloc(strlen("test_123") + 1);

if ( buffer != NULL)
strcpy(buffer, "test_123");
return buffer;
}

int *foo3(void)
{
int *t = malloc(sizeof(int) * 3);

if (t != NULL)
{
t[0] =1;
t[1] = 2;
t[2] = 3;
}
return t;
}


int main(void)
{
int *p = foo3();
char *s = foo2();

printf("foo1: %d\n", foo1());

if (s != NULL)
{
printf("foo2: %s\n", s);
free(s);
}
else
printf("foo2: returned NULL\n");

if (p != NULL)
{
printf("foo3: %d, %d, %d\n", p[0], p[1], p[2]);
free(p);
}
else
printf("foo3: returned NULL\n");
return 0;
}


 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-28-2011
"Mark" <> writes:
<snip>
> warning: function returns address of local variableI thought it is not
> allowed to return a local variable, but foo1() works fine and it seems
> there is a huge difference between returning pointer to a local object
> and the object itself.


Yes there is. You can't return the object itself. Al you can do (in C)
is return values. Objects are pieces of storage that contain values and
locally declared objects disappear when the function containing the
declaration returns.

From that it should be clear that returning a pointer to a local object
is going to cause trouble -- the object pointed to is about to
disappear. Your foo1 is OK because the line:

return p;

returns the value contained in the object p.

> Could anybody shed some light on this issue? Thanks in advance!


You might find the FAQ useful: http://c-faq.com/

--
Ben.
 
Reply With Quote
 
Angus
Guest
Posts: n/a
 
      01-28-2011
On Jan 28, 2:27*pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> "Mark" <mark_cruzNOTFORS...@hotmail.com> writes:
>
> <snip>
>
> > warning: function returns address of local variableI thought it is not
> > allowed to return a local variable, but foo1() works fine and it seems
> > there is a huge difference between returning pointer to a local object
> > and the object itself.

>
> Yes there is. *You can't return the object itself. *Al you can do (in C)
> is return values. *Objects are pieces of storage that contain values and
> locally declared objects disappear when the function containing the
> declaration returns.
>
> From that it should be clear that returning a pointer to a local object
> is going to cause trouble -- the object pointed to is about to
> disappear. *Your foo1 is OK because the line:
>
> * return p;
>
> returns the value contained in the object p.
>
> > Could anybody shed some light on this issue? Thanks in advance!

>
> You might find the FAQ useful:http://c-faq.com/
>
> --
> Ben.


On my compiler the output is
foo1: 99
foo2: test_123
foo3: 1, 2, 3

I guess on other compilers this will not always work?
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-28-2011
Angus <> writes:

> On Jan 28, 2:27Â*pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>> "Mark" <mark_cruzNOTFORS...@hotmail.com> writes:
>>

<snip>
>> From that it should be clear that returning a pointer to a local object
>> is going to cause trouble -- the object pointed to is about to
>> disappear.

<snip>
>> --
>> Ben.


Best not to quote sig blocks (even short ones).

> On my compiler the output is
> foo1: 99
> foo2: test_123
> foo3: 1, 2, 3
>
> I guess on other compilers this will not always work?


In some sense it may not even work on that compiler in that it probably
works by accident. Small changes to the program could produce all sorts
of odd effects. Even altering the compiler's options could change the
behaviour.

--
Ben.
 
Reply With Quote
 
Mickey Mouse
Guest
Posts: n/a
 
      01-29-2011
On Fri, 28 Jan 2011 17:04:57 -0500, "Scott Fluhrer"
<> wrote:

>Another real possibility is that it works as shown on 99% of the runs, but
>prints gibberish on the other 1%.


Thats OK I can read and write gibberish. After enough beers I can
fluently speak it as well
 
Reply With Quote
 
Angus
Guest
Posts: n/a
 
      02-04-2011
On Jan 28, 10:04*pm, "Scott Fluhrer" <sfluh...@ix.netcom.com> wrote:
> "Ben Bacarisse" <ben.use...@bsb.me.uk> wrote in message
>
> news:0.2a968d7b5d53c1ae37c8.20110128212124GMT.87pq ...
>
>
>
>
>
>
>
>
>
> > Angus <anguscom...@gmail.com> writes:

>
> >> On Jan 28, 2:27 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> >>> "Mark" <mark_cruzNOTFORS...@hotmail.com> writes:

>
> > <snip>
> >>> From that it should be clear that returning a pointer to a local object
> >>> is going to cause trouble -- the object pointed to is about to
> >>> disappear.

> > <snip>
> >>> --
> >>> Ben.

>
> > Best not to quote sig blocks (even short ones).

>
> >> On my compiler the output is
> >> foo1: 99
> >> foo2: test_123
> >> foo3: 1, 2, 3

>
> >> I guess on other compilers this will not always work?

>
> > In some sense it may not even work on that compiler in that it probably
> > works by accident. *Small changes to the program could produce all sorts
> > of odd effects. *Even altering the compiler's options could change the
> > behaviour.

>
> Another real possibility is that it works as shown on 99% of the runs, but
> prints gibberish on the other 1%. *This can happen on real computers that
> use the same stack to hold both automatics and interrupt contexts.
>
> This is Yet Another reason to avoid undefined behavior; just because your
> program works once does not mean that it'll work the next time; even if
> you'll rerun the exact same program...
>
> --
> poncho


I believe it IS ok to return a static variable.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-04-2011
Angus <> writes:
> On Jan 28, 10:04Â*pm, "Scott Fluhrer" <sfluh...@ix.netcom.com> wrote:

[...]
>> Another real possibility is that it works as shown on 99% of the runs, but
>> prints gibberish on the other 1%. Â*This can happen on real computers that
>> use the same stack to hold both automatics and interrupt contexts.
>>
>> This is Yet Another reason to avoid undefined behavior; just because your
>> program works once does not mean that it'll work the next time; even if
>> you'll rerun the exact same program...

>
> I believe it IS ok to return a static variable.


It's not *possible* to return a static variables. As someone already
explained upthread, functions do not return variables; they return
values.

A function may return the *value* of a variable, and that's perfectly ok
(as long as that value doesn't contain a pointer to a local object). Or
it may return the *address* of a variable, which is ok if and only if
the variable's lifetime doesn't end when the current invocation of the
function does.

So what you probably *meant* to say is correct: it's ok for a function
to return the address of a static variable, because such a variable's
lifetime is the entire execution of the program. (But making a variable
static changes its behavior, which may or may not be what you want.)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
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
Is it legal to return a local variable from function. hari C Programming 13 12-02-2007 11:20 PM
Can we make a local variable in a function as global variable??? sairam Python 2 04-05-2007 07:16 PM
return type of a function that returns a local variable Jess C++ 2 03-22-2007 12:30 PM
Is pointer parameter local variable to a function? ali C++ 3 03-07-2007 04:38 AM
How to create an initialise pointer to pointer to function variable; iceColdFire C++ 9 05-23-2005 07:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57