From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,345a8b767542016e X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-03-20 20:31:56 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: wv9557@yahoo.com (Will) Newsgroups: comp.lang.ada Subject: Re: memory leakages with Ada? Date: 20 Mar 2002 20:31:56 -0800 Organization: http://groups.google.com/ Message-ID: <4a885870.0203202031.6eedf2ca@posting.google.com> References: <3c90af1e@news.starhub.net.sg> <3c91bfa3.1987537@news.demon.co.uk> <3C9629E3.8030109@home.com> NNTP-Posting-Host: 199.173.188.113 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1016685116 19080 127.0.0.1 (21 Mar 2002 04:31:56 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 21 Mar 2002 04:31:56 GMT Xref: archiver1.google.com comp.lang.ada:21506 Date: 2002-03-21T04:31:56+00:00 List-Id: "Warren W. Gay VE3WWG" wrote in message news:<3C9629E3.8030109@home.com>... > > This was actually one of my first observations when I started learning Ada95. > A C function does not have a simple way to return a string (a few basic > choices exist, though one can vary the theme somewhat). C++ gets around this > with objects, but they end up resorting to malloc() inside of the object to > carry this off. (excuse the lack of const keywords in these C examples) : > > /* The malloc approach */ > char *uppercase(char *s) { > char *buf = strdup(s); /* Clone the string for modification */ > > ...do uppercasing on buf... > return buf; > } > > The caller gets a dynamically allocated string back, and must free it when > he is finished with it. Ug. > > /* The dirty old static buffer approach */ > char *uppercase(char *s) { > static char my_big_buf[4096]; > > strcpy(my_big_buf,s); > ...do uppercasing on my_big_buf... > return my_big_buf; > } > > This is very bad, because it won't work for all sized strings. It is also > very unsafe in threaded programs. Ug! > > /* The user supplied buffer approach */ > char *uppercase(char *s, char *user_buf) { > strcpy(user_buf,s); > ...do uppercasing in user_buf... > return user_buf; > } The horrible thing about these examples is the absence of bounds checkings.