I have been developing my own compression algorithm for my school project.It is like LZ77 but uses fixed blocks,and a dictionary containing the position of the repeating block (dic.inpos)& where it is to be copied(dic.charpos);I am using Gnu C++,The program crashes or gets stuck on initializing in..
Also the program runs almost finely when run in debugger..
(but never completes decompression)

#include<iostream>
#include<fstream>
#include "cmpfmem.h"
struct dicv
{
    unsigned long inpos;
    unsigned long charpos;
};
int main()
{
    unsigned short int  blocksize;
    unsigned long int dicsize;
    char filename[512]="F:\\Mana.vpcf";
    const unsigned long int ULfilesize=ffunc::getfilesize(filename);
    unsigned char *in=NULL,*out=NULL;
    dicv *dic=NULL;
    std::ifstream ifile;
    ifile.open(filename,std::ios::binary);
    ifile.read((char*)&blocksize,sizeof blocksize);
    ifile.read((char*)&dicsize,sizeof dicsize);
    dic=new dicv [dicsize];
    for(unsigned long i=0;i<=dicsize;++i)
    {
        ifile.read((char*)&dic[i],sizeof dic[i]);
    }
    unsigned long insize=ULfilesize-2-4-(8*dicsize);
    in=new unsigned char[ULfilesize-2-4-(8*dicsize)];
    for(unsigned long j=0;j<insize;++j)
    {
        ifile.read((char*)&in[j],sizeof in[j]);
    }
    out= new unsigned char[insize*blocksize];
    unsigned long searchin=0;
    unsigned long inpos=0,outpos=0;
    for(;inpos<=insize;)
    {
        if(dic[searchin].inpos==inpos)
            {
                for(unsigned long k=0;k<blocksize;++k)
                {
                    out[outpos++]=out[dic[searchin].charpos+k];
                }
                ++inpos;
                ++searchin;
            }
        else
            {
                out[outpos++]=in[inpos++];
            }
    }
    std::ofstream ofile;
    ofile.open("F:\\Mana1.ucf",std::ios::binary);
    for(unsigned long l=0;l<outpos;++l)
        ofile.write((char*)&out[l],sizeof out[l]);
    ofile.close();
    ifile.close();
}

> dic=new dicv [dicsize];
> for(unsigned long i=0;i<=dicsize;++i)
First problem, this runs off the end of the array.

Better check your other array subscripts as well.

I Tried that but the program crashes even before reaching there.It usually crashes at the dynamic allocation of in

Also when compiling the compiler frequently shows

Compiling: C:\Documents and Settings\Administrator\My Documents\c++\Compressor\uncmp2.cpp
Linking console executable: C:\Documents and Settings\Administrator\My Documents\c++\Compressor\uncmp2.exe
e:\Dev-Cpp\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\mingw32\bin\ld.exe: cannot open output file C:\Documents and Settings\Administrator\My Documents\c++\Compressor\uncmp2.exe: Permission denied
collect2: ld returned 1 exit status

> Also when compiling the compiler frequently shows
That's because the program is still running when you try to rebuild it (or you're still debugging it)

> I Tried that but the program crashes even before reaching there.It usually crashes at the dynamic allocation of in
Like I said, check them all.

> unsigned long insize=ULfilesize-2-4-(8*dicsize);
Because your first for loop reads 1 too many dic entries, the rest of the calculations for the rest of the file are also off.

Post your latest code.

Mention Problems are fixed but now the decompression stops before completion.The actual filesize is 511Kb.But Decompressed file only 411KB

#include<iostream>
#include<fstream>
#include "cmpfmem.h"
struct dicv
{
    unsigned long inpos;
    unsigned long charpos;
};
int main()
{
    unsigned short int  blocksize;
    unsigned long int dicsize;
    char filename[512]="F:\\Mana.vpcf";
    const unsigned long int ULfilesize=ffunc::getfilesize(filename);
    unsigned char *in=NULL,*out=NULL;
    dicv *dic=NULL;
    std::ifstream ifile;
    ifile.open(filename,std::ios::binary);
    ifile.read((char*)&blocksize,sizeof blocksize);
    ifile.read((char*)&dicsize,sizeof dicsize);
    dic=new dicv [dicsize];
    for(unsigned long i=0;i<dicsize;++i)
    {
        ifile.read((char*)&dic[i],sizeof dic[i]);
    }
    unsigned long insize=ULfilesize-2-4-(8*dicsize);
    in=new unsigned char[ULfilesize-2-4-(8*dicsize)];
    for(unsigned long j=0;j<insize;++j)
    {
        ifile.read((char*)&in[j],sizeof in[j]);
    }
    out= new unsigned char[insize*blocksize*2];
    unsigned long searchin=0;
    unsigned long inpos=0,outpos=0;
    for(;inpos<=insize;)
    {
        if(dic[searchin].inpos==inpos)
            {
                for(unsigned long k=0;k<blocksize;++k)
                {
                    out[outpos++]=out[dic[searchin].charpos+k];
                }
                ++inpos;
                ++searchin;
            }
        else
            {
                out[outpos++]=in[inpos++];
            }
    }
    std::ofstream ofile;
    ofile.open("F:\\Mana1.ucf",std::ios::binary);
    for(unsigned long l=0;l<outpos;++l)
        ofile.write((char*)&out[l],sizeof out[l]);
    ofile.close();
    ifile.close();
}
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.