comp.lang.ada
 help / color / mirror / Atom feed
* pointer in C & in  Ada
@ 2001-08-07 22:39 Lin
  2001-08-07 23:05 ` Didier Utheza
                   ` (4 more replies)
  0 siblings, 5 replies; 17+ messages in thread
From: Lin @ 2001-08-07 22:39 UTC (permalink / raw)


If "int *p" declares that pointer p points to integer type, then
what's the meaning of "void *p"? How can I represent it(void *p) in
Ada language?

Many thanks,
Lin



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

* Re: pointer in C & in  Ada
  2001-08-07 22:39 pointer in C & in Ada Lin
@ 2001-08-07 23:05 ` Didier Utheza
  2001-08-08  9:29   ` Florian Weimer
  2001-08-07 23:31 ` tmoran
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 17+ messages in thread
From: Didier Utheza @ 2001-08-07 23:05 UTC (permalink / raw)
  To: Lin

As far as C is concern I can explain the following:
		void function(argument);	is actually a function
that does not return a result. In other word a procedure.

		void *function(arguments); or void *p; are elements that
point to an undefined type.

When you declare a pointer in C, like int *point_to_int; you are stating
two related things. The type int gives you the offset (the number of
memory cells necessary to represent the data, with int let's say 4 bytes
on a 32bits machine). The actual pointer: point_to_int is
refering/pointing to the base cell of rour object (the first cell of the
four that contain your integer).

So what happens when you define your pointer as void *point_to_anything;
you have a pointer that can access any type. This is because you just
define the base cell doing so. The actual offset (given by the type) is
unknown and it your job as the programmer to let the system know about it
(in C, they use the macro function sizeof() that returns the size of the
type-argument).

About Ada95, like you I am in the vague. My guess is that it is not
possible in pure Ada, because on the constraint on type checking and that
you have to use a specific pragma such as unconstrained_checking (I do not
remember the name exactly). Good sources for such examples are the source
codes of the GUI libraries (GtkAda, JEWL,....). But I am sure that
somebody will give you an answer pretty soon.
				Didier Utheza.

On 7 Aug 2001, Lin wrote:

> If "int *p" declares that pointer p points to integer type, then
> what's the meaning of "void *p"? How can I represent it(void *p) in
> Ada language?
> 
> Many thanks,
> Lin
> 
> 




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

* Re: pointer in C & in  Ada
  2001-08-07 22:39 pointer in C & in Ada Lin
  2001-08-07 23:05 ` Didier Utheza
@ 2001-08-07 23:31 ` tmoran
  2001-08-08  3:14 ` DuckE
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 17+ messages in thread
From: tmoran @ 2001-08-07 23:31 UTC (permalink / raw)


> If "int *p" declares that pointer p points to integer type, then
> what's the meaning of "void *p"? How can I represent it(void *p) in
> Ada language?

  You can't.  What you can do is make a pointer to something that
exists, (Interfaces.C.Char for instance) and then, where C would cast
the void to some actual thing in order to use it, do the same in Ada
by an Unchecked_Conversion to the desired type pointer.



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

* Re: pointer in C & in  Ada
  2001-08-07 22:39 pointer in C & in Ada Lin
  2001-08-07 23:05 ` Didier Utheza
  2001-08-07 23:31 ` tmoran
@ 2001-08-08  3:14 ` DuckE
  2001-08-08  9:32 ` Des Walker
  2001-08-08 14:59 ` Ted Dennison
  4 siblings, 0 replies; 17+ messages in thread
From: DuckE @ 2001-08-08  3:14 UTC (permalink / raw)


In C "void *p" means that 'p' contains a pointer to an address where the
type of data at that address is unspecified.  This corresponds to an Ada
variable of type System.Address.

In C you cannot access data using the pointer unless you cast it to a
particular type.  This corresponds to using
System.Address_To_Access_Conversions in Ada.

SteveD

BTW: While my answer may not be techically correct, I believe it will lead
you to something that works.

"Lin" <lin@post.com> wrote in message
news:86772402.0108071439.1c3e1e40@posting.google.com...
> If "int *p" declares that pointer p points to integer type, then
> what's the meaning of "void *p"? How can I represent it(void *p) in
> Ada language?
>
> Many thanks,
> Lin





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

* Re: pointer in C & in  Ada
  2001-08-07 23:05 ` Didier Utheza
@ 2001-08-08  9:29   ` Florian Weimer
  2001-08-08 14:52     ` Ted Dennison
  2001-08-09 16:54     ` Didier Utheza
  0 siblings, 2 replies; 17+ messages in thread
From: Florian Weimer @ 2001-08-08  9:29 UTC (permalink / raw)


Didier Utheza <ddutheza@bu.edu> writes:

> About Ada95, like you I am in the vague. My guess is that it is not
> possible in pure Ada,

You can use chars_ptr instead.  It is guaranteed to have the same
representation as void * in C (not by the Ada standard, but by the C
standard).



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

* Re: pointer in C & in  Ada
  2001-08-07 22:39 pointer in C & in Ada Lin
                   ` (2 preceding siblings ...)
  2001-08-08  3:14 ` DuckE
@ 2001-08-08  9:32 ` Des Walker
  2001-08-08 10:40   ` Florian Weimer
  2001-08-08 18:03   ` tmoran
  2001-08-08 14:59 ` Ted Dennison
  4 siblings, 2 replies; 17+ messages in thread
From: Des Walker @ 2001-08-08  9:32 UTC (permalink / raw)


Lin wrote:
> 
> If "int *p" declares that pointer p points to integer type, then
> what's the meaning of "void *p"? How can I represent it(void *p) in
> Ada language?
> 
> Many thanks,
> Lin

Hi,

Adding to what others have said you might also want to consider in what
context you want to use the 'Ada equivalent' of the void * type.

According to the online ANSI C Rationale:
"The use of void * (``pointer to void'')  as a generic object pointer
type is an invention of the Committee.  Adoption of this type was
stimulated by the desire to specify function prototype arguments that
either quietly convert arbitrary pointers (as in fread)  or complain if
the argument type does not exactly match (as in strcmp)."

This essentially permits the compiler to avoid issuing a warning about a
type mismatch in using functions where the programmer has decided the
type doesn't matter.

In Ada getting good type definitions is a significant part of the
development process. Bypassing the use of the type information, by using
a 'generic pointer type', is sometimes counter productive for both the
code user and the compiler. You could use Ada generics to develop
templates, which can be instantiated with appropriate type information.
Or you might be able to make use of tagged types to express some
underlying relationship between the data types.

Alternatively if you are considering calling C functions from Ada then,
as someone else has pointed out, it is worth looking at the Interfaces.C
package to see what it has to offer.

	Regards
	Des Walker



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

* Re: pointer in C & in  Ada
  2001-08-08  9:32 ` Des Walker
@ 2001-08-08 10:40   ` Florian Weimer
  2001-08-08 18:03   ` tmoran
  1 sibling, 0 replies; 17+ messages in thread
From: Florian Weimer @ 2001-08-08 10:40 UTC (permalink / raw)


Des Walker <des.walker@amsjv.com> writes:

> Alternatively if you are considering calling C functions from Ada then,
> as someone else has pointed out, it is worth looking at the Interfaces.C
> package to see what it has to offer.

void * was accidentally (at least I think so) left out in the
Interfaces.C and its children.



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

* Re: pointer in C & in  Ada
  2001-08-08  9:29   ` Florian Weimer
@ 2001-08-08 14:52     ` Ted Dennison
  2001-08-08 16:52       ` Florian Weimer
  2001-08-09 16:54     ` Didier Utheza
  1 sibling, 1 reply; 17+ messages in thread
From: Ted Dennison @ 2001-08-08 14:52 UTC (permalink / raw)


In article <87r8umlx13.fsf@deneb.enyo.de>, Florian Weimer says...
>
>Didier Utheza <ddutheza@bu.edu> writes:
>
>> About Ada95, like you I am in the vague. My guess is that it is not
>> possible in pure Ada,
>
>You can use chars_ptr instead.  It is guaranteed to have the same
>representation as void * in C (not by the Ada standard, but by the C
>standard).

..assuming the compiler used for the C code bothers to follow that standard.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: pointer in C & in  Ada
  2001-08-07 22:39 pointer in C & in Ada Lin
                   ` (3 preceding siblings ...)
  2001-08-08  9:32 ` Des Walker
@ 2001-08-08 14:59 ` Ted Dennison
  2001-08-08 16:53   ` Florian Weimer
                     ` (2 more replies)
  4 siblings, 3 replies; 17+ messages in thread
From: Ted Dennison @ 2001-08-08 14:59 UTC (permalink / raw)


In article <86772402.0108071439.1c3e1e40@posting.google.com>, Lin says...
>
>If "int *p" declares that pointer p points to integer type, then
>what's the meaning of "void *p"? How can I represent it(void *p) in
>Ada language?


I generally use System.Address (or a private type derived from it). There was a
long discussion about this a few years back. What I remember was that this is
not guaranteed to be portable (to work on every platform), but no-one could
think of an existing platform on which it doesn't work. If an access to
Interfaces.C.Char seems more portable, use that.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: pointer in C & in  Ada
  2001-08-08 14:52     ` Ted Dennison
@ 2001-08-08 16:52       ` Florian Weimer
  0 siblings, 0 replies; 17+ messages in thread
From: Florian Weimer @ 2001-08-08 16:52 UTC (permalink / raw)


Ted Dennison<dennison@telepath.com> writes:

>>You can use chars_ptr instead.  It is guaranteed to have the same
>>representation as void * in C (not by the Ada standard, but by the C
>>standard).
>
> ..assuming the compiler used for the C code bothers to follow that
> standard.

If the compiler is broken, all bets are off anyway.  For this
particular issue, I think it's highly unlikely you will find a
compiler which gets this wrong because historically, char * was used
in places where now, you are supposed to use void *.



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

* Re: pointer in C & in  Ada
  2001-08-08 14:59 ` Ted Dennison
@ 2001-08-08 16:53   ` Florian Weimer
  2001-08-08 18:03   ` tmoran
  2001-08-08 23:29   ` Des Walker
  2 siblings, 0 replies; 17+ messages in thread
From: Florian Weimer @ 2001-08-08 16:53 UTC (permalink / raw)


Ted Dennison<dennison@telepath.com> writes:

> If an access to Interfaces.C.Char seems more portable, use that.

Any type which is compatible at the C interface level with char * will
do (for example, Interfaces.C.Strings.chars_ptr).



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

* Re: pointer in C & in  Ada
  2001-08-08  9:32 ` Des Walker
  2001-08-08 10:40   ` Florian Weimer
@ 2001-08-08 18:03   ` tmoran
  1 sibling, 0 replies; 17+ messages in thread
From: tmoran @ 2001-08-08 18:03 UTC (permalink / raw)


>This essentially permits the compiler to avoid issuing a warning about a
>type mismatch in using functions where the programmer has decided the
>type doesn't matter.
  Sometimes it's useful to replace a single C function that uses void*
with two different Ada functions, with two different variable types,
both of which call the same C function (or even both of which are
simply "pragma import"s of the same C function).  In C you can say
"this parameter is this type, or it's any type", while in Ada you
could make specific different functions (overloading the same name)
to allow exactly the desired set of parameter types.



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

* Re: pointer in C & in  Ada
  2001-08-08 14:59 ` Ted Dennison
  2001-08-08 16:53   ` Florian Weimer
@ 2001-08-08 18:03   ` tmoran
  2001-08-08 23:29   ` Des Walker
  2 siblings, 0 replies; 17+ messages in thread
From: tmoran @ 2001-08-08 18:03 UTC (permalink / raw)


>I generally use System.Address (or a private type derived from it).
  Remembering of course that a System.Address is not guaranteed to
be the same as an access value.  Think of mixed memory models on the
segmented x86 architecture, where one might have a base and an offset,
while the other is strictly an offset with an assumed base.



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

* Re: pointer in C & in  Ada
  2001-08-08 14:59 ` Ted Dennison
  2001-08-08 16:53   ` Florian Weimer
  2001-08-08 18:03   ` tmoran
@ 2001-08-08 23:29   ` Des Walker
  2001-08-09 20:21     ` Florian Weimer
  2 siblings, 1 reply; 17+ messages in thread
From: Des Walker @ 2001-08-08 23:29 UTC (permalink / raw)



Ted Dennison <dennison@telepath.com> wrote in message
news:otcc7.2752$NJ6.10767@www.newsranger.com...
> In article <86772402.0108071439.1c3e1e40@posting.google.com>, Lin
says...
> >
> >If "int *p" declares that pointer p points to integer type, then
> >what's the meaning of "void *p"? How can I represent it(void *p) in
> >Ada language?
>
>
> I generally use System.Address (or a private type derived from it).
There was a
> long discussion about this a few years back. What I remember was that
this is
> not guaranteed to be portable (to work on every platform), but no-one
could
> think of an existing platform on which it doesn't work. If an access
to
> Interfaces.C.Char seems more portable, use that.
>
> ---
> T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
>           home email - mailto:dennison@telepath.com

Hi,

I didn't read the aforementioned thread, so apologies if this is old
information. Just for interest on interfacing to C functions, I note
that the C-FAQ mentions some platforms that use different addressing
mechanisms depending on the type of object being addressed, in a
discussion about the null pointer in C.

http://www.eskimo.com/~scs/C-faq/q5.17.html

There probably isn't an Ada port for most of these platforms, although
the 8086 family gets a mention, as some may use 16 bit data pointers and
32 bit function pointers.

    Regards
    Des Walker






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

* Re: pointer in C & in  Ada
  2001-08-08  9:29   ` Florian Weimer
  2001-08-08 14:52     ` Ted Dennison
@ 2001-08-09 16:54     ` Didier Utheza
  2001-08-13 15:47       ` lange92
  1 sibling, 1 reply; 17+ messages in thread
From: Didier Utheza @ 2001-08-09 16:54 UTC (permalink / raw)


Thanks for the information. My guess is that is usefull for interfacing
with C that uses it for anything.

On Wed, 8 Aug 2001, Florian Weimer wrote:

> Didier Utheza <ddutheza@bu.edu> writes:
> 
> > About Ada95, like you I am in the vague. My guess is that it is not
> > possible in pure Ada,
> 
> You can use chars_ptr instead.  It is guaranteed to have the same
> representation as void * in C (not by the Ada standard, but by the C
> standard).
> 
> 




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

* Re: pointer in C & in  Ada
  2001-08-08 23:29   ` Des Walker
@ 2001-08-09 20:21     ` Florian Weimer
  0 siblings, 0 replies; 17+ messages in thread
From: Florian Weimer @ 2001-08-09 20:21 UTC (permalink / raw)


"Des Walker" <des.walker@dessy.fsnet.co.uk> writes:

> I didn't read the aforementioned thread, so apologies if this is old
> information. Just for interest on interfacing to C functions, I note
> that the C-FAQ mentions some platforms that use different addressing
> mechanisms depending on the type of object being addressed, in a
> discussion about the null pointer in C.
>
> http://www.eskimo.com/~scs/C-faq/q5.17.html

| The old HP 3000 series uses a different addressing scheme for byte
| addresses than for word addresses; like several of the machines above
| it therefore uses different representations for char * and void *
| pointers than for other pointers.

So even on this architecture, HP got the char * <-> void *
correspondence right, it seems.  Most probably, there is really
nothing to worry about.



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

* Re: pointer in C & in  Ada
  2001-08-09 16:54     ` Didier Utheza
@ 2001-08-13 15:47       ` lange92
  0 siblings, 0 replies; 17+ messages in thread
From: lange92 @ 2001-08-13 15:47 UTC (permalink / raw)
  To: Didier Utheza



On Thu, 9 Aug 2001, Didier Utheza wrote:

> Thanks for the information. My guess is that is usefull for interfacing
> with C that uses it for anything.
>
> > > About Ada95, like you I am in the vague. My guess is that it is not
> > > possible in pure Ada,

I found this routine somewhere in a GPL'd work long ago:

type handler is access
   function (state : t_state) return Integer;

There was no obvious use for "state", though I would expect it to be used
for something like saving the stack, since this was actually used in a
routine for assigning interrupt handlers.

The "return Integer" I would assume to make this of the type "int *"
instead of "void *", but I would imagine it could be used for something
else with a little modification. Perhaps it could be written as an access
type to a procedure instead of a function?

Perhaps this could be a little light at the end of the tunnel, I don't
know. But I thought it was a stroke of pure genius when I saw it, myself.

DanL






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

end of thread, other threads:[~2001-08-13 15:47 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-07 22:39 pointer in C & in Ada Lin
2001-08-07 23:05 ` Didier Utheza
2001-08-08  9:29   ` Florian Weimer
2001-08-08 14:52     ` Ted Dennison
2001-08-08 16:52       ` Florian Weimer
2001-08-09 16:54     ` Didier Utheza
2001-08-13 15:47       ` lange92
2001-08-07 23:31 ` tmoran
2001-08-08  3:14 ` DuckE
2001-08-08  9:32 ` Des Walker
2001-08-08 10:40   ` Florian Weimer
2001-08-08 18:03   ` tmoran
2001-08-08 14:59 ` Ted Dennison
2001-08-08 16:53   ` Florian Weimer
2001-08-08 18:03   ` tmoran
2001-08-08 23:29   ` Des Walker
2001-08-09 20:21     ` Florian Weimer

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