How to access memory allocated in function 
Author Message
 How to access memory allocated in function

Hello

How can i access memory allocated dynamically in a function after i leave the
function, something like this:

main()
{
int *p;
sub(p);

Quote:
}

void sub(int *p)
{
 p = malloc(.....);

Quote:
}

Thanks !


Fri, 03 Jul 1998 03:00:00 GMT  
 How to access memory allocated in function

Quote:

>Hello
>How can i access memory allocated dynamically in a function after i leave the
>function, something like this:

You cannot, because pointers are passed by value like all other objects
in C.

Quote:
>main()
>{
>int *p;
>sub(p);

No prototype for sub() has been seen, so sub() will be called as
a function taking an unspecified number of parameters and returning
int, i.e. as if sub() had been declared as "int sub();".

This may be a problem because sub does not return an int.

Quote:
>}
>void sub(int *p)
>{
> p = malloc(.....);
>}

If you want assigments to a variable passed to a function to have
effect after the function returns, assigning to the function
parameter is _not_ enough.

Two persons once wrote a book about C. In this book they state
that you should think about function parameters as conveniently
initialized local variables of your function. Since the same
two persons also "invented" the C programming language, maybe
we should trust them.

Consider:

   void
   sub(int n)
   {
      n = 4;
   }

   main()
   {
      int m;

      sub(m);
      return 0;
   }

Would you expect m to be set to 4 after the call to sub()? This
situation is very similar, if not identical, to the posted example.

The most common approaches to pass back a value from a function
to the caller are

 1) return the value from the function or
 2) pass a pointer to a variable to the function and let the
    function set that variable to the desired value.

The second method mimics "call by reference", as known from other
languages.

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-6254




Fri, 03 Jul 1998 03:00:00 GMT  
 How to access memory allocated in function

Quote:

> Hello

> How can i access memory allocated dynamically in a function after i leave the
> function, something like this:

> main()
> {
> int *p;
> sub(p);

sub(&p);

Quote:
> }

> void sub(int *p)

void sub(int **p)

Quote:
> {
>  p = malloc(.....);

*p = malloc(...);

Quote:
> }

  C always passes by value; if you want to change a pointer in a
function, you have to pass the function a pointer to that pointer.

  Clear? :)

/**James Robinson***********************            
  "If a fatal error occurs, the program should not be allowed to continue."



Fri, 03 Jul 1998 03:00:00 GMT  
 How to access memory allocated in function

Quote:

>Hello

>How can i access memory allocated dynamically in a function after i leave the
>function, something like this:

>main()
>{
>int *p;
>sub(p);
>}

>void sub(int *p)
>{
> p = malloc(.....);
>}

>Thanks !

The way your program is written, you can't.  This program would create a
memory leak.  The pointer p in the sub() function is a temporary copy of
the contents of the p pointer from the main() function.  The malloc()
will allocate memory and store a pointer to the memory in the sub()'s p
pointer, but that p pointer will be deleted when the function exits.
Here are 2 different ways to allocate memory in a subroutine and return a
pointer to the memory.

int* sub() /* this version returns a pointer to the allocated memory */
   {
   return  (int*) malloc(sizeof(int));
   }

int main ()
   {
   int *p;

   p = sub();
   *p = 3;  /* just store a 3 in the memory */
   return 0;
   }

or:

void sub (int **p) /* this version takes a pointer to the pointer p */
   {
   *p = (int*) malloc (sizeof(int));
   }

int main ()
   {
   int *p;

   sub(&p);
   *p = 3;
   return 0;
   }

If you are using a C++ compiler, you can use a version that is closer to
your original code by using a reference to the main's pointer.  

void sub (int& p)
   {
   p = (int*) malloc (sizeof(int));
   }
int main()
   {
   int *p;

   sub (p);
   *p = 3;
   return 0;
   }  

--
***********************************************

Digital Equipment Corporation
All opinions expressed are my own and should
not be assumed to be the opinion of my employer



Fri, 03 Jul 1998 03:00:00 GMT  
 How to access memory allocated in function

:>
:> How can i access memory allocated dynamically in a function after i leave
:> the function,

: int* sub() /* this version returns a pointer to the allocated memory */
:    {
:    return  (int*) malloc(sizeof(int));
             ^^^^^^
The cast is unnecessary in ANSI C and should be removed See FAQ section 7.7.

:    }

: If you are using a C++ compiler, you can use a version that is closer to
: your original code by using a reference to the main's pointer.  

: void sub (int& p)
:    {
:    p = (int*) malloc (sizeof(int));
:    }

This is not really the approriate group for this - and its wrong.
'p' is just a reference to an int it needs to be a reference to a pointer
to an int.

void
sub(int*& p)
{
    p = (int*)malloc(sizeof(int));

Quote:
}

And in C++ the cast is necessary. ;')

Regards

   -A.

--
| A.Champion                |



Fri, 03 Jul 1998 03:00:00 GMT  
 How to access memory allocated in function

Quote:

> Hello

> How can i access memory allocated dynamically in a function after i leave the
> function, something like this:

> main()
> {
> int *p;
> sub(p);
> }

> void sub(int *p)
> {
>  p = malloc(.....);
> }

> Thanks !

How about:

void sub( int ** ppI )
{
    *ppI = malloc( sizeof( int ) );

    if( *ppI != NULL )
    {
        **ppI = 42;
    }

Quote:
}

main()
{
    int * pI;

    sub( &pI );

    if( pI != NULL )
    {
        printf( "%d\n", *pI );
    }
    else
    {
        printf( "malloc failed" );
    }

Quote:
}

which should print the answer to the ultimate question of life the universe and
everything - if given enough memory :-).

HB.



Fri, 03 Jul 1998 03:00:00 GMT  
 How to access memory allocated in function

says :

Quote:

>Hello

>How can i access memory allocated dynamically in a function after i leave the
>function, something like this:

>main()
>{
>int *p;
>sub(p);
>}

>void sub(int *p)
>{
> p = malloc(.....);
>}

>Thanks !

There is no way to free that memory unless you do it in the sub itself. Anyway in the
above example the local copy of 'p' in sub() will hold the result of malloc() and
forget it as soon as the function terminates.

here is an example how to allocate in a function and use the memory in the caller.

#include <stdlib.h>

void sub(int **pp)
{
*pp = malloc(.......);

Quote:
}

int main(void)
{
int *p;
sub(&p);
/* do whatever with block pointed by p */

...
...
...

free(p);

Quote:
}

--
/* --------------------------------------------------------

http://www.eskimo.com/~mag/index.html
***********************************************************
To understand recursion one must first understand recursion
***********************************************************
-------------------------------------------------------- */


Mon, 06 Jul 1998 03:00:00 GMT  
 How to access memory allocated in function
Quote:

>Hello

>How can i access memory allocated dynamically in a function after i leave the
>function, something like this:

>main()
>{
>int *p;
>sub(p);
>}

>void sub(int *p)
>{
> p = malloc(.....);
>}

>Thanks !

Im sorry ive mailed the person and followed it up here.. *sigh* ok here it goes
again..

p = (int *)malloc(sizeof(int));
What we've done is cast it as an pointer to an integer then allocated it
the size of one int (thus it points to a space in memory that has the size
of holding 1 integer). Ofcourse anything might be in that memory address
you'd need to place something there to clear it up.
--
Thamer Al-Herbish
(ShadowS)
"{*filter*}age angst paid off well.. now I'm bored and old."- Kurt Cobain.
-------------------------------------------------------------------------------



Thu, 08 Oct 1998 03:00:00 GMT  
 How to access memory allocated in function

Quote:

>>How can i access memory allocated dynamically in a function after i leave the
>>function, something like this:

>>void sub(int *p)
>>{
>> p = malloc(.....);
>>}

>p = (int *)malloc(sizeof(int));
>What we've done is cast it as an pointer to an integer then allocated it
>the size of one int (thus it points to a space in memory that has the size
>of holding 1 integer). Ofcourse anything might be in that memory address
>you'd need to place something there to clear it up.

As a solution to the original posters problem, this is entirely useless,
but it is not completely useless, since it can still be used as a bad
example :-).

The problem that pointers are passed by value like all other variables in
C is treated in the FAQ for comp.lang.c in the question named
"I have a function which accepts, and is supposed to initialize, a pointer"
using somewhat archaic syntax. For those of us who cannot decipher
K&R C, Steve Summit describes:

   void f(int *ip)
   {
      satic int dummy = 5;
      ip = &dummy;
   }

Casting the return value of malloc() is not a useful tip for standard
C, but it may be needed for older implementations. An implementation
that accepts "void sub(int *p)" but declares malloc() as returning
something other than a "void *" in <stdlib.h> might be a rare
collectors item.

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-6254



Thu, 08 Oct 1998 03:00:00 GMT  
 How to access memory allocated in function

If you want to change the value of "p", which is actually an address, inside
the function and access it later, then you will have to pass a pointer to "p"
instead of just "p" to the function. So your function must be "sub(int **p)"



: >Hello
: >
: >
: >How can i access memory allocated dynamically in a function after i leave the
: >function, something like this:
: >
: >main()
: >{
: >int *p;
: >sub(p);
: >}
: >
: >void sub(int *p)
: >{
: > p = malloc(.....);
: >}
: >
: >Thanks !
: Im sorry ive mailed the person and followed it up here.. *sigh* ok here it goes
: again..

: p = (int *)malloc(sizeof(int));
: What we've done is cast it as an pointer to an integer then allocated it
: the size of one int (thus it points to a space in memory that has the size
: of holding 1 integer). Ofcourse anything might be in that memory address
: you'd need to place something there to clear it up.
: --
: Thamer Al-Herbish
: (ShadowS)
: "{*filter*}age angst paid off well.. now I'm bored and old."- Kurt Cobain.
: -------------------------------------------------------------------------------



Thu, 08 Oct 1998 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. allocating memory in one function and freeing it with another function

2. allocating memory with functions

3. Allocating memory inside a function

4. allocating memory and processing thru function.

5. Do the string functions allocate memory?

6. standard variadic functions allocate memory?

7. getline function that allocates memory on-the-fly

8. allocate memory for function

9. allocate memory in a function

10. C functions that allocate memory

11. Freeing memory allocated inside a function

12. Allocating memory for function argument

 

 
Powered by phpBB® Forum Software