Einzelnen Beitrag anzeigen

Kas Ob.

Registriert seit: 3. Sep 2023
353 Beiträge
 
#9

AW: GNU C++ - wie Speicherfehler abfangen ?

  Alt 15. Aug 2024, 09:27
QChar is 2 bytes structure supplied with methods and default constructor, yes there is a constructor but it is more filling with default value which is 0.

Now, look how you declared it, in your code there is no "*", it is local field and in-place, trying to delete it itself will not be possible.

Also see your example posted here against the one in the GitHub
Code:
namespace "qvc" {
class QChar {
private:
    ::QChar* origin_obj;
public:
    QChar();
    ~QChar();
...
};

qvc::QChar::~QChar(void)
{
    #ifdef DEBUG
    std::wcout << L"cpp: QChar: dtor..." << std::endl;
    #endif
    try {
        if (nullptr != origin_obj) {
            std::wcout << L"not null" << std::endl;
            delete origin_obj;
        }
    }
    catch (std::exception &e) {
        std::wcout << L"Exception: ";
        std::wcout << e.what() << std::endl;
    }
}
}
Code:
namespace qvc {

qvc::QChar::QChar(void     ) { origin_obj = new ::QChar( ); }
qvc::QChar::QChar(char    t) { origin_obj = new ::QChar(t); }
qvc::QChar::QChar(uint8_t t) { origin_obj = new ::QChar(t); }
qvc::QChar::QChar(uint16_t t) { origin_obj = new ::QChar(t); }
qvc::QChar::QChar(uint32_t t) { origin_obj = new ::QChar(t); }
qvc::QChar::QChar(wchar_t t) { origin_obj = new ::QChar(t); }
qvc::QChar::QChar(short   t) { origin_obj = new ::QChar(t); }
...

qvc::QChar::~QChar(void)
{
    #ifdef DEBUG
    std::wcout << L"cpp: QChar: dtor..." << std::endl;
    #endif
    try {
        if (nullptr != origin_obj) {
            std::wcout << L"not null" << std::endl;
            delete origin_obj;
        }
    }
    catch (std::exception &e) {
        std::wcout << L"Exception: ";
        std::wcout << e.what() << std::endl;
    }
}
See the difference ?

In all cases, QChar is 2 bytes and this is smaller and lighter than using dynamic one with extra pointer (either 32 or 64 bit, meaning 4 or 8 bytes) with overhead of allocating and deallocating, just slap it where ever you need to use, as static and in place then use it and don't free it, because it does not need to be free.

In other words use it as you use int or LONG .... the only difference is it will have initial value, and comes with methods, that is it.
Kas
  Mit Zitat antworten Zitat