#Help with Huffman

9 messages · Page 1 of 1 (latest)

narrow sandal
#

I'm working on this GenerateLeafList method

    // Generate the leaf list for the Huffman algorithm (used in READ AND WRITE)
    //
    // Note:    Will cause leaks until ClearTree is implemented
    void GenerateLeafList() {
        // 1. Iterate through the frequency table and dynamically create a leaf node for each non-0
        // frequency. Add it to the mLeafList vector.
        mLeafList.clear();
        for (int i = 0; i < std::size(mFrequencyTable); i++)
        {
            if (mFrequencyTable[i] != 0 ) //&& mFrequencyTable[i] != 256
            {
                //Nodes should be dynamically alocated before put in leaf list
                HuffNode* newLeaf = new HuffNode(i, mFrequencyTable[i]);
                mLeafList.push_back(newLeaf);
            }
        }
    }

The error when I try to test is Exception thrown: read access violation.
_Pnext was 0x8. on the function

#if _ITERATOR_DEBUG_LEVEL == 2
    _CONSTEXPR20 void _Orphan_range_unlocked(pointer _First, pointer _Last) const {
        _Iterator_base12** _Pnext = &_Mypair._Myval2._Myproxy->_Myfirstiter;
        while (*_Pnext) {
            const auto _Pnextptr = static_cast<const_iterator&>(**_Pnext)._Ptr;
            if (_Pnextptr < _First || _Last < _Pnextptr) { // skip the iterator
                const auto _Temp = *_Pnext; // TRANSITION, VSO-1269037
                _Pnext           = &_Temp->_Mynextiter;
            } else { // orphan the iterator
                const auto _Temp = *_Pnext; // TRANSITION, VSO-1269037
                _Temp->_Myproxy  = nullptr;
                *_Pnext          = _Temp->_Mynextiter;
            }
        }
    }```
rugged ploverBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

fast barn
#

What is mFrequencyTable

#

mLeafList.clear(); is almost certainly a memory leak btw

#

You'll have to look at the stack to understand why something is going wrong in _Orphan_range_unlocked

#

The thing about memory errors / corruption is they tend to manifest far from their source

#

!wiki asan

rugged ploverBOT
# fast barn !wiki asan
How To Use Sanitizers

Sanitizers are tools which generate additional code in your program that can catch many common programming mistakes,
such as:

General Advice

Not all sanitizers can be combined, but when they can, use e.g.:
-fsanitize=address,undefined to combine them.
Always compile with debug info to get line numbers, variable names, etc.

MSVC 19.27+ and VS 2019 16.9+
Sample Program
int main(void) {
    int x;
    return x;
}
`-fsanitize=memory -g` Output

SUMMARY: MemorySanitizer: use-of-uninitialized-value /tmp/test.cpp:3:5 in main
Exiting
(3:5 is line and column of return)

rugged ploverBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.