From owner-freebsd-questions Mon May 13 10:42:22 2002 Delivered-To: freebsd-questions@freebsd.org Received: from mail.liwing.de (mail.liwing.de [213.70.188.162]) by hub.freebsd.org (Postfix) with ESMTP id 6827237B401 for ; Mon, 13 May 2002 10:42:13 -0700 (PDT) Received: (qmail 15198 invoked from network); 13 May 2002 17:51:02 -0000 Received: from stingray.liwing.de (HELO liwing.de) ([213.70.188.164]) (envelope-sender ) by mail.liwing.de (qmail-ldap-1.03) with SMTP for ; 13 May 2002 17:51:02 -0000 Message-ID: <3CDFF958.C850E528@liwing.de> Date: Mon, 13 May 2002 19:35:20 +0200 From: Jens Rehsack Organization: LiWing IT-Services X-Mailer: Mozilla 4.78 [en] (Windows NT 5.0; U) X-Accept-Language: en MIME-Version: 1.0 To: Paul Everlund Cc: freebsd-questions@freebsd.org Subject: Re: C malloc question References: Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Paul Everlund wrote: > > Hi all! > > I have a question regarding malloc and returning arrays in C. > > The following C program is an example. > > #include > #include > > 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: 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