Returning pointers from functions ... 
Author Message
 Returning pointers from functions ...

I am hoping a C guru out there can help me. My problem is understanding
what happens when a pointer is returned from a function e.g.:

char *readln(void);

Inside readln I keep getting characters using getc checking that none
are illegal and so on. However, I am unsure about using the:

return(buf)

as this implies to me that a pointer to a locally defined buffer - buf
is actually being returned. As it has been defined locally at the end
of the function the space is made available for later use.

I hope I have explained the problem fairly well; can anyone help?

                        - Chris Trueman.



Mon, 18 Jul 1994 19:26:47 GMT  
 Returning pointers from functions ...

Quote:
(Chris Trueman) writes:
>... However, I am unsure about using the:

>return(buf)

>as this implies to me that a pointer to a locally defined buffer - buf
>is actually being returned.

C includes two major sub-parts, `values' and `objects'.  An object is
something that holds a value, and a value is one of the obvious
things---such as a number like 3.14 or 42---or one of C's more {*filter*}
varieties, such as pointers.

Quote:
>As it has been defined locally at the end of the function the space
>is made available for later use.

The word `it' here dangles---do you mean the buffer, or the variable
`buf'?  Perhaps they are the same, perhaps not; the original article
does not include enough context to say.

One of C's oddities is that an array object `acts up' when you try to
take its value.  Thus, if you have:

        char buf[100];

then `buf' is an array object containing 100 sub-objects, but the
`value' of `buf' is a pointer to the first sub-object, rather than the
100 values of the 100 sub-objects.  This is due to The Rule (detailed
in other articles).  You must memorize The Rule in order to deal with
arrays and pointers correctly, just as you must memorize the rules of
arithmetic in order to add and subtract.  The rules are in some sense
arbitrary, but they make the system work.

On the other hand, perhaps you have:

        char *buf;

In this case buf is not an array object, and its value is straightforward.

In either case, objects in C have lifetimes or *durations*.  For
ordinary variables, there are only two; these are known as `static' and
`automatic'.  Static objects last for the entire program run, while
automatic objects exist only during the execution of their enclosing
block.  Once the block exits, the objects it defined are no longer
valid.  (Whether they still exist anywhere is machine-dependent.  On
most machines, they do, and you can get in trouble because it is hard
to predict when their memory is reused, so your programs appear to work
`randomly'.)

There is an additional storage duration that has no official name; it
is often called `heap space' or `managed memory'.  Space allocated via
malloc() and its friends is good until free()d, or until the program
exits.  This space also consists of objects and can therefore hold
values.

Given the declaration:

        char *buf;

then if buf points to heap space or to a static object,

        return (buf);

returns a valid value.  On the other hand, given:

        char buf[100];

then if buf has static duration,

        return (buf);

returns a valid value (namely &buf[0]), but if buf has automatic
duration, it returns an invalid value (because &buf[0] no longer
exists, at least in principle, by the time the `return' completes).
Similarly, given:

        char xbuf[100];
        ...
        char *buf = &xbuf[0];

then:

        return (buf);

is valid if and only if xbuf still exists after the return (i.e.,
xbuf has static duration or is declared in a still-active block).
--
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)



Tue, 19 Jul 1994 18:57:02 GMT  
 Returning pointers from functions ...

        I am presuming you want to do something in a function that gathers
data into some local variable and exit with a pointer to that data. You can
do this like so:

char *retptr;
char *foo(void);

retptr = foo();

char *foo(void) {
static char invar;
        invar = getchar();
        return(&invar);

Quote:
}

by declaring the local var static, tow things happen, 1: the value of the
variable is retained from one call to another, and 2: it is initialized in
memory only once so its address stays consistent.
        -Brian


Tue, 19 Jul 1994 23:43:01 GMT  
 Returning pointers from functions ...

Quote:

>I am hoping a C guru out there can help me. My problem is understanding
>what happens when a pointer is returned from a function e.g.:
>char *readln(void);
>Inside readln I keep getting characters using getc checking that none
>are illegal and so on. However, I am unsure about using the:
>return(buf)
>as this implies to me that a pointer to a locally defined buffer - buf
>is actually being returned. As it has been defined locally at the end
>of the function the space is made available for later use.
>I hope I have explained the problem fairly well; can anyone help?

This is where the malloc function comes into play.

When you declare the local _buf_ variable something like

        char buf[MAX_BUF_SIZE]; /* or whatever */

Then yes - the integrity of the space allocated for that set of charaters
is only maitained while that invocation of that function is still current.  
When you exit the function the integrity of the space is lost.

However if you declare your local variable as:

        char *buf;

and then call malloc with something like

        buf = (char *)malloc (sizeof (char) * MAX_BUF_SIZE);

(you can miss out the "sizeof (char) *" in this case because it is one (1)
but you might need if you are using a different variable type)

Then the integrity of the space pointed to by _buf_ is maintained after
that invocation of the function.  The _buf_ variable itself is lost
but the space it pointed to is not.  This way you can return _buf_ from
your funtion store it in a local varibale (of type "char *") in the calling
function and still access the array.  The local variable should be
decalred in the same way as _buf_ but there is no need for a malloc
for that variable.

Hope this helps

--
{           Michael Corbett             | TIME: Natures way of         .__o  }

{ If Murphy's Law can go wrong, it will.| happening at once.        (*)/'(*) }



Fri, 22 Jul 1994 09:01:13 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Pointer to function returning pointer to function returning...

2. Question about signal()/pointers to functions that return pointers to functions

3. Function returning pointers to functions (repost)

4. Function returning pointers to functions

5. (member) function returning pointer to functions like itself?

6. functions returning pointers to functions

7. QUESTION: functions returning pointers to functions

8. return pointer to function?

9. Return pointer to function as argument

10. Return pointer to function as arguement.

11. problem returning pointer from function

12. returning pointer from function

 

 
Powered by phpBB® Forum Software