简体   繁体   中英

Can't get rid of warning c6385 while trying to free dynamic memory (2D matrix) c

I've been breaking my head over this issue, VScode gives me this weird warning about malloc calculations

c6385 "Severity Code    Description Project File    Line    Suppression State
Warning C6385   Reading invalid data from 'temp':  the readable size is 'sizeof(int *)*width_flag' bytes, but '16' bytes may be read"

And again with

Warning C6386   Buffer overrun while writing to 'temp':  the writable size is 'sizeof(int *)*width_flag' bytes, but '16' bytes might be written"

I think I am doing mallloc and free by the book and still this comes up

the code

poolList_t* pools_f(pixmat** mtrx, image_t image, poolList_t* pools, int width_flag) {
int i, j, size;
int** temp = NULL;
pix_t* root = NULL;
co_t center;
if (sizeof(int*) * width_flag > 0)
    temp = malloc(sizeof(int*) * width_flag); //alocating memory for matrix which will contain 1/0 for each blue/non-blue pixel
if (temp) {
    if (sizeof(int) * image.height > 0)
        for (i = 0;i < width_flag; i++) {
            temp[i] = malloc(sizeof(int) * image.height);
        }// allocate memory to temp color signed matrix
}

if (temp != 0)
{

    for (i = 0; i < image.height; i++) {
        for (j = 0;j < width_flag;j++) {
            if (mtrx[j][i].color.r == 155 && mtrx[j][i].color.g == 190 && mtrx[j][i].color.b == 245) { // Registering blue and non-blue pixel to 2d matrix named temp
                temp[j][i] = 1;
            }
            else {
                temp[j][i] = 0;
            }
        }
    }
    for (i = 0;i < image.height;i++) {
        for (j = 0;j < width_flag;j++) {
            if (temp[j][i] == 1) { //Going over all the pixels in the matrix and checking if it is blue or not
                size = 1;
                temp[j][i] = 0;
                pix_insert(&root, mtrx[j][i].cordinate); //saving the blue pixel to the head of linked list
                segment(root, mtrx, temp, image, i, j, &size); //entering to Image segmentation function to find adjacent blue pixels
                if (size > 9) {
                    center = pool_middle(root, size);
                    pool_insert(&pools, size, center, root); //saving the pool to linked list ONLY if it has size of 10 or more
                }
            }
            deallocpix(&root); //deallocting the memory of the pixel's linked list
        }
    }
    for (i = 0;i < width_flag;i++) {
        free(temp[i]); //deallocating temp matrix memory
    }
    free(temp);
    return pools;
} else
    return pools;
}

the structs I use

typedef struct pool { //pools' list extructed of bmp
int size; //the number of pixels that combine the pool
co_t pool_center;
pix_t* pix; //list of pool pixels
struct pool* next;
}poolList_t;

typedef struct pixel { //list of pixels
co_t p;
struct pixel* next;
}pix_t;

typedef struct { //pixel's coordinates
int x;
int y; 
 }co_t;

typedef struct { // struct to contain each pixel color & coordinates
color_t color;
co_t coordinate;
} pixmat;

Using arrays of pointers adds unnecesary indirection. Use array pointers

if(width_flag && image.height)
{
    int (*temp)[image.height];

    temp = malloc(sizeof(*temp) * width_flag); //alocating memory for matrix which will contain 1/0 for each blue/non-blue pixel
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM