表达式:_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)错误 [英] Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Error

查看:435
本文介绍了表达式:_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此错误发生在运行时,我不知道是什么导致它 - 代码看起来正确。

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;
}

错误显然发生在第52行(print )。除此之外,打印的文本显示非常大,通常为负数的数字。是什么原因导致了?

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?

推荐答案

发行



Issue

void Event::set(Room r, const std::string& name) 
{
    d_room = &r;
    //      ^
    d_name = name;
}

您正引用临时对象: Room r 传递的值,在范围结束时销毁:}

You are referencing to the temporary object: Room r passed by value, which is destroyed at the end of the scope: }.

您必须重新分配成员指针:

Instead you must reallocate the member pointer:

d_room = new Room(r);



为什么出错



在C ++类中编写C风格代码。

Why it went wrong

Because you are writing C-style code in C++ classes.

在C ++中,我们倾向于:

In C++ we tend to:


  1. 避免裸指针,喜欢智能指针:

  1. Avoid naked pointers, prefer smart pointers:

class Event 
{
    std::shared_ptr<Room> d_room;
 ...

Event::~Event() { /* no need to delete */ }


  • 使用构造函数重载(而不是在构建之后使用 set -类函数):

    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);
    


  • 避免使用原始数组,请改用STL设施:

  • Avoid raw arrays, use STL facilities instead:

    std::vector<Event> lectures;
    // or
    std::array<Event, 5> lectures;
    




  • 另一个问题



    r.d_hasProjector!= r.d_hasProjector; //检查r.d_hasProject是否不是自身

    Another issue

    r.d_hasProjector != r.d_hasProjector; // checks if r.d_hasProject is not itself

    您可能想要

    r.d_hasProjector =!r.d_hasProjector;

    完成代码: 链接

    此外,这里是一个必读的链接C ++的东西,我相信,对你非常有用: http://www.parashift.com / c ++ - faq /

    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/

    修改:我忘记了您的问题:


    除此之外,打印的文本显示的数字非常大,通常为负数。是什么原因?

    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.

    这篇关于表达式:_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆