Help with compression program

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2003
Posts: 22
Reputation: Mike29936 is an unknown quantity at this point 
Solved Threads: 0
Mike29936 Mike29936 is offline Offline
Newbie Poster

Help with compression program

 
0
  #1
Dec 23rd, 2003
The other day, I got an idea for a compression program, and decided to write up a function that compresses a file into "filename.compressed".

Compression function works fine, but I get a nasty assertion failure at the return of the main() function, after the file's been compressed. Assertion failure is on line 1017 of dbgheap.c, the expression being asserted is _BLOCK_TYPE_IS_VALID_(pHead->nBlockUse).

Here's my code:

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. #include <deque>
  5. using namespace std;
  6. void addstrings(char **first,char *second) {
  7. int size = strlen(*first)+strlen(second);
  8. char *temp = new char[size+1];
  9. strcpy(temp,*first);
  10. strcpy(temp+strlen(*first),second);
  11. delete [] *first;
  12. *first=temp;
  13. }
  14.  
  15. int compress(char *file,int level,int *additional=NULL) {
  16. int ret=0;
  17. FILE *thefile=fopen(file,"r");
  18. if (thefile) {
  19. int filesize=0;
  20. while (fgetc(thefile)!=EOF) filesize++;
  21. if (filesize%level) {
  22. ret=2;
  23. if (additional) *additional=filesize;
  24. } else {
  25. printf("Size of file: %d\n",filesize);
  26. rewind(thefile);
  27. char *temp = new char[filesize];
  28. fread(temp,filesize,1,thefile);
  29. int temp2;
  30. int temp3;
  31. deque<char*> table;
  32. char *tempstring=NULL;
  33. bool found=false;
  34.  
  35. for (temp2=0;temp2<filesize;temp2+=level) {
  36.  
  37. tempstring = new char[level+1];
  38. for (temp3=0;temp3<level;temp3++)
  39. tempstring[temp3]=temp[temp2+temp3];
  40. tempstring[temp3]=0;
  41. for (temp3=0;temp3<table.size();temp3++)
  42. if (strcmp(table[temp3],tempstring)==0) {
  43. found=true;
  44. break;
  45. }
  46. if (!found) table.push_back(tempstring);
  47. }
  48. char *tempfilestring = new char[strlen(file)+1];
  49. strcpy(tempfilestring,file);
  50. addstrings(&file,".compressed");
  51. fclose(thefile);
  52. thefile = fopen(file,"wb+");
  53. if (thefile) {
  54. fprintf(thefile,"%d\n",table.size());
  55. for (temp2=0;temp2<table.size();temp2++)
  56. fprintf(thefile,"%d%s\n",temp2,table[temp2]);
  57. for (temp2=0;temp2<filesize;temp2+=level) {
  58. tempstring = new char[level+1];
  59. for (temp3=0;temp3<level;temp3++)
  60. tempstring[temp3]=temp[temp2+temp3];
  61. tempstring[temp3]=0;
  62. for (temp3=0;temp3<table.size();temp3++)
  63. if (strcmp(tempstring,table[temp3])==0) {
  64. fprintf(thefile,"%d",temp3);
  65. break;
  66. }
  67. }
  68. fclose(thefile);
  69. }
  70. else ret = 3;
  71. delete [] temp;
  72. for (temp2=0;temp2<table.size();temp2++)
  73. delete [] table[temp2];
  74. table.clear();
  75. }
  76. } else ret = 1;
  77. return ret;
  78. }
  79. int main(void) {
  80. char *buffer = new char[256];
  81. int compression = 0;
  82. int result=0;
  83. while (compression<256) buffer[compression++]=0;
  84. printf("What file?\n");
  85. fgets(buffer,255,stdin);
  86. buffer[strlen(buffer)-1]=0;
  87. printf("Compression level?\n");
  88. scanf("%d",&compression);
  89. switch (compress(buffer,compression,&result)) {//Get file and level of compression from above, and call compress function
  90. case 0: printf("File compressed.\n");break;
  91. case 1: printf("Couldn't open file.\n");break;
  92. case 2: printf("Invalid compression level. Level must divide evenly into file size. Filesize was %d\n",result);break;
  93. case 3: printf("Error creating new file to put compressed information in. File name was %s.compressed.",buffer);break;
  94. }
  95. delete [] buffer;
  96. return 0;
  97. }

I started out learning C, so if the code's a bit C-like, it's because I'm stuck with my old C ways.

Note that I've yet to write a decompression routine, but the compression routine seems to compress a .txt with "hello" in it to a file 2 characters larger, although the "hello" portion seems to be garbled.

The compression is pretty simple. It loads the file into a spot in memory, checks for strings of length 'level', checks if that string is unique to all previous strings found, and if so, adds it in to a table. When it's done, it will have found all unique strings, and will then proceed to write them in the compressed file in this fashion:

0firststring
1secondstring
2thirdstring
And then in the file, will plot numbers corresponding to the string:

012012012

Will be this in the uncompressed file:
firststringsecondstringthirdstringfirststringsecondstringthirdstringfirststringsecondstringthirdstring

And I thought of it myself.

It has a few bugs in it, the 3 main ones for now being that it has trouble loading the file into memory, so it's compressing garbage as of now, it has an assertion failure at the end, and the level of compression must divide evenly into the file size.

So can anyone help?

(edit) Replaced first code with current code. Still gives an assertion failure after the return statement in main().
Last edited by Mike29936; Dec 23rd, 2003 at 2:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 22
Reputation: Mike29936 is an unknown quantity at this point 
Solved Threads: 0
Mike29936 Mike29936 is offline Offline
Newbie Poster

Re: Help with compression program

 
0
  #2
Dec 23rd, 2003
Hmm, I just noticed that I forgot to load the file into memory, and to rewind() the file after I found how big it was.

I fixed that, and now it seems to get the unique strings. All that's left is to fix some other stuff.

Still has an assertion failure, though.

(edit) Pretty much fixed everything in the compression code, except for the assertion failure. I haven't been able to find anything that causes that, and when I run my debugger on it, it happens after the return statement on main().
Last edited by Mike29936; Dec 23rd, 2003 at 2:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member

Re: Help with compression program

 
0
  #3
Jan 1st, 2004
I haven't looked through your code - maybe you could post your latest version as you've obviously updated it with some fixes since you first posted it. The assertion failure you're seeing, this is often caused by accessing some memory that doesn't belong to you while the program is executing. This can show up in many different ways, sometimes as a crash or some other observable behaviour during execution, but sometimes it goes unnoticed until the program terminates, just as you describe.

If you can post your code it might be possible to spot something. Maybe check that you don't write outside array bounds anywhere or abuse your pointers.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC