473,657 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get pointer and offset from vector<bool>::i terator (or reference)

Dear list,

I am using std::vector<boo l> (bit_vector) to store my bit sequence. To
access the same sequence from C (to expose to a python module), I need
to know the pointer and offset of vector::<bool>: :iterator (or
reference). However, given a std::vector<boo l> a, all a.begin(), a[0]
etc are instances of a proxy class so I can not do things like
&*a.begin(). Is there a safe way to get the information I need?

Many thanks in advance.
Bo
Jan 31 '06 #1
8 3540
* Bo Peng:

I am using std::vector<boo l> (bit_vector) to store my bit sequence. To
access the same sequence from C (to expose to a python module), I need
to know the pointer and offset of vector::<bool>: :iterator (or
reference). However, given a std::vector<boo l> a, all a.begin(), a[0]
etc are instances of a proxy class so I can not do things like
&*a.begin(). Is there a safe way to get the information I need?


Something like

void python( int const bitValues[], int nValues );

void callPython( std::vector<boo l> const& bits )
{
std::vector<int > const values( bits.begin(), bits.end() );
python( &values[0], safe_cast<int>( values.size() ) );
}

where "safe_cast" is your favorite safe cast from one integral type to
another, presumably aborting or throwing an exception if the value can't
be represented by the destination type.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 31 '06 #2
Bo Peng wrote:
I am using std::vector<boo l> (bit_vector) to store my bit sequence. To
access the same sequence from C (to expose to a python module), I need
to know the pointer and offset of vector::<bool>: :iterator (or
reference). However, given a std::vector<boo l> a, all a.begin(), a[0]
etc are instances of a proxy class so I can not do things like
&*a.begin(). Is there a safe way to get the information I need?


No. Bits have no separate address.

V
Jan 31 '06 #3
Victor Bazarov wrote:
Bo Peng wrote:
I am using std::vector<boo l> (bit_vector) to store my bit sequence. To
access the same sequence from C (to expose to a python module), I need
to know the pointer and offset of vector::<bool>: :iterator (or
reference). However, given a std::vector<boo l> a, all a.begin(), a[0]
etc are instances of a proxy class so I can not do things like
&*a.begin(). Is there a safe way to get the information I need?

No. Bits have no separate address.


Really? What I need is actually the starting point of the bits. I.e.,
&*vec.begin( ) if the vector is not vector<bool>. It should not be too
diffcult since bit_vector always starts from the beginning of a WORD.

Cheers,
Bo
Jan 31 '06 #4
Bo Peng wrote:
Victor Bazarov wrote:
Bo Peng wrote:
I am using std::vector<boo l> (bit_vector) to store my bit sequence.
To access the same sequence from C (to expose to a python module), I
need to know the pointer and offset of vector::<bool>: :iterator (or
reference). However, given a std::vector<boo l> a, all a.begin(), a[0]
etc are instances of a proxy class so I can not do things like
&*a.begin(). Is there a safe way to get the information I need?


No. Bits have no separate address.


Really? What I need is actually the starting point of the bits. I.e.,
&*vec.begin( ) if the vector is not vector<bool>. It should not be too
diffcult since bit_vector always starts from the beginning of a WORD.


There is no requirement that 'vector<bool>' keeps its bits in the same
array, same value, or same {whatever_other _collection_you _can_think_of}.

So, if you _know_ that those bits are a contiguous representation of some
kind of integral value (or whatever you think they are), you know already
where and/or how to get them.

All I can suggest is to repackage them as you need them, just like Alf
explained.

V
Jan 31 '06 #5
Victor Bazarov wrote:
All I can suggest is to repackage them as you need them, just like Alf
explained.


The problem is that the goal of this python module is to *expose* the
underlying vector<bool> (other vectors are easy and have been done) and
allow users to read/write the vector directly. For example

v = getVector(); # return a python object v
print v
print v[2]
v[0] = 1
v[3] = 0

Bo
Feb 1 '06 #6
>
For that you need to provide a wrapper to the python side.


Exactly. I have already working code for other vectors. The python
wrapper object knows vector type and starting pointer and can correctly
read/write vector items. In the case of vector<bool>, I am unable to get
the starting address.

The only way out may be extending python in C++, instead of C. I mean,
store a vector reference directly instead of pointer. I really do not
know how to do this since all examples of extending Python is written in C.

Cheers,
Bo
Feb 1 '06 #7
* Bo Peng:
Victor Bazarov wrote:
All I can suggest is to repackage them as you need them, just like Alf
explained.


The problem is that the goal of this python module is to *expose* the
underlying vector<bool> (other vectors are easy and have been done) and
allow users to read/write the vector directly. For example

v = getVector(); # return a python object v
print v
print v[2]
v[0] = 1
v[3] = 0


For that you need to provide a wrapper to the python side.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 1 '06 #8
On Tue, 31 Jan 2006 18:54:00 -0600, Bo Peng <bp***@rice.edu > wrote:

For that you need to provide a wrapper to the python side.

Exactly. I have already working code for other vectors. The python
wrapper object knows vector type and starting pointer and can correctly
read/write vector items. In the case of vector<bool>, I am unable to get
the starting address.


vector<bool> is an exception to all other vectors. You cannot access
its storage directly like a buffer, neither in C++ nor in any other
language. As the C++ standard says, the reference returned by
vector<bool>::o perator[] is some kind of proxy class that lets you
read and write individual bool elements, but that's all.

If you can't do it in C++, "extending Python" won't help, either.
The only way out may be extending python in C++, instead of C. I mean,
store a vector reference directly instead of pointer. I really do not
know how to do this since all examples of extending Python is written in C.

Cheers,
Bo


--
Bob Hairgrove
No**********@Ho me.com
Feb 1 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2444
by: Jim Campbell | last post by:
Suppose I wanted to a class to contain a pointer, but I didn't want to make the copy constructor or assignment operator reinitialize this pointer. (Think of a class that contains an mmapped pointer - having to duplicate the memory could be very expensive.) Instead, I wanted only the non-copy construction to create the pointer, and have copies of the object, achieved through either copy construction or assignment, refer to the pointer...
1
1502
by: Ruediger Knoerig | last post by:
In my current project I used templates to provide an universial interface to vector objects. For this purpose I declared two methods with an additional template argument: template<class Vector> void transform(const Vector &x) template<class Vector> void inv_transform(Vector &x) Doing it this way I can use it on all data containers providing index array access via :
4
1977
by: Daniel Aarno | last post by:
Consider the following code: typedef int A; class B { public: B(A& var) : m_var(var) {} virtual ~B() {} protected:
3
1523
by: C# Expert | last post by:
I am using managed c++ code. I have created i function with one argument that is reference to double. fun(double& d) When I see it in object browser in visual studio 2005, it shows pointer instead of reference. fun(double* d) I reference that .dll from my project which is written in c#,.
11
4215
by: asdf | last post by:
C++ allows a reference to a pointer, but doesn't allow a pointer to a reference, why?
7
6333
by: Mohan | last post by:
Hi, What are the advantages/disadvantages of using a pointer instead of Reference in the Copy Constructor ? For Example, Writing the Copy constructor for the Class "Temp" as below, Temp(const base *ptrBase)
9
2326
by: sulekhasweety | last post by:
dear all, can anyone explain the differences between pointer variable and reference variables ?
6
3844
by: swathi amireddy | last post by:
Currently im trying to migrate a project from .net 1.1 to 2.0.While running the application its giving an exception for the below code while doing the update operation: protected void btnUpdate_Click(object sender, System.EventArgs e) { int ParamID=Convert.ToInt16(Session.ToString());(showing exception at this line) .... .... } private void grdParameters_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) {
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.