[C++-sig] RE: Pointers to arrays...

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Tue Dec 23 01:31:41 CET 2003


--- Phil Hornby <phil.hornby at accutest.co.uk> wrote:
> I think you have miss-understood what I am doing.

I was almost certain that would be the case. :)

> What I am passing in is
> NOT a string, it is in fact 8 data bytes from a CAN message. CAN is a bus
> system used largely in the automotive industry. I had anti-cipated being
> able to pass a python list or tuple in to the constructor in-place of the
> (BYTE *)

How do you want you 8 bytes to "appear" in Python? A sequence of integers? A
string of 8 characters? A special type that you bind via class_<>? An opaque
pointer? -- You have to make this decision first. It is probably not an easy
decision to make.

One thing to consider is that exchanging data by value (i.e. by copying) rather
than by reference often leads to a significant simplification and is
automatically safe. Eight bytes is not very much to copy, so I'd be inclined to
copy the pointee of BYTE* to a new Python string when you convert to Python and
to copy the string back to the memory where BYTE* lives when you convert from
Python. Which approach exactly will work best for you I don't know. If you only
have a few functions that exchange BYTE* the easiest way would be to work with
thin wrappers as outlined before. If that is not feasible you could try your
hand with custom converters, but this is significantly more involved.

> know there are these new fangled thinks I can use called references.

These are still essentially pointers, you just don't have to write the * (and
references are important because the caller doesn't have to know if the
parameter is used by value or by reference). The juicy bit (one of the juicy
bits) of C++ is something else: support for encapsulating memory management. If
you make use of this in C++ it is often very straightforward to define the
Python bindings. Your case appears to be not so straightforward mainly because
you have to solve two problems at once: (1) make the interface safe, (2) define
the Python bindings. But if you can afford to copy as outlined above it should
not be too hard.

Ralf


__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/




More information about the Cplusplus-sig mailing list