由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教(C++)
相关主题
Help - C++ Debug Assertion Failed请教一个C语言的面试题
C++中size_type怎么处理?C++ question about template typedef
C++ Template Question多维数组用vector是不是更方便?
Debug Assertion Failed说起来上次谁推荐的memmove作面试题来着
问个小问题C++的exception大家常用吗?
一个简单的C编程问题问一个 Andrei A. 的 Modern c++ design 书里边的一个问题
C 中的typedef 一问Remove elements from multiple vectors in C++
问一个gcc下bit field的对齐问题Why the number is not exact in C++
相关话题的讨论汇总
话题: norm话题: gen话题: std话题: vector话题: unsigned
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
Why the number is not exact in C++问个小问题
difference between FILE and struct FILE一个简单的C编程问题
typedefC 中的typedef 一问
请教一个2维动态矩阵的问题问一个gcc下bit field的对齐问题
Help - C++ Debug Assertion Failed请教一个C语言的面试题
C++中size_type怎么处理?C++ question about template typedef
C++ Template Question多维数组用vector是不是更方便?
Debug Assertion Failed说起来上次谁推荐的memmove作面试题来着
相关话题的讨论汇总
话题: norm话题: gen话题: std话题: vector话题: unsigned