1
votes

I want to use 2 large bitsets much larger than this example, so I have to use malloc to create them. I manipulate the 1st array "foo" to create a "template" array and then copy it to the 2nd "bar", and then I want to continue to manipulate and use the 2nd array in my program, and when I'm done I want to be able to copy the template "foo" to "bar" again quickly, so I can start over from that point.

#include <bitset>
#include <iostream>
using namespace std;

int main ()
{
long i;
const long size=1000;

    bitset<size>* foo;
    foo = (bitset<size>*) malloc (sizeof(bitset<size>));
    foo->set();

    for(i=3;i<1000;i=i+3) { foo->reset(i); }

    bitset<size>* bar;
    bar = (bitset<size>*) malloc (sizeof(bitset<size>));
    bar->set();

    std::cout << foo->count() << " " << bar->count() << '\n';

    memcpy( &bar, &foo, sizeof(foo) );
    std::cout << foo->count() << " " << bar->count() << '\n';

    bar->reset(1); bar->reset(2);
    std::cout << foo->count() << " " << bar->count() << '\n';

    return 0;
}

Output:

667 1000
667 667
665 665

The memcpy line works but when I then make changes in the "bar" bitset it also changes "foo". Instead of the memcpy I can also just do: bar=foo; but again foo is changed when bar changes. There are 2 separate arrays in memory as can be seen from the first cout line where the counts are different.

I assume what happens is both pointers points to the same array in memory after the copy, how can I avoid this so I can save the "template" array for later use?

1
Why are you using a raw pointer and (even worse) malloc() here at all? std::bitset isn't a POD type and at least should be allocated using new. It seems you have some serious misconceptions about how all of that works. - πάντα ῥεῖ
No C++ book that I know of shows to the beginner that malloc is to be used (even bad C++ books). Where are you getting your reading material in how to write C++ programs? Are you reading C books instead of C++ books? - PaulMcKenzie
so I have to use malloc to create them. -- Consider using the boost libraries and dynamic_bitset. Also, your question is bordering on the XY threshold. What is the higher-level problem that you're trying to solve? Maybe all of this bitset stuff is not the way to go about solving the real issue. - PaulMcKenzie
sizeof(foo) -- That doesn't work. The sizeof knows nothing about "dynamic array" sizes. The sizeof is a compile-time value denoting the size of the type that's specified. The sizeof(foo) is more than likely going to be either 4 or 8, depending on the pointer size. Basically the code you wrote is fundamentally flawed in multiple areas. - PaulMcKenzie
Another way to say memcpy( &bar, &foo, sizeof(foo) ); would be bar = foo; You're copying the pointer, not the underlying value. - Sid S

1 Answers

0
votes

Use the class interface only. I would advise you to avoid new/delete/malloc/free. As for whatever is going on when you memcpy over the internal data of a complicated object you do not control, that is almost certainly causing "undefined behavior". You are overwriting whatever happens to be internal to that class, but fortunately you didn't crash this time. It looks like on your specific compiler, you just overwrite some memory addresses, so one ends up pointing at the other's data area. I would expect it to crash later when it tries to clean up (although technically anything can happen).

Here is a minor correction of your code:

#include <bitset>
#include <iostream>
using namespace std;

int main()
{
    long i;
    const long size = 1000;

    bitset<size> foo;
    foo.set();

    for (i = 3; i < 1000; i = i + 3) { foo.reset(i); }

    bitset<size> bar;
    bar.set();

    std::cout << foo.count() << " " << bar.count() << '\n';

    bar = foo;
    std::cout << foo.count() << " " << bar.count() << '\n';

    bar.reset(1); bar.reset(2);
    std::cout << foo.count() << " " << bar.count() << '\n';

    return 0;
}

667 1000
667 667
667 665