Problems with bit-packing and dynamically allocating

178 views Asked by At

Im having trouble with this piece of code.

Im building an image encoder. Basically I built an encoding array using values from the image. The array is called 'codes', and stores char* representations of what the binary values will be.

This section reads the grey value of each pixel, looks up its value in the 'codes' array, and packs a byte of binary values(tempString). Once 8 values have been read, the tempString is added to the end of the already encoded unsigned byte array(encodedString).

The program runs until numBytes is about 27 thousand bytes then seg faults.

I know its a long shot, but Im hoping its a glaring problem with how I allocate memory.

    unsigned char* encodedString = malloc(1);
    unsigned char* tempString;
    encodedString[0] = '\0';

    unsigned char packedString = 0;
    int one = 1;
    int zero = 0;
    int width = image->width;
    int height = image->height;
    int row, col, count=0, numBytes=0; //numBytes is the number of already encoded bytes
    for(row = 0; row<height; row++)
    for(col = 0; col<width; col++)
    {
            int value = image->pixel[row][col];    //Gets the pixel value(0-255)
            char* code = codes[value];             //Gets the compression code for the color

            int length = strlen(code);

            for(index=0;index<length;index++)
            {
                    //This loop goes through every character in the code 'string'
                    if(code[index] == '1')
                            packedString = packedString | one;
                    else
                            packedString = packedString | zero;

                    count++;
                    if(count == 8)  //If 8 consecutive values have been read, add to the end of the encoded string
                    {
                            tempString = realloc(encodedString, (strlen(encodedString)+2));
                            if(tempString == NULL)
                                    return NULL;

                            encodedString = tempString;

                            //Add newly formed binary byte to the end of the already encoded string
                            encodedString[numBytes] = packedString;
                            //Add terminating character to very end             
                            encodedString[numBytes+1] = '\0';

                            count=0; //reset count
                            numBytes++;
                    }
                    else
                         packedString = packedString << 1;
            }

            *length_of_encoded_string += strlen(codes[value]);

    }
1

There are 1 answers

0
bw1024 On BEST ANSWER

Don't call strlen(str) on a binary string! Use your numBytes instead. strlen will return the index of the first zero it finds in your string. Then realloc will stop increasing the size of your your string, leading to the segfault when you access it using numBytes.