comp.lang.ada
 help / color / mirror / Atom feed
From: "Warren W. Gay VE3WWG" <ve3wwg@home.com>
Subject: Re: memory leakages with Ada?
Date: Mon, 18 Mar 2002 17:54:45 GMT
Date: 2002-03-18T17:54:45+00:00	[thread overview]
Message-ID: <3C9629E3.8030109@home.com> (raw)
In-Reply-To: a6svum$5u8$1@nh.pace.co.uk

Marin David Condic wrote:

...

> But being an active C programmer at the moment (and sadly shaking my head
> and telling my associates "It doesn't have to be this way..." as we spend
> months debugging and chasing among other typical C problems - memory leaks)
> I find myself having to deal with dynamically allocated data in the most
> mundane of circumstances - often because that's just what many of the
> library calls return to you. (The libraries we are using - not necessarily
> the standard C libraries.) You just find yourself doing mallocs (Bumper
> sticker: "Free The Mallocs!") all over the place and referencing things with
> pointers all the time just because that's the way C wants you to do it. Ada
> doesn't make it necessary to use dynamic allocation or pointers to do
> mundane tasks.
> 
> MDC

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;
}

This is ugly because the user must supply a receiving buffer. However, this
is also the safest and thread safe way to do it, provided the caller has not
made a size error on the supplied buffer. Another variation is to supply a
buffer size, and then somehow decide how to deal with strings that are too
long (truncate, abort, or whatever).

In Ada95, this mundane issue is mundane and easy :

function Uppercase(S : String) return String is
    UC : String(1..S'Length) := S;
begin
    ...uppercase UC...
    return UC;
end Uppercase;

The caller has equal convenience :

declare
    UC : String := Uppercase("Take that!");
begin
    ...

This is so much easier, and most important *reliable*.
-- 
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg




  reply	other threads:[~2002-03-18 17:54 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-03-14 14:07 memory leakages with Ada? Calvin Ow
2002-03-14 14:31 ` Larry Kilgallen
2002-03-14 20:42   ` Nick Roberts
2002-03-14 21:11     ` Larry Kilgallen
2002-03-14 21:07   ` Anh_Vo
2002-03-14 20:12 ` Marin David Condic
2002-03-15  9:37   ` John McCabe
2002-03-15 12:55     ` Pat Rogers
2002-03-16  4:36       ` Will
2002-03-16  4:53         ` Pat Rogers
2002-03-16 12:21         ` Larry Kilgallen
2002-03-16  9:13       ` DPH
2002-03-16 14:38         ` Pat Rogers
2002-03-16 14:56           ` DPH
2002-03-16 15:51             ` Preben Randhol
2002-03-16 16:39               ` DPH
2002-03-16 19:51                 ` Pat Rogers
2002-03-16 20:40                   ` DPH
2002-03-17 19:31                   ` Richard Riehle
2002-03-17 21:49                     ` Pat Rogers
2002-03-17 22:02                       ` Pat Rogers
2002-03-18 22:32                         ` Randy Brukardt
2002-03-18 22:47                           ` Pat Rogers
2002-03-18  7:22                       ` Richard Riehle
2002-03-18 17:35                     ` Marin David Condic
2002-03-17 16:26                 ` Steve Doiel
2002-03-16 20:18         ` Robert A Duff
2002-03-16 20:36           ` DPH
2002-03-15 14:20     ` Marin David Condic
2002-03-18 17:54       ` Warren W. Gay VE3WWG [this message]
2002-03-18 19:54         ` Hyman Rosen
2002-03-18 20:34           ` Larry Kilgallen
2002-03-18 21:18             ` Hyman Rosen
2002-03-18 21:45               ` Larry Kilgallen
2002-03-20  1:19                 ` Hyman Rosen
2002-03-20 17:06                   ` Warren W. Gay VE3WWG
2002-03-20 17:56                     ` Larry Kilgallen
2002-03-20 17:48                   ` Marin David Condic
2002-03-22  0:25               ` Matthew Woodcraft
2002-03-22  5:10                 ` Hyman Rosen
2002-03-18 22:18         ` Marin David Condic
2002-03-20 20:49         ` Bertrand Augereau
2002-03-21  4:31         ` Will
2002-03-15 16:00     ` Hyman Rosen
2002-03-15 21:59       ` Chad R. Meiners
2002-03-17  5:43         ` Kevin Cline
2002-03-17  7:22           ` Chad R. Meiners
2002-03-18  4:09             ` Kevin Cline
2002-03-18 16:54               ` Chad R. Meiners
2002-03-18 17:38             ` Warren W. Gay VE3WWG
2002-03-19  9:21               ` John McCabe
2002-03-19 17:11                 ` Warren W. Gay VE3WWG
2002-03-19 17:16                   ` Pat Rogers
2002-03-19 17:51                   ` David C. Hoos
2002-03-19 18:20                   ` Frank J. Lhota
2002-03-19 23:43                     ` Mark Johnson
2002-03-20 15:09                       ` Frank J. Lhota
2002-03-17  7:27           ` Hyman Rosen
2002-03-18  3:52             ` Kevin Cline
2002-03-18  5:37               ` Hyman Rosen
2002-03-15 17:41   ` Kevin Cline
2002-03-15 18:00     ` Marin David Condic
2002-03-15 18:08     ` Hyman Rosen
2002-03-16 10:15       ` Kevin Cline
2002-03-14 23:14 ` Kevin Cline
2002-03-15  3:20 ` Steve Doiel
2002-03-15  9:32   ` John McCabe
2002-03-15 15:46     ` Hyman Rosen
2002-03-15 17:29     ` Kevin Cline
2002-03-15 15:48   ` Jeffrey Carter
2002-03-16  3:05     ` Steve Doiel
2002-03-16 20:19       ` Jeffrey Carter
2002-03-15 17:25   ` Kevin Cline
2002-03-15 18:03     ` Hyman Rosen
2002-03-16 10:07       ` Kevin Cline
2002-03-17  3:00         ` Hyman Rosen
2002-03-15  9:27 ` John McCabe
  -- strict thread matches above, loose matches on Subject: below --
2002-03-20  6:25 Christoph Grein
2002-03-20 16:35 ` Hyman Rosen
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox