comp.lang.ada
 help / color / mirror / Atom feed
* memory management
@ 2005-05-26  0:57 alex goldman
  2005-05-26  2:14 ` David C. Hoos, Sr.
  2005-05-26 12:10 ` Robert A Duff
  0 siblings, 2 replies; 23+ messages in thread
From: alex goldman @ 2005-05-26  0:57 UTC (permalink / raw)


As I understood from reading the Ada tutorial for C/C++ programmers,
"access" is essentially like C++ smart pointer, except that you don't need
to do anything to dereference it.

How will the following work:

Record A contains "access" to record B; 
record B contains "access" to record A.

If I create an instance of one of them with "new", will it be destroyed when
"access" to it goes out of scope?




^ permalink raw reply	[flat|nested] 23+ messages in thread
* Re: Memory_Management
@ 2005-04-19 20:30 Anh Vo
  0 siblings, 0 replies; 23+ messages in thread
From: Anh Vo @ 2005-04-19 20:30 UTC (permalink / raw)
  To: baldrick, fracttcarf; +Cc: comp.lang.ada

Or may be the intention was to allocate the memory for component Value of I1 and I2. If this is the case, allocate and assign to Value as shown below. Then, the results should be printed out as expected.

     I1.Value := new Integer' (123);

     I2.Value := new Integer' (456);

By the way, I am the author of the Memory Management. I do have a new and improved version of Memory Management. In addition, I have change the name to Storage Pool Handler. If any one is interested, please let me know with an private email. I will sent it to you.

AV

>>> Duncan Sands <baldrick@free.fr> 04/19/05 2:18 AM >>>
Hi Bini, maybe what is happening is this:

> 	I1 := (AF.Controlled with Value => new Integer'(123));

here the right-hand-side (RHS) is an object of My_Data type.
You assign it to I1, another object of My_Data type.  After
the assignment, RHS is finalized.  This frees the memory - the
same memory I1.Value is pointing to.  In detail:

(0) Memory is allocated to hold the integer 123.  A pointer
to the memory is in RHS.Value.
(1) a bit-wise copy is made of RHS onto I1.  I1.Value points
to the integer.
(2) Adjust is called on I1.  Since you don't seem to have
defined Adjust, this does nothing.
(3) Finalize is called on RHS.  This frees the memory holding
123.  Now I1.Value is pointing to freed memory.

You then look at I1.Value.all, which could be anything.

You need to define Adjust.






^ permalink raw reply	[flat|nested] 23+ messages in thread
* Memory_Management
@ 2005-04-19  1:39 Bini
  2005-04-19  9:18 ` Memory_Management Duncan Sands
  0 siblings, 1 reply; 23+ messages in thread
From: Bini @ 2005-04-19  1:39 UTC (permalink / raw)


with Ada.Finalization;
with Memory_Management;

package Pkg is
	My_Pool : Memory_Management.User_Pool(Size => 500);

	type My_Int is access Integer;
	for My_Int'Storage_Pool use My_Pool;

	type My_Data is new Ada.Finalization.Controlled with record
		Value	: My_Int;
	end record;

	procedure Initialize(Data : in out My_Data);
	procedure Finalize(Data : in out My_Data);
end Pkg;

with Ada.Unchecked_Deallocation;

package body Pkg is
	.
        .
	procedure Finalize(Data : in out My_Data) is
	begin
		Free(Data.Value);
	end Finalize;
        .
        .
end Pkg;

with Ada.Text_IO;
with Ada.Finalization;
with Pkg;
procedure Fun is
	package TIO renames Ada.Text_IO;
	package AF renames Ada.Finalization;

	I1, I2	: Pkg.My_Data;
	I3	: Pkg.My_Int;
begin
	I1 := (AF.Controlled with Value => new Integer'(123));
	TIO.Put_Line(I1.Value.all'Img);
	I2 := (AF.Controlled with Value => new Integer'(456));
	TIO.Put_Line(I2.Value.all'Img);
	I3 := new Integer'(789);
	TIO.Put_Line(I3.all'Img);
end Fun;


===> I write some test code with Memory_Management
package(http://www.adapower.com/index.php?Command=Class&ClassID=Advanced&CID=222)
from Anh Vo

fun.exe result is

0
0
789

and I1.Value'Address and I2.Value'Address is equal.
I can not understand this result.
My English is poor.
Thank You.



^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2005-06-13  4:01 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-26  0:57 memory management alex goldman
2005-05-26  2:14 ` David C. Hoos, Sr.
2005-05-26 13:21   ` Steve
2005-05-26 18:40     ` alex goldman
2005-05-28  2:13       ` Steve
2005-05-28  5:19         ` Jeffrey Carter
2005-05-28 14:48           ` Steve
2005-05-26 18:47     ` Pascal Obry
2005-05-27 14:33   ` Martin Krischik
2005-05-26 12:10 ` Robert A Duff
2005-05-27 14:31   ` Martin Krischik
2005-05-28 11:44     ` Robert A Duff
2005-05-28 13:03       ` Simon Wright
2005-05-31 12:04         ` Robert A Duff
2005-06-02 15:42       ` Thomas Maier-Komor
2005-06-02 17:05         ` Robert A Duff
2005-06-03  1:41       ` Steve
2005-06-03 10:12         ` alex goldman
2005-06-13  4:01         ` Dave Thompson
  -- strict thread matches above, loose matches on Subject: below --
2005-04-19 20:30 Memory_Management Anh Vo
2005-04-19  1:39 Memory_Management Bini
2005-04-19  9:18 ` Memory_Management Duncan Sands
2005-04-20  1:06   ` Memory_Management Bini

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