Dynamically allocation of bitsets

Hi, I don't have any idea why my program crashes.
Is this the wrong way to dynamically allocate a bitset?
1
2
3
4
5
6
7
8
9
 #include<iostream>
 #include<bitset>
 using namespace std;

 int main(){
   bitset<400> *coord = new bitset<400>;
   coord[8]=1;
 }


Thanks in advance
unnecessary dynamic memory allocation http://www.cplusplus.com/forum/general/138037/#msg731921

you've got T *foo = new T; with T=bitset<400>
suppose that instead T=int
1
2
int *foo = new int;
foo[8] = 1;

¿do you realize the mistake yet?
I think I did. And what about this way of dynamically allocating a bitset:
bitset<400UL>& coord = *(new bitset<400UL>());

Is it ok? my program doesn't crash now.
That's okay, but to ne555's first point, why allocate it with new at all? Why not do
bitset<400> coord;
Allocation is faster and you don't have to worry about deleting it yourself.
> bitset<400UL>& coord = *(new bitset<400UL>());
that's a leak.
Topic archived. No new replies allowed.