1

I am trying to write code to test the speed of lz4 compression instead of using -b code in terminal. I am using ubuntu Ubuntu 20.04.2 LTS. And visual studio ide. Here is my code, it has some trouble when running the second time of the for loop in main.
This line: fread(src,16384,1,fc);

When i = 1, it works as I expected. However, when i = 2, the malloc() problem just pop up.

//The split file function works perfectly.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "lz4.h"
int splitFile(char* fileIn, size_t maxSize);
#include "lz4.c"


int main()
{
    time_t start,end;
    int compress, decompress;
    int64_t num_f=0;
    int64_t i;



    char* src=malloc(16384);
    int srcSize= 16385;//(int)(strlen(src) + 1);
    int dstCapacity=0; 
    int compressed_data_size=0;
    
    char* compressed_data = malloc((size_t)dstCapacity);
    char buff[200];

    FILE * fc;

    num_f = splitFile("/home/ziruo/research/1stpro/test.txt",16384);
    printf("num_f=%ld\n",num_f);

    start = time(NULL);
    for( i = 1; i <= num_f; i++)
    {
        sprintf(buff,"/home/ziruo/research/1stpro/test.txt.%03d",i);
        printf("buff %s\n",&buff);
        if (compressed_data == NULL)
        {
            printf("faild to generae storage\n");
        }
        fc = fopen(buff,"r");
        printf("fc is: %d\n",fc);

        fread(src,16384,1,fc);
        srcSize=(int)(strlen(src) + 1);
        dstCapacity= LZ4_compressBound(srcSize)
        compressed_data_size = LZ4_compress_default(src,compressed_data,srcSize,dstCapacity);
        LZ4_compress_default(src,compressed_data,srcSize,dstCapacity);
        printf("data size ratio %.2f\n", (float)compressed_data_size/srcSize);
        
    }

    end = time(NULL);

    printf("time used(s): %f\n",difftime(end,start));


    return 0;

}




int splitFile(char* fileIn, size_t maxSize)
{
    int result = 0;
    FILE* fIn;
    FILE* fOut;
    char buffer[1024 * 16];
    size_t size;
    size_t read;
    size_t written;


    if ((fileIn != NULL) && (maxSize > 0))
    {
        fIn = fopen(fileIn, "rb");
        if (fIn != NULL)
        {
            fOut = NULL;
            result = 1;   // we have at least one part

            while (!feof(fIn))
            {
                // initialize (next) output file if no output file opened
                if (fOut == NULL)
                {
                    sprintf(buffer, "%s.%03d", fileIn, result);
                    fOut = fopen(buffer, "wb");
                    if (fOut == NULL)
                    {
                        result = -1;
                        break;
                    }

                    size = 0;
                }

                // calculate size of data to be read from input file in order to not exceed maxSize
                read = sizeof(buffer);
                if ((size + read) > maxSize)
                {
                    read = maxSize - size;
                }

                // read data from input file
                read = fread(buffer, 1, read, fIn);
                if (read == 0)
                {
                    result = -1;
                    break;
                }

                // write data to output file
                written = fwrite(buffer, 1, read, fOut);
                if (written != read)
                {
                    result = -1;
                    break;
                }

                // update size counter of current output file
                size += written;
                if (size >= maxSize)   // next split?
                {
                    fclose(fOut);
                    fOut = NULL;
                    result++;
                }
            }

            // clean up
            if (fOut != NULL)
            {
                fclose(fOut);
            }
            fclose(fIn);
        }
    }

    return (result);
}

And here is the picture of error enter image description here

Ziruo Jin
  • 21
  • 3
  • 4
    `int dstCapacity=0; char* compressed_data = malloc((size_t)dstCapacity);` allocates a 0 sized block of memory. Maybe you want to calculate the destination size required before allocating? – Retired Ninja Jun 10 '21 at 03:46
  • Please learn how to create a [mcve] to show us, with emphasis on the *minimal* part. Minimizing your programs help with debugging and makes it easier to find the source of the problem. – Some programmer dude Jun 10 '21 at 04:20
  • `char* src=malloc(16384); int srcSize=16385;` One of these numbers is not like the other. – n. m. could be an AI Jun 10 '21 at 06:51
  • @Retired Ninja I am trying to give it a size and then calculate the size of it from the LZ4_compressBound function. – Ziruo Jin Jun 10 '21 at 15:53
  • @n. 1.8e9-where's-my-share m. I think the srcSize is based on the int(strlen(src) +1); – Ziruo Jin Jun 10 '21 at 15:56
  • `strlen(src)` cannot be more than 16383, whatever you put there. – n. m. could be an AI Jun 10 '21 at 17:50
  • @ZiruoJin `char* compressed_data = malloc((size_t)dstCapacity);` allocates a 0 sized block of memory. You need to figure out what `dstCapacity` needs to be before you allocate the memory. You've rearranged the code now, but in the first iteration of it it just looked like you had two lines reversed. – Retired Ninja Jun 10 '21 at 22:15

0 Answers0