Thread: possible to have pointers in bitset?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    No, that is not a weakness of bitset. &t1 is the address of t1. &t2 is the address of t2. t1 and t2 contain addresses, and the address contained by t1 is the same as the address contained by t2 because they point to the same array. The address of t1 and the address of t2, on the other hand, are different, because they are not the same pointer. This applies even if t1 and t2 are int*s instead of bitset<8>*s.

    now go run your code in data type int, you will find that &t1 == &t2
    I agree, if 0012FF70 == 0012FF6C, upon which I have a proof that all integers are equal

    Oh, for the record I ran my tests in MSVC8, from Microsoft Visual C++ 2005 Express.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >but &t1 != &t2
    That's true whether you're talking ints or bitsets. &t1 means the address of the pointer, not where the pointer points. Run the second code you posted above that allocates an int array.

  3. #18
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    ok, i understand what you guys are explaining, thanks! it's my mistake, thanks for your help! =)
    Last edited by franziss; 04-07-2007 at 03:13 AM.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    you can only point to t2 but what if you want to store the memory of t2?
    If you wanted to store the address of t2 in t1, you would make t1 a bitset<8>** (or int**, as the case may be). For example:
    Code:
    #include <bitset>
    #include <iostream>
    
    int main()
    {
        const std::size_t array_size = 10;
        std::bitset<8>* t2;
        std::bitset<8>** t1;
    
        t2 = new std::bitset<8>[array_size];
        t1 = &t2; // store the address of t2
        std::cout << *t1 << std::endl;
        std::cout << t2 << std::endl;
        delete[] t2;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    int *t1; // a pointer

    t1 is empty coz t1 is suppose to be pointer and it has not point to anything. so we can change t1 ( which stores the addresss )
    Well, to be precise t1 points to garbage, you should set it to 0 (or NULL, which is synonymous with 0 in C++) if you want it to be a null pointer (and thus not point to "anything"). Eventually, t1 does store an address, but it stores the address of an int (the first int in an array of ints, and thus it points to that array).

    but when we have

    bitset<8> *t1;

    t1 is not empty, it has a address, it only serves to point, but you cannot change its address
    t1 here also points to garbage, until the point:
    t1 = t2;
    where it then points to the same array of bitset<8>s as t2 points to. Basically, the address in t2 is copied to t1, but the address of t1 and t2 are different.

    You are right to say that you cannot change its address, but you can change what it points to (and thus change the address it contains).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    ok, thanks for your advice! =)

    then what is the difference if i'm using bitset or char? or either way works?

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by swoopy View Post
    It depends on what you are comfortable using. Either way will work.
    The good part of a bitset is that you know you are dealing with n number of bits (even if internally there are bits unused), and you can access bits using a higher level interface.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    2 diffrent variables can never have the same adress? :/ atleast i tought so.

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >2 diffrent variables can never have the same adress?
    True except for unions, where members of the union share the same address.

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, 05: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