possible to have pointers in bitset?

This is a discussion on possible to have pointers in bitset? within the C++ Programming forums, part of the General Programming Boards category; Code: bitset<8> *t1, *t2; t2 = new bitset<8> [1]; *t1 = t2; I realised that I can't have pointer to ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    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?

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Try
    Code:
    t1 = t2;

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    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

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > t2 = new bitset<8> [1];
    > *t1 = t2;
    This should be:
    Code:
    	t2 = new bitset<8>;
    	t1 = t2;

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    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.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    To get the individual bits:
    Code:
    	std::bitset<8> t1(255);
    	for (int i=0; i<8; i++)
    	{
    		std::cout << t1[i] << '\n';
    	}

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    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
    Last edited by franziss; 04-07-2007 at 03:09 AM.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >t2 = t1
    Are you sure you understand pointers? It would be:
    t2 = &t1;

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    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.

    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    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;

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    t2 is a dynamic allocated bitset.
    That is false. t2 is a dynamically allocated array of bitset<8>s.

    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    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

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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).

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    i'm using microsoft visual studio 2005, yes, t1 points to the same bitset as t2, but &t1 != &t2

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21