首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Milestone2.exe中0x7700C42D的未处理异常: Microsoft C++异常:内存位置为0x003EF5DC的std::bad_alloc

Milestone2.exe中0x7700C42D的未处理异常: Microsoft C++异常:内存位置为0x003EF5DC的std::bad_alloc
EN

Stack Overflow用户
提问于 2018-03-23 21:10:58
回答 1查看 140关注 0票数 0

ErrorState.h

代码语言:javascript
复制
#ifndef ERRORSTATE_ERRORSTATE_H
#define ERRORSTATE_ERRORSTATE_H


#include <iostream>

namespace AMA
{
    class ErrorState
    {
         char* m_message;

    public:
        explicit ErrorState(const char* errorMessage = nullptr);
        //ErrorState(const ErrorState& em) = delete;
        //ErrorState& operator=(const ErrorState& em) = delete;
        void clear();
        bool isClear() const;

        void message(const char* str);

        const char* message()const;
        virtual ~ErrorState();

    };
    std::ostream& operator<<(std::ostream&, ErrorState&);

}

#endif // !ERRORSTATE_ERRORSTATE_H

ErrorState.cpp

代码语言:javascript
复制
#include <iostream>
#include "ErrorState.h"

namespace AMA
{

    ErrorState::ErrorState(const char* errorMessage)
    {
        if (errorMessage == nullptr)
        {
            m_message = nullptr;
        }
        else
        {
            message(errorMessage);
        }
    }
    /*ErrorState::ErrorState(const ErrorState& em) 
    {


    }

    ErrorState& ErrorState::operator=(const ErrorState& em) 
    {
        strncpy(m_message, em.m_message, sizeof(m_message));
        return *this;
    }
    */
    void ErrorState::clear()
    {
        delete [] this->m_message;
        this->m_message = nullptr;
    }

    bool ErrorState::isClear() const
    {
        if (this->m_message == nullptr)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    void ErrorState::message(const char* str)
    {
        //if (isClear() == false)
        //{
        //  clear();
        //}


        this->m_message = new char[strlen(str) + 1];
        strcpy(this->m_message, str);
    }

    const char* ErrorState::message()const
    {
        return this->m_message;
    }
    ErrorState::~ErrorState()
    {
        delete[] this->m_message;
    }

    std::ostream& operator<<(std::ostream& output,  ErrorState& state)
    {
        if (!state.isClear())
        {
            output << state.message();
        }
        return output;

    }
}

Main.cpp

代码语言:javascript
复制
#include <iostream>
#include "ErrorState.h"

using namespace std;
using namespace AMA;

int main() {
    ErrorState T("Testing Error State Module");
    ErrorState e;
    int ret = 0;
    bool ok = true;
    cout << T << endl << e << endl << "isClear(): " << (e.isClear() ? "Passed!" : "Failed!") << endl;
    if (!e.isClear()) ok = false;
    cout << endl;
    cout << "===========| Long Message\r";
    for (int i = 0; i < 10000000; i++) {
        if (i % 1000000 == 0) {
            cout << "*";
            cout.flush();
        }
        e.message("Some error message that is really long long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long");
    }
    cout << '*' << endl;
    cout << e << endl << "isClear(): " << (e.isClear() ? "Failed!" : "Passed!") << endl;
    if (e.isClear()) ok = false;
    cout << endl;

    e.message("Short Message");
    cout << e << endl << e.message() << endl << "isClear(): " << (e.isClear() ? "Failed!" : "Passed!") << endl;
    if (e.isClear()) ok = false;

    e.clear();
    cout << e << endl << "isClear(): " << (e.isClear() ? "Passed!" : "Failed!") << endl;
    if (!e.isClear()) ok = false;
    cout << endl;

    if (ok) {
        cout << "You passed all the tests!" << endl;
    }
    else {
        cout << "You did not pass all the tests, keep working on your project!" << endl;
        ret = 1;
    }

    return ret;

}

我正在尝试创建一个错误检测程序。但是在我的其中一个函数中,它给了我错误,而且我不知道如何修复它,因为我是新的,而且还在学习。这就是我需要如何实现这个我无法正确完成的功能。

void message(const char* str);

此函数存储由str指向的C样式字符串的副本:

  • 去分配分配给先前存储的消息的任何内存。
  • 分配存储str副本所需的动态内存(请记住包含空终止符的一个额外字节)
  • 将地址str处的字符串复制到分配的内存中。
EN

回答 1

Stack Overflow用户

发布于 2018-03-23 21:39:53

显然,我们的处境是一样的。只是想帮你。我在Reddit上发现了这个。所以可能对你有帮助。

“在构造函数中,如果传入一个字符串,它就会调用'message',第一件事就是调用m_message成员的delete。它还没有初始化为nullptr。所以您要对一个随机值调用delete,在本例中是0xb754bff4。”

代码语言:javascript
复制
ErrorMessage::ErrorMessage(const char* errorMessage)
{
m_message = nullptr;

if(errorMessage) 
    message(errorMessage);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49458340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档