Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > return of pointer

Reply
Thread Tools

return of pointer

 
 
Sergey Koveshnikov
Guest
Posts: n/a
 
      11-04-2003
Hello,
If my function return a pointer on a memory area, where do I free it?
e.x.:
char *foo()
{
char *p;
p = malloc(10);
strcpy(p, "something");
return(p);
}
void bar()
{
char *p = foo();
printf("%s", p);
free(p);
}
But force anybody to calling free() after use foo() not elegant solution,
for my mind... How can I avoid posible memory leaks if somebody forget to
call free()?

Thanks a lot!
--
Sergey Koveshnikov.
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      11-04-2003
In article <bo7re9$r3c$(E-Mail Removed)>, Sergey Koveshnikov wrote:
> Hello,
> If my function return a pointer on a memory area, where do I free it?
> e.x.:
> char *foo()
> {
> char *p;
> p = malloc(10);
> strcpy(p, "something");
> return(p);
> }
> void bar()
> {
> char *p = foo();
> printf("%s", p);
> free(p);
> }
> But force anybody to calling free() after use foo() not elegant solution,


Maybe not, but as long as you are consistent and document the
interface it works well.

> for my mind... How can I avoid posible memory leaks if somebody forget to
> call free()?


You can't force someone to remember to call free().

Creating a foo_alloc() and a foo_free() function might help
though (untested):

#include <stdlib.h>
#include <stdio.h>

int foo_alloc(char **p)
{
/* sanity checks of *p here */

*p = malloc(10);
if (*p == NULL) return 1;
strcpy(*p, "something");
return 0;
}

int foo_free(char **p)
{
/* sanity checks of *p here */

free(*p);
*p = NULL;

return 0;
}

void bar()
{
char *p;

foo_alloc(&p);
printf("foo: %s\n", p);

foo_free(&p);
}




--
Andreas Kähäri
 
Reply With Quote
 
 
 
 
Sergey Koveshnikov
Guest
Posts: n/a
 
      11-04-2003
i.e. No way to do automatic cleanup dynamic allocated memory, if it's
unnecessary anywhere, except context of 'bar()', isn't it?
--
Sergey Koveshnikov.
 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      11-04-2003
In article <bo83p3$qkh$(E-Mail Removed)>, Sergey Koveshnikov wrote:
> i.e. No way to do automatic cleanup dynamic allocated memory, if it's
> unnecessary anywhere, except context of 'bar()', isn't it?


There is no built-in garbage collector in C, no.

--
Andreas Kähäri
 
Reply With Quote
 
Sergey Koveshnikov
Guest
Posts: n/a
 
      11-04-2003
But, what can I do if some foreign code expect from my function that it will
return char*, and it doesn't call free()? Do you know any 'save string'
library for ANSI C that provide self-destroyed strings?
May be it's possible to solve this problem with macros or 'static' type?

PS. Sorry if my questions are stupid, I'm new in the C.
--
Sergey Koveshnikov.
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      11-04-2003
Sergey Koveshnikov <(E-Mail Removed)> wrote:

> Hello,
> If my function return a pointer on a memory area, where do I free it?
> e.x.:
> char *foo()
> {
> char *p;
> p = malloc(10);
> strcpy(p, "something");
> return(p);
> }
> void bar()
> {
> char *p = foo();
> printf("%s", p);
> free(p);
> }
> But force anybody to calling free() after use foo() not elegant solution,
> for my mind... How can I avoid posible memory leaks if somebody forget to
> call free()?


Maybe it's a good idea to adopt the bevaviour of, for example, the
standard library string functions, and let the caller take the full
responsibility for memory allocation/deallocation. E.g.:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BIG_ENOUGH 50

char *foo( char *p )
{
if ( p != NULL )
strcpy( p, "something" );
return( p );
}

int main( void )
{
char *p = malloc( BIG_ENOUGH );
if ( foo( p ) != NULL )
printf( "%s\n", p );
free( p );
return 0;
}

Note: you may want foo() to take an additional argument specifying the
buffer size, in order to avoid buffer overflows.

OTOH, if you, for example, want to imitate the non-standard strdup
function, you of course have to allocate the memory inside the function,
document the behaviour and hopefully the user of the function will get
it right.

HTH
Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      11-04-2003
In article <bo89eu$dc4$(E-Mail Removed)>, Sergey Koveshnikov wrote:
> But, what can I do if some foreign code expect from my function that it will
> return char*, and it doesn't call free()? Do you know any 'save string'
> library for ANSI C that provide self-destroyed strings?
> May be it's possible to solve this problem with macros or 'static' type?
>
> PS. Sorry if my questions are stupid, I'm new in the C.


You, as a library implementor, are not required to make sure
that the users of the library writes correct code.

Document the correct usage (whether to explicitly free() storage
or to call a special cleanup function), and that's the end of
your troubles.

It is possible to return a pointer to a static array, but you
will have to make sure that you point this out to the library
user as well, so that he/she doesn't wander off and tries to
free() that pointer, or so that he/she doesn't store its value
somewhere and gets confused when the data it points to suddenly
changes after the next call to your function.

My favourite solution is to provide two functions; one for
allocation and one for deallocation. This is a bit too involved
if it's just a question of a simple string and not a struct or
some opaque data type.

--
Andreas Kähäri
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      11-04-2003
In <bo7re9$r3c$(E-Mail Removed)> Sergey Koveshnikov <(E-Mail Removed)> writes:

>Hello,
>If my function return a pointer on a memory area, where do I free it?
>e.x.:
>char *foo()
>{
> char *p;
> p = malloc(10);
> strcpy(p, "something");
> return(p);
>}
>void bar()
>{
> char *p = foo();
> printf("%s", p);
> free(p);
>}
>But force anybody to calling free() after use foo() not elegant solution,
>for my mind... How can I avoid posible memory leaks if somebody forget to
>call free()?


This is a perfectly valid approach. No matter where the memory is
dynamically allocated, someone may forget to release it. This is an
intrinsic issue with dynamically allocated memory.

The alternative is to pass to foo the address of the buffer. If the
buffer was not dynamically allocated, no need to release it:

#define STRING "something"

void foo(char *p)
{
strcpy(p, STRING);
}

void bar()
{
char p[sizeof STRING];
foo(p);
printf("%s\n", p);
}

Of course, this doesn't work if only the called function can evaluate
the size of the buffer.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Morris Dovey
Guest
Posts: n/a
 
      11-04-2003
Sergey Koveshnikov wrote:

> Hello,
> If my function return a pointer on a memory area, where do I free it?
> e.x.:
> char *foo()
> {
> char *p;
> p = malloc(10);
> strcpy(p, "something");
> return(p);
> }
> void bar()
> {
> char *p = foo();
> printf("%s", p);
> free(p);
> }
> But force anybody to calling free() after use foo() not elegant solution,
> for my mind... How can I avoid posible memory leaks if somebody forget to
> call free()?
>
> Thanks a lot!


Sergey...

You may be able to write a wrapper for malloc() that keeps a list
of all allocations it makes, then write a function to call free()
for each of the allocations in that list. Refine as necessary.

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall very far from the tree.

 
Reply With Quote
 
James Hu
Guest
Posts: n/a
 
      11-04-2003
On 2003-11-04, Sergey Koveshnikov <(E-Mail Removed)> wrote:
> Hello,
> If my function return a pointer on a memory area, where do I free it?
> e.x.:
> char *foo()
> {
> char *p;
> p = malloc(10);
> strcpy(p, "something");
> return(p);
> }
> void bar()
> {
> char *p = foo();
> printf("%s", p);
> free(p);
> }
> But force anybody to calling free() after use foo() not elegant solution,
> for my mind... How can I avoid posible memory leaks if somebody forget to
> call free()?
>
> Thanks a lot!


You can design a callback interface to someone that wants the
memory:

void foo(void (*cb)(char *))
{
char *p = malloc(10);
if (p)
strcpy(p, "something");
cb(p);
free(p);
}

void mycallback(char *p)
{
printf("%s", p);
}

void bar(char *p)
{
foo(mycallback);
}

-- James
 
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
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
Which are the best manner to return a pointer to a pointer of two-dim array? decker C Programming 9 11-10-2007 12:17 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments