comp.lang.ada
 help / color / mirror / Atom feed
* Interfacing Ada95 to C++ - Object passing problem
@ 2002-04-25 11:44 Markus Knauss
  2002-04-25 12:39 ` Neil Butterworth
  2002-04-25 17:50 ` Stephen Leake
  0 siblings, 2 replies; 5+ messages in thread
From: Markus Knauss @ 2002-04-25 11:44 UTC (permalink / raw)


Hello,

I'm going to use Ada95 libraries in C++. During evaluation of the Ada
interfacing capabilities I found an interessting problem when passing
objects.

I hope that anybody knows about the described problem and can me tell
where I made an error.

First I will introduce snippets of the sources and then describe the
problem and its solution.

The body and spec of the Ada implementation defines a class with a
single attribute. The attribute is an unbounded_string. The attribute
of an object of this class can be set with the "set" method. Below
excerpts from the spec and body file:

<snip (spec-file)>
type s_type is record
  name : ada.strings.unbounded.unbounded_string;
end record;
pragma convention (c, s_type);
type s_obj is access s_type;
<snap>

<snip (body-file)>
function create
  return s_obj is
begin
  return new s_type;
end create;

procedure set (obj : in out s_obj; name : string) is
begin
  obj.name := to_unbounded_string (name);
end set;
procedure set_c (obj : in out s_obj; name :
interfaces.c.pointers.chars_ptr) is
begin
  set (obj, value (name));
end set_c;
pragma export (c, set_c, "set");
<snap>
The class and the exported function is reflected in a C-Header file.
The class and function is accessed from a C++ main. Below are excerpts
of the header and the main file.

<snip (header file)>
struct s_type {};
typedef s_type* s_obj;
extern "C" s_obj create();
extern "C" void set(s_obj obj, char* name);
<snap>

<snip (file including main)>
extern "C" void adainit();
int main(int argc, char** argv) {
  adainit();
  s_obj obj = create();  set(obj, "Hello world!");
}
<snap>
The sources were compiled using gnat 3.13p and gcc 2.95.3.

Now the problem: When converting the string to an unbounded string, in
the set procedure, the program crashes with an segmentation fault. To
this point everything works fine. When omitting the attributes "in
out" to the obj parameter in the procedures set_c and set everything
works fine.

As I investigated, the Segmentation fault occurs when freeing some
memory in the implementation of To_Unbounded_String. I tested the same
implementation with Bounded_Strings and the result was: If using 'in
out' attributes for obj, the objects attribute was empty but
assignment and conversion worked. Omitting the 'in out' attributes
everything worked fine.

Does anyone know about this or knows why the attributes 'in out' must
be omitted?

Thanks for your answers,
  Markus



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

* Re: Interfacing Ada95 to C++ - Object passing problem
  2002-04-25 11:44 Interfacing Ada95 to C++ - Object passing problem Markus Knauss
@ 2002-04-25 12:39 ` Neil Butterworth
  2002-04-25 17:50 ` Stephen Leake
  1 sibling, 0 replies; 5+ messages in thread
From: Neil Butterworth @ 2002-04-25 12:39 UTC (permalink / raw)



"Markus Knauss" <markus.knauss@gmx.net> wrote in message
news:6c12025b.0204250344.5944d770@posting.google.com...
> Hello,
>
> I'm going to use Ada95 libraries in C++.

Standard C++, the topic of this newsgroup, has nothing to say about using
Ada libraries. You need to post this question to a newsgroup that supports
your specific C++/Ada development environment.

NeilB







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

* Re: Interfacing Ada95 to C++ - Object passing problem
  2002-04-25 11:44 Interfacing Ada95 to C++ - Object passing problem Markus Knauss
  2002-04-25 12:39 ` Neil Butterworth
@ 2002-04-25 17:50 ` Stephen Leake
  2002-04-26  6:20   ` Markus Knauss
  1 sibling, 1 reply; 5+ messages in thread
From: Stephen Leake @ 2002-04-25 17:50 UTC (permalink / raw)


markus.knauss@gmx.net (Markus Knauss) writes:

> The body and spec of the Ada implementation defines a class with a
> single attribute. The attribute is an unbounded_string. The attribute
> of an object of this class can be set with the "set" method. Below
> excerpts from the spec and body file:
> 
> <snip code that tries to pass C strings to Ada code>

The C value "Hello World" is _not_ of the Ada type String. String is
an Ada unconstrained array; it has bounds with it. If you want to pass
a C string to Ada code, you need to use the package
Interfaces.C.Strings, or make some compiler-dependent guesses about
how Ada strings are actually stored.

I suggest you start with very simple C code and very simple Ada code,
and figure out how to pass strings back and forth. then add the
Unbounded strings and stuff.

-- 
-- Stephe



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

* Re: Interfacing Ada95 to C++ - Object passing problem
  2002-04-25 17:50 ` Stephen Leake
@ 2002-04-26  6:20   ` Markus Knauss
  2002-04-27 13:50     ` Jacob Sparre Andersen
  0 siblings, 1 reply; 5+ messages in thread
From: Markus Knauss @ 2002-04-26  6:20 UTC (permalink / raw)


Stephen Leake <stephen.a.leake.1@gsfc.nasa.gov> wrote in message 
> The C value "Hello World" is _not_ of the Ada type String. String is
> an Ada unconstrained array; it has bounds with it. If you want to pass
> a C string to Ada code, you need to use the package
> Interfaces.C.Strings, or make some compiler-dependent guesses about
> how Ada strings are actually stored.

That's correct. As you can see in the code I'm passing the String
using Interfaces.C.Strings. This is done through the procedure
"set_c". The procedure is exported with the unmangled name "set". The
example gets a little obfuscated by that, my fault.

The problem is with passing complex objects. In the example it is the
Greeting object. Passing simple types, strings and arrays works fine.

My question is why the "in out" attributes must be omitted to get it
all working? I do not know why and so I'm asking if anybody knows what
is going on.

Thanks for your response,
  Markus



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

* Re: Interfacing Ada95 to C++ - Object passing problem
  2002-04-26  6:20   ` Markus Knauss
@ 2002-04-27 13:50     ` Jacob Sparre Andersen
  0 siblings, 0 replies; 5+ messages in thread
From: Jacob Sparre Andersen @ 2002-04-27 13:50 UTC (permalink / raw)


Markus Knauss wrote:

> My question is why the "in out" attributes must be omitted to get it
> all working? I do not know why and so I'm asking if anybody knows what
> is going on.

Just a wild guess: Because your Ada compiler only uses a
pointer to transfer a tagged record when it is a "in"
parameter, and your C++ compiler always uses pointers for
transferring of classes.

Jacob
-- 
Sk�ne Sj�lland Linux User Group - http://www.sslug.dk/
N�ste m�de: Linux til min mor.
Torsdag den 2. maj 2002 p� Malm� H�gskola, Citadellsv�gen 7.



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

end of thread, other threads:[~2002-04-27 13:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-25 11:44 Interfacing Ada95 to C++ - Object passing problem Markus Knauss
2002-04-25 12:39 ` Neil Butterworth
2002-04-25 17:50 ` Stephen Leake
2002-04-26  6:20   ` Markus Knauss
2002-04-27 13:50     ` Jacob Sparre Andersen

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