C++ 表达式:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19689639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Error
提问by Will
This error occurs during run time, and I'm not sure what's causing it - the code looks correct to me.
此错误发生在运行时,我不确定是什么原因造成的 - 代码在我看来是正确的。
#include <iostream>
#include <string>
using namespace std;
struct Room {
int d_noSeat;
bool d_hasProjector;
Room() = default;
Room(const Room& r);
};
class Event {
Room* d_room;
std::string d_name;
public:
Event();
Event(const Event& e);
~Event();
void set(Room r, const std::string& name);
void print();
};
Event::Event() : d_room(0), d_name("") {};
void Event::print() {
std::cout << "Event: " << d_name;
if (d_room != 0) {
std::cout << " in size " << d_room->d_noSeat;
if (d_room->d_hasProjector)
std::cout << " with";
else
std::cout << " without";
std::cout << " projector";
}
std::cout << std::endl;
return;
}
void printEvent(Event e) {
e.print();
return;
}
void Event::set(Room r, const std::string& name) {
d_room = &r;
d_name = name;
}
// Room shallow copy constructor
Room::Room(const Room& r) :
d_noSeat(r.d_noSeat),
d_hasProjector(r.d_hasProjector)
{ }
// Event deep copy constructor
Event::Event(const Event& e) :
d_name(e.d_name),
d_room(new Room(*e.d_room))
{ }
// Event destructor
Event::~Event()
{
delete[] d_room;
}
int main() {
const int noLect = 5;
Room r;
Event lectures[noLect];
for (int i = 0; i < noLect; ++i) {
r.d_noSeat = i + 1;
r.d_hasProjector != r.d_hasProjector;
lectures[i].set(r, "CSI2372");
lectures[i].print();
}
std::cout << "-------------------" << std::endl;
for (int i = 0; i < noLect; ++i) {
printEvent(lectures[i]);
}
return 0;
}
The error apparently occurs at line 52 (first line in the print() function). In addition to this, the printed text displays numbers that are very large and often negative. What is causing this?
错误显然发生在第 52 行(print() 函数中的第一行)。除此之外,打印的文本显示的数字非常大且通常为负数。这是什么原因造成的?
回答by Ivan Aksamentov - Drop
Issue
问题
void Event::set(Room r, const std::string& name)
{
d_room = &r;
// ^
d_name = name;
}
You are referencing to the temporary object: Room r
passed by value, which is destroyed at the end of the scope: }
.
您正在引用临时对象:Room r
按值传递,该对象在范围结束时被销毁:}
。
Instead you must reallocate the member pointer:
相反,您必须重新分配成员指针:
d_room = new Room(r);
Why it went wrong
为什么出错
Because you are writing C-style code in C++ classes.
因为您正在 C++ 类中编写 C 风格的代码。
In C++ we tend to:
在 C++ 中,我们倾向于:
Avoid naked pointers, prefer smart pointers:
class Event { std::shared_ptr<Room> d_room; ... Event::~Event() { /* no need to delete */ }
Use constructor overloading (instead of using
set
-like functions after construction):Event(Room& r, const std::string& name): d_room(new Room(r)), d_name(name) {}
Pass by reference:
void set(Room& r, const std::string& name);
Avoid raw arrays, use STL facilities instead:
std::vector<Event> lectures; // or std::array<Event, 5> lectures;
避免裸指针,更喜欢智能指针:
class Event { std::shared_ptr<Room> d_room; ... Event::~Event() { /* no need to delete */ }
使用构造函数重载(而不是
set
在构造后使用-like 函数):Event(Room& r, const std::string& name): d_room(new Room(r)), d_name(name) {}
通过引用传递:
void set(Room& r, const std::string& name);
避免使用原始数组,而是使用 STL 工具:
std::vector<Event> lectures; // or std::array<Event, 5> lectures;
Another issue
另一个问题
r.d_hasProjector != r.d_hasProjector; // checks if r.d_hasProject is not itself
r.d_hasProjector != r.d_hasProjector; // checks if r.d_hasProject is not itself
You probably want
你可能想要
r.d_hasProjector = !r.d_hasProjector;
r.d_hasProjector = !r.d_hasProjector;
Complete code:link
完整代码:链接
Also, here is a must-read link about advanced C++ stuff which, I believe, will be very useful to you: http://www.parashift.com/c++-faq/
此外,这里有一个关于高级 C++ 内容的必读链接,我相信它对您非常有用:http: //www.parashift.com/c++-faq/
Edit:I forgot about your question:
编辑:我忘了你的问题:
In addition to this, the printed text displays numbers that are very large and often negative. What is causing this?
除此之外,打印的文本显示的数字非常大且通常为负数。这是什么原因造成的?
Those numbers are garbage. Variables that are not explicitly initialized are not initialized at all. Memory is allocated but holds old information from previous program. It could contain anything. When you read from uninitialized variables, you'll get this garbage. You had a pointer which was pointing to a destroyed object. So the pointer was effectively uninitialized.
那些数字是垃圾。未显式初始化的变量根本不会被初始化。内存已分配,但保留来自先前程序的旧信息。它可以包含任何东西。当你读取未初始化的变量时,你会得到这些垃圾。您有一个指向已销毁对象的指针。所以指针实际上是未初始化的。
回答by molbdnilo
Your problem is here:
你的问题在这里:
void Event::set(Room r, const std::string& name) {
d_room = &r;
d_name = name;
}
The &r
takes the address of an object whose lifetime ends when the function returns, resulting in undefined behaviour when you later try to access it.
在&r
接受一个对象,其生命周期结束时函数返回时,导致不确定的行为,当您稍后尝试访问它的地址。
If you want to use pointers, you need to allocate them dynamically:
如果要使用指针,则需要动态分配它们:
void Event::set(Room* r, const std::string& name) {
d_room = r;
d_name = name;
}
// ...
for (int i = 0; i < noLect; ++i) {
Room* r = new Room;
r->d_noSeat = i + 1;
r->d_hasProjector != r.d_hasProjector;
lectures[i].set(r, "CSI2372");
lectures[i].print();
}
// ...
But it doesn't look like you need pointers here, you should be able to have
但看起来你在这里不需要指针,你应该能够有
Room d_room;
in the Event
class.
在Event
课堂上。