Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 13 May 2002 19:35:20 +0200
From:      Jens Rehsack <rehsack@liwing.de>
To:        Paul Everlund <tdv94ped@cs.umu.se>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: C malloc question
Message-ID:  <3CDFF958.C850E528@liwing.de>
References:  <Pine.GSO.4.33.0205131845160.22929-100000@gren.cs.umu.se>

next in thread | previous in thread | raw e-mail | index | archive | help


Paul Everlund wrote:
> 
> Hi all!
> 
> I have a question regarding malloc and returning arrays in C.
> 
> The following C program is an example.
> 
> #include <stdlib.h>
> #include <string.h>
> 
> static char *init()
> {
>   char *str;
>   int i;
> 
>   str = (char *)malloc(sizeof(char), 128);
malloc doesn't have 2 params
>   for(i = 0; i < 127; i++)
>     str[i] = "A";
>   str[i] = '\0';
>   return str;
> }
> 
> int main()
> {
>   int i;
> 
>   for(i = 0; i < 500000; i++)
>     init();
>   printf("%s\n", init());
>   return 0;
> }
> 
> This program takes up a lot of malloc'ed memory after a while,
> so how do one return arrays and free memory in the main function
> everytime the array have been used? I would like to not use fix-
> ed size arrays, as the real function should be general enough
> to handle all kind of array sizes.
> 
> As the size is known in the main function, I guess I could send
> in a buffer to get the data, as in init(char *buf), but I would
> like to avoid this.
> 
> What is the best solution for this problem?

Because it's a very small sample, I do not really see the goal...

Maybe a solution is

typedef struct object_t {
  size_t array_size
  char *ptr2array;
} object_t;

static int
init( struct object_t *obj )
{
  int rc = 0;
  if( object )
  {
    if( NULL != obj->ptr2array )
      free( obj->ptr2array );

    if( NULL != ( obj->ptr2array = calloc( WANTED_ARRAY_SIZE, sizeof(char) ) ) )
    {
      obj->array_size = WANTED_ARRAY_SIZE;
      ++rc;
    }
  }

  return rc;  
}
 
int
main(void)
{
  int i;
  object_t obj = { 0, NULL };

  for(i = 0; i < 500000; i++)
    init( &obj ); /* FIXME: check return code */

  printf( "%s\n", obj->ptr2array );

  return 0;
}
 
> Thanks a lot in advance!
> 
> Best regards,
> Paul
> 
> To Unsubscribe: send mail to majordomo@FreeBSD.org
> with "unsubscribe freebsd-questions" in the body of the message

-- 
L     i  W     W     W  i                 Jens Rehsack
L        W     W     W
L     i   W   W W   W   i  nnn    gggg    LiWing IT-Services
L     i    W W   W W    i  n  n  g   g
LLLL  i     W     W     i  n  n  g   g    Friesenstraße 2
                                  gggg    06112 Halle
                                     g
                                 g   g
Tel.:  +49 - 3 45 - 5 17 05 91    ggg     e-Mail: <rehsack@liwing.de>
Fax:   +49 - 3 45 - 5 17 05 92            http://www.liwing.de/

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3CDFF958.C850E528>