comp.lang.ada
 help / color / mirror / Atom feed
From: Brad Moore <brad.moore@shaw.ca>
Subject: Re: newbie: can't read fast enough... :-)    memory leaks...
Date: Wed, 03 Sep 2014 22:57:50 -0600
Date: 2014-09-03T22:57:50-06:00	[thread overview]
Message-ID: <hjSNv.114789$Fo3.103404@fx09.iad> (raw)
In-Reply-To: <1409739464.7121.235.camel@obry.net>

On 2014-09-03 4:17 AM, Pascal Obry wrote:
> Le mercredi 03 septembre 2014 à 02:38 -0700, gdotone@gmail.com a
> écrit :
>> i know i'll get there, currently in chapter 3, of Ada 95 PSPD, anyway, can Ada code have/produce memory leaks?  well, maybe not "can" it but, does Ada, natively prevent memory leaks?
>>
>> like, C seems to leak all over the place, if not managed correctly by the programmer.
>
> This depends on the implementation. Ada make it possible to use a
> garbage collector but as far as I know no implementation is using one.
> So yes, every Ada compiler around will leak memory if not properly
> freed.
>
> To free an allocated object one need to instantiate
> Ada.Unchecked_Deallocation.
>
> Note that well designed object can deallocate themselves when possible
> by using Ada.Finalization API. As an example, Unbounded_String are using
> dynamic allocation but you need need to worry about that as a user.
>

A couple of points on heap allocation that I think newcomers to Ada 
often miss, or fail to appreciate.

Compared to languages like C and C++, I would say Ada reduces the need 
for heap allocation.

For example, In C, for a function that can return a string of variable 
length, a common approach is to allocate memory for the result from the 
heap, and then return the address of the allocated result to the caller, 
since in C, functions essentially return numeric values.
A numeric value might be a char, int, float, or an address of some type.

eg.

char * get_string()
{
    const char *s1 = "The return result";
    char * ptr = malloc(strlen(S1) + 1);
    strcpy(ptr, s1);
    return ptr;
}

or alternatively, pass the address of the buffer as a parameter into the 
function as in...

void get_string(char *buf, int buflen)
{
    const char *s1 = "The return result";
    if (strlen(s1) + 1 > buflen) {
       FATAL_ERR("Buffer too small");
    }
    strcpy(buf, s1);
}

But this relies on the caller knowing the maximum length of the result 
before making the call.

In Ada, unconstrained types such as string types and arrays can be 
easily returned on the stack as a result without allocating from the heap.

eg.

function Get_String return String is
begin
   return "The return result";
end Get_String;

The second point is that another alternative to calling 
Unchecked_Deallocation is to let the finalization of an access type free 
all heap allocations for that type, using a nested scope.

eg.
      declare
         type String_Access is access String;
         A, B : String_Access;
      begin
         A := new String'("String 1");
         B := new String'("String 2");
         ... -- Operate on the data.
      end;
      -- A and B are automatically freed here because the String_Access
      -- is finalized since the scope of the declaration of the access
      -- type is being exited, and finalizing an access type also
      -- finalizes its collection (allocated objects designated by that
      -- type).

This is a nice way to free up a bunch of objects that are no longer 
needed, if it can be guaranteed that the objects are only needed in a 
nested scope. I like this better than garbage collection, because the
deallocation occurs more consistently, when and where it is needed.
I view this as being less error prone, if one is able to take advantage 
of this approach.

There are other techniques and libraries that are helpful in this area,
but I think these two points are a good starting point.

Brad


  parent reply	other threads:[~2014-09-04  4:57 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-03  9:38 newbie: can't read fast enough... :-) memory leaks gdotone
2014-09-03 10:17 ` Pascal Obry
2014-09-03 10:48   ` G.B.
2014-09-03 15:50     ` Pascal Obry
2014-09-03 17:28     ` Robert A Duff
2014-09-03 17:36       ` Pascal Obry
2014-09-03 19:09         ` gdotone
2014-09-03 19:19           ` gdotone
2014-09-03 19:29             ` Dmitry A. Kazakov
2014-09-03 20:19         ` Robert A Duff
2014-09-03 19:19       ` Dmitry A. Kazakov
2014-09-03 20:31         ` Robert A Duff
2014-09-04  7:18           ` Dmitry A. Kazakov
2014-09-04  4:57   ` Brad Moore [this message]
2014-09-04  5:32     ` Jeffrey Carter
2014-09-04  7:23       ` Dmitry A. Kazakov
2014-09-04 14:23       ` Brad Moore
2014-09-04 15:36         ` Brad Moore
2014-09-04 18:28           ` Robert A Duff
2014-09-05  4:58             ` anon
2014-09-05  9:45               ` Simon Wright
2014-09-05 21:54                 ` anon
2014-09-06  6:28                   ` Simon Wright
2014-09-04 15:55         ` G.B.
2014-09-04 16:43         ` Jeffrey Carter
replies disabled

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