x******a 发帖数: 6336 | 1 I defined a random number generator class norm, I would like to define a std
::vector of norm. However it did not work.
I got "Debug Assertation Failed" after the first of for loop.
Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse).
Any input are appreciated. Thanks!
code:
unsigned long seeds[]={123, 345, 356};
std::vector myseeds(std::begin(seeds), std::end(seeds));
std::vector mynorm;
for(auto it=myseeds.begin(); it!=myseeds.end(); ++it)
{
mynorm.push_back(norm(*it));
}
class norm
{
private:
typedef boost::mt19937 Eng;
typedef boost::normal_distribution<> Norm;
typedef boost::variate_generator GEN;
GEN *gen;
public:
//constructor
norm(unsigned long Seed) { gen= new GEN( Eng(Seed), Norm(0.0,1.0));}
//destructor;
~norm(){ delete gen;}
//method
double next() { return (*gen)();}
} |
w******w 发帖数: 126 | 2 ...
unsigned long seeds[]={123, 345, 356};
std::vector myseeds(std::begin(seeds), std::end(seeds));
std::vector mynorm;
for(auto it=myseeds.begin(); it!=myseeds.end(); ++it)
{
mynorm.push_back(norm(*it));
}
你把一个 unsigned long 的vector 往 norm的vector 里面去塞,C++ 会不高兴的 我
想 ^_^ 难道让编译器去隐士转换? :-) |
l*********s 发帖数: 5409 | 3 This line is the problem:
mynorm.push_back(norm(*it));
create a tempory norm object then push it into the vector with default
assignment operator(ie. bit wise copying). Then the temp object is destroyed
along with the GEN member object, leaving a dangling raw pointer in the
vector.
you shall use smarter pointer instead |
x******a 发帖数: 6336 | 4 thanks. After replacing the raw pointer with shared_ptr, it works now.
destroyed
【在 l*********s 的大作中提到】 : This line is the problem: : mynorm.push_back(norm(*it)); : create a tempory norm object then push it into the vector with default : assignment operator(ie. bit wise copying). Then the temp object is destroyed : along with the GEN member object, leaving a dangling raw pointer in the : vector. : you shall use smarter pointer instead
|
m*********a 发帖数: 3299 | 5 std::vector mynorm;
for(auto it=myseeds.begin(); it!=myseeds.end(); ++it)
{
mynorm.push_back(new norm(*it));
}
std::vector mynorm;
for(auto it=myseeds.begin(); it!=myseeds.end(); ++it)
{
norm &p=*(new norm(*it))
mynorm.push_back(p);
}
【在 x******a 的大作中提到】 : thanks. After replacing the raw pointer with shared_ptr, it works now. : : destroyed
|
a*********a 发帖数: 3656 | 6 u have got to be kidding me.
【在 m*********a 的大作中提到】 : std::vector mynorm; : for(auto it=myseeds.begin(); it!=myseeds.end(); ++it) : { : mynorm.push_back(new norm(*it)); : } : std::vector mynorm; : for(auto it=myseeds.begin(); it!=myseeds.end(); ++it) : { : norm &p=*(new norm(*it)) : mynorm.push_back(p);
|
h**6 发帖数: 4160 | 7 mynorm.emplace_back(*it); |
a*********a 发帖数: 3656 | 8 you have got to take care of that GEN* gen. from stack overflow:
"The _BLOCK_TYPE_IS_VALID assertion gets fired, when you overwrite the
header of an block allocated by new. This happens when you slice objects,
use dead objects, etc."
"This type of error could be caused by Heap corruption."
my understanding is that you are overwriting the "gen" which is a pHead
pointing to a block on heap. that block is going to be lost. but I may be
wrong, since I have never seen such error messages before.
write your own copy constructor would solve this problem or as other
suggested, use a smart pointer.
it is amazing how far the compilers have come along. in the old days, code
like this would actually compile without a fart.
std
【在 x******a 的大作中提到】 : I defined a random number generator class norm, I would like to define a std : ::vector of norm. However it did not work. : I got "Debug Assertation Failed" after the first of for loop. : Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse). : Any input are appreciated. Thanks! : code: : unsigned long seeds[]={123, 345, 356}; : std::vector myseeds(std::begin(seeds), std::end(seeds)); : std::vector mynorm; : for(auto it=myseeds.begin(); it!=myseeds.end(); ++it)
|
h*****0 发帖数: 4889 | 9 unique_ptr preferred...
【在 x******a 的大作中提到】 : thanks. After replacing the raw pointer with shared_ptr, it works now. : : destroyed
|