Thread: Increment gone wrong

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by awsdert View Post
    After adding some print statements to track the results of tables I found it's not the reading of the encoding thats the issue, its the point that I move each symbol to an index that matches it's uid (for speed during the deflation process):

    Code:
    typedef struct _ZLIB_SYMBOL
    {
        uint uid;
        uint src;
        uint val;
        uint sym;
        uint lit;
        uint len;
        uint get;
        uint cpy;
        uint use;
        uint nxt[2];
    } ZLIB_SYMBOL;
    ...
    int CompareSymbols( void const *A, void const *B )
    {
        ZLIB_SYMBOL const *a = A, *b = B;
    
        if ( a->use != b->use )
            return b->use - a->use;
    
        if ( a->len != b->len )
            return a->len - b->len;
    
        return a->uid - b->uid;
    }
    ...
    int SortZlibSymbolsByUID( ZLIB *zlib, int id )
    {
        uint j = 0;
        ZLIB_SYMBOLS *Symbols = &(zlib->Symbols[id]);
        BUFFER *Entries = AccessBuffer( zlib->Buffers, Symbols->entries );
        ZLIB_SYMBOL *symbols = Entries->addr, *dst, *src;
        FILE *verbose = zlib->Buffers->Alloc->verbose;
    
        if ( verbose && !quiet_zlib )
        {
            ECHO
            (
                verbose,
                fprintf( verbose, "Prior to qsort() " );
                EchoZlibSymbolsListDetails( verbose, zlib, id )
            );
        }
    
        qsort( symbols, Entries->used, sizeof(ZLIB_SYMBOL), CompareSymbols );
    
        if ( verbose && !quiet_zlib )
        {
            ECHO
            (
                verbose,
                fprintf( verbose, "Prior to manual " );
                EchoZlibSymbolsListDetails( verbose, zlib, id )
            );
        }
    
        symbols = GrowZlibSymbolsBuffer( zlib, id, Symbols->highest + 1 );
    
        if ( !symbols )
            return ENOMEM;
    
        for ( j = Entries->used - 1; j; --j )
        {
            src = symbols + j;
            dst = symbols + src->uid;
    
            if ( src->uid > j )
            {
                *dst = *src;
                /* This is where I'm loosing data when I shouldn't be (in theory anyways) */
                memset( src, 0, sizeof(ZLIB_SYMBOL) );
            }
        }
    
        Entries->used = Symbols->highest + 1;
    
        return 0;
    }
    I've added a comment to the point I can see is causing the error down the line, although in theory shouldn't be, any ideas on how to fix it? Or even better any ideas on how to fob off the duty to CompareSymbols()?
    Your problem is in here, from "buffer.c".

    Code:
    #include "memory.h"
    
    void * default_allocator( void *ud, void *ptr, size_t had, size_t get )
    {
            uchar *tmp;
            (void)ud;
    
            if ( !get )
            {
                    if ( ptr )
                            free( ptr );
    
                    return NULL;
            }
    
            tmp = ptr ? malloc( get ) : realloc( ptr, get );
    
            if ( !tmp )
                    return NULL;
    
    
            if ( get > had )
                    memset( tmp + had, 0, get - had );
    
    
            return tmp;
    }
    ...
    Last edited by hamster_nz; 08-14-2021 at 02:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hex increment
    By davidx in forum C Programming
    Replies: 3
    Last Post: 10-19-2019, 07:06 AM
  2. Two pre increment in one expression
    By h255874 in forum C Programming
    Replies: 4
    Last Post: 09-21-2019, 08:47 AM
  3. Post Increment an Pre Increment operators in c++
    By anil_ in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2011, 08:27 PM
  4. can't get loop to increment
    By rivkyfried1 in forum C Programming
    Replies: 2
    Last Post: 10-11-2010, 04:03 AM
  5. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM

Tags for this Thread