# possible to have pointers in bitset?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-07-2007
franziss
possible to have pointers in bitset?
Code:

```        bitset<8> *t1, *t2;         t2 = new bitset<8> [1];         *t1 = t2;```
I realised that I can't have pointer to bitset, unlike int *.
When i assign *t1, *t2, each of them have their own memory allocation, so when *t1=t2, the address of t1 is unchanged, not t2.

Anybody has idea on how to have pointers for bitset?
• 04-07-2007
cunnus88
Try
Code:

`t1 = t2;`
• 04-07-2007
franziss
i type wrongly, should be t1 = t2;

it doesn't work, http://www.sgi.com/tech/stl/bitset.html states bitset doesn't have iterators! i guess i have use int instead of bitset, where i assume an int contains 32 bits
• 04-07-2007
swoopy
> t2 = new bitset<8> [1];
> *t1 = t2;
This should be:
Code:

```        t2 = new bitset<8>;         t1 = t2;```
• 04-07-2007
franziss
it doesn't make any difference, the gist is, bitset doesn't have pointers, so it is better to use char and using bit manipulations, instead of bitset.
• 04-07-2007
swoopy
To get the individual bits:
Code:

```        std::bitset<8> t1(255);         for (int i=0; i<8; i++)         {                 std::cout << t1[i] << '\n';         }```
• 04-07-2007
swoopy
>so it is better to use char and using bit manipulations, instead of bitset.
It depends on what you are comfortable using. Either way will work.
• 04-07-2007
franziss
the weakness of bitset is, you cannot use pointers.

Code:

```bitset<8>  t1; bitset<8>  *t2; t2 = t1```
you can have a pointer t2 pointing to the contents of t1.

But do note that the &t2 is not equal to &t1, which means t2 has its own unique memory, so this is the weakness of bitset, compared to char.

It does not support pointer

more details is here http://www.sgi.com/tech/stl/bitset.htm
• 04-07-2007
swoopy
>t2 = t1
Are you sure you understand pointers? It would be:
t2 = &t1;
• 04-07-2007
laserlight
Quote:

the weakness of bitset is, you cannot use pointers.
No, SGI's STL reference (with a .html page, not .htm) says that it does not support iterators. It is making a comparison between bitsets and standard containers such as vectors, which have iterators. So, you can have a pointer to a bitset, but you cannot have an iterator to a bit in a bitset.

Quote:

But do note that the &t2 is not equal to &t1
That's obvious. t1 is a bitset<8>, t2 is a bitset<8>*. Change bitset<8> to int and it would still be true. On the other hand, &t1 == t2, assuming you actually assigned to t2 properly.
• 04-07-2007
franziss
sorry, typo error. Try this

Code:

```        bitset<8> *t1, *t2;         t2 = new bitset<8> [array_size];         t1 = t2;```
t2 is a dynamic allocated bitset.

bitset cannot have pointers, unlike char. if you guys try the code above, you will have notice t1 and t2 have their own memory allocation. unlike

Code:

```        int *t1, *t2;         t2 = new int [array_size];         t1 = t2;```
• 04-07-2007
laserlight
Quote:

t2 is a dynamic allocated bitset.
That is false. t2 is a dynamically allocated array of bitset<8>s.

Quote:

if you guys try the code above, you will have notice t1 and t2 have their own memory allocation
I tried it with the sample code below, and I find no such thing. t1 is a copy of t2, so t1 points to the same array of bitset<8>s as t2.

Code:

```#include <bitset> #include <iostream> int main() {     const std::size_t array_size = 10;     std::bitset<8> *t1, *t2;     t2 = new std::bitset<8>[array_size];     t1 = t2;     std::cout << t1 << std::endl;     std::cout << t2 << std::endl;     delete[] t2; }```
• 04-07-2007
franziss
you are right in saying t1 is pointing to t2, but go and check &t1 and &t2,
they are of different memory allocation, this is the weakness of bitset. you can only point to t2 but what if you want to store the memory of t2?

now go run your code in data type int, you will find that &t1 == &t2
• 04-07-2007
swoopy
What compiler are you using? I tried the code you posted above, and it works fine for me. After t1 = t2, t1 points to the same bitset as t2. I'm using a Borland compiler at the moment, but I'm pretty sure this above code will also work with g++ (Dev-C++ for example).
• 04-07-2007
franziss
i'm using microsoft visual studio 2005, yes, t1 points to the same bitset as t2, but &t1 != &t2
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last