I have been programming a compressor based on lz for my school project.When i used fixed block sizes there were no problems but now when i started using variable block sizes it crashes.The compression takes place without much problem but decompression crashes and debuggers does not report any error.

Uncompression code

#include<iostream>
#include<fstream>
#include<cstring>
#include<ctime>
#include<windows.h>
#include "cmpfmem.h"
#pragma pack(push, 1)
struct dicv{
    unsigned long inpos;
    unsigned long pos;
    unsigned char blocksize;
};
#pragma pack(pop)//4+2+1=7
#pragma pack(push, 1)
struct {
    unsigned long orgfilesize;
    unsigned long dicsize;
    unsigned char prop;
}fileinf;
#pragma pack(pop)//4+2+1=7
int main(int n,char *args[])
{
    //{
    SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
    char ifilename[512],ofilename[512];
    if(n<3)    {

        std::cout<<"Enter the ifile:";
        std::cin>>ifilename;
        std::cin.get();
        std::cout<<"Enter the ofile:";
        std::cin>>ofilename;
        std::cin.get();
    }
    else
    {
        strcpy(ifilename,args[1]);
        strcpy(ofilename,args[2]);
    }
    //}
    unsigned long filesize=ffunc::getfilesize(ifilename),dicc=0,insize=0,inpos=0,outpos=0,pos;
    unsigned char blocksize;
    dicv *dic;
    std::ifstream ifile;
    ifile.open(ifilename,std::ios::binary);
    ifile.read((char*)&fileinf,sizeof fileinf);
    dic= new dicv[fileinf.dicsize];
    for(unsigned long i=0;i<fileinf.dicsize;++i)
            ifile.read((char*)&dic[i],sizeof (dicv));
    std::cout<<fileinf.dicsize;
    std::cin.get();
    for(unsigned long j=0;j<fileinf.dicsize;++j)
        std::cout<<dic[j].inpos<<"\t"<<dic[j].pos<<"\t"<<(int)dic[j].blocksize<<"\n";
    std::cin.get();
    insize=filesize-((fileinf.dicsize)*sizeof(dicv));
    unsigned char *in,*out;
    in=new unsigned char[insize];
    out=new unsigned char[fileinf.orgfilesize];
    for(unsigned long k=0;k<insize;++k)
        ifile.read((char*)&in[k],sizeof in[k]);
    ifile.close();
    while(inpos<insize)
    {
        if(dic[dicc].inpos==outpos)
        {
            std::cout<<"\n"<<outpos;
            pos=dic[dicc].pos;
            blocksize=dic[dicc].blocksize;
            std::cout<<"\t"<<pos<<"\t"<<(int)blocksize;
            //std::cin.get();
            for(unsigned char m=0;m<blocksize;++m)
                out[outpos++]=in[pos+m];
            ++dicc;
            std::cout<<"\t"<<dicc;
        }
        else
        {
            out[outpos++]=in[inpos++];
        }

    }
    std::cin.get();
    std::cin.get();
}

Compressor

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<conio.h>
#include<cstring>
#include<ctime>
#include<windows.h>
#include "cmpfmem.h"
#pragma pack(push, 1)
struct dicv{
    unsigned long inpos;
    unsigned long pos;
    unsigned char blocksize;
};
#pragma pack(pop)//4+2+1=7
#pragma pack(push, 1)
struct {
    unsigned long orgfilesize;
    unsigned long dicsize;
    unsigned char prop;
}fileinf;
#pragma pack(pop)//4+2+1=7
struct{
    unsigned char len;
    unsigned long pos;
}bestmatch;
inline void writeout(char filename[],dicv dic[],
                     unsigned long dicsize,unsigned char out[],unsigned long outpos){
    std::ofstream ofile;
    ofile.open(filename,std::ios::binary);
    ofile.write((char*)&fileinf,sizeof fileinf);
    for(unsigned long int j=0;j<dicsize;++j)
        ofile.write((char*)&dic[j],sizeof dic[j]);
    for(unsigned long int i=0;i<outpos;++i)
        ofile.write((char*)&out[i],sizeof out[i]);
    ofile.close();
}
inline void Compressormain(unsigned char in[],unsigned char out[],
                            unsigned long filesize,short int blocksizelim,char ofilename[]){
    dicv *dic=NULL;
    unsigned long dicsize=filesize,inpos=0,outpos=0,charpos=0,dicc=0,limit=0,limit2=0,len=0,offset=0,maxoffset=32000;
    dic=new dicv [dicsize];
    for(unsigned i=0;i<blocksizelim;++i)
        out[outpos++]=in[inpos++];
    while(inpos<filesize)
    {
        printf("\b\b\b\b\b\b\b%d",inpos);
        limit2=filesize-inpos<255?filesize-inpos:255;
        bestmatch.len=0;
        for(charpos=inpos-blocksizelim,offset=0;charpos>0 && bestmatch.len<blocksizelim && offset<maxoffset;--charpos,++offset)
        {

            limit=limit2<inpos-charpos?limit:inpos-charpos;
            for(len=0;in[inpos+len]==in[charpos+len] && len<limit;++len);
            if(len>=bestmatch.len)
            {
                bestmatch.len=len;
                bestmatch.pos=charpos;
            }

        }
        if(bestmatch.len>7)
        {
            std::cout<<"\t"<<bestmatch.pos<<"\t"<<(int)bestmatch.len<<"\n";
            dic[dicc].inpos=inpos;
            dic[dicc].pos=bestmatch.pos;
            dic[dicc++].blocksize=bestmatch.len;
            inpos+=bestmatch.len;
        }
        else
            out[outpos++]=in[inpos++];
    }
    fileinf.orgfilesize=filesize;
    fileinf.dicsize=dicc;
    fileinf.prop=0;
    writeout(ofilename,dic,dicc,out,outpos);
}
int main(int n,char *args[]){
        SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
        system("cls");
//{
//==============================================================================
//                         Variable Declarations
//==============================================================================
    //Time calculations
        time_t begin,end=0,diff=0;
        clock_t clocks;
    //Variables
        unsigned char Marker=0;
        char filename[512],ofilename[512];
        if(n>1)
        {
            strcpy(filename,args[1]);
            strcpy(ofilename,args[2]);
        }
        else
        {
         std::cout<<"ifilename:";
         std::cin>>filename;
         std::cout<<"ofilename:";
         std::cin>>ofilename;
        }
        char debugmode=0;
    //Constants
        const unsigned long int ULfilesize=ffunc::getfilesize(filename);
        short int blocksizelim=0;
        if(n>1)
            blocksizelim=(short)atol(args[3]);
        else
            blocksizelim=255;
    //Pointers
        unsigned char *finbuffer=NULL,*foutbuffer=NULL;
//}
//{
//==============================================================================
//                         Dynamic Array Declartions
//==============================================================================

        finbuffer= new unsigned char [ULfilesize];
        foutbuffer= new unsigned char [ULfilesize];
//}
//{
//==============================================================================
//                         Processing of Data
//==============================================================================

        begin=time(NULL);
        ffunc::readfile(finbuffer,filename,ULfilesize);
        std::cout<<ULfilesize<<"\n";
        Compressormain(finbuffer,foutbuffer,ULfilesize,blocksizelim,ofilename);
        end=time(NULL);
        diff=end-begin;
        clocks=clock();
//}
//{
//==============================================================================
//                        Console Input and Outputs
//==============================================================================

        puts("Press 'd' for Stat mode and any other key to continue......");
        debugmode=getch();
        system("cls");
        std::cout<<"Filename: "<<filename<<"\n";
        std::cout<<"Filsize : "<<(float)ULfilesize/1024<<" KBs "<<"("<<ULfilesize<<" bytes)"<<"\n";
        std::cout<<"Blocksize Limit: "<<blocksizelim<<"\n";
        if(debugmode=='d')
        {
            std::cout<<"================\n"
                     <<"   Stat mode\n"
                     <<"================\n";

            std::cout<<"Marker  : "<<(short)Marker<<"\n";
            std::cout<<"Time for processing:"<<diff<<"\nClocks for processing:"<<clocks<<"\n";
            if(diff!=0)
                std::cout<<"KBs/s:"<<(float)((float)ULfilesize/1024)/diff<<"\n";
            std::cout<<"Bytes/clock:"<<(float)ULfilesize/clocks<<"\n";
        }
        getch();
//}
}

>Without No Reason
Damn straight. There's always a reason, even if it's not immediately obvious to you.

>when i started using variable block sizes it crashes
Then your first task should be verifying that you're reading from/writing to memory that you own. I'd wager that your problem is an out of bounds index somewhere.

On line 80 of your compressor code I encountered this: system("cls"); first: this has nothing to do with the crashes, but it isn't recommended to use the system command
(look here, this info does also apply to the "CLS" command)

As Narue said it's probably an override or out-of-bound error of dynamically allocated memory which is causing the crash :) ...

You must be using a magic compiler as your uncompression code isn't even compiling with me, look at the compiler's output:

Borland C++ 5.82 for Win32 Copyright (c) 1993, 2005 Borland
tmp.cpp:
Error E2209 tmp.cpp 6: Unable to open include file 'cmpfmem.h'
Error E2090 tmp.cpp 41: Qualifier 'ffunc' is not a class or namespace name in fu
nction main(int,char * *)
Error E2141 tmp.cpp 41: Declaration syntax error in function main(int,char * *)
Error E2451 tmp.cpp 55: Undefined symbol 'insize' in function main(int,char * *)

Error E2451 tmp.cpp 62: Undefined symbol 'inpos' in function main(int,char * *)
Error E2451 tmp.cpp 64: Undefined symbol 'dicc' in function main(int,char * *)
Error E2451 tmp.cpp 67: Undefined symbol 'pos' in function main(int,char * *)
Warning W8004 tmp.cpp 84: 'out' is assigned a value that is never used in functi
on main(int,char * *)
*** 7 errors in Compile ***

What's cmpfmem.h ? (that's probably why it isn't compiling: I don't have that file)

cmpfmem.h contains some functions that i developed when i created my first fixed blocksize algorithm.Under ffunc there are functions to get filesize,file prop and save files

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.