Routine C language data structures

Handling CSDN self https://blog.csdn.net/u013213111/article/details/93784522

Followed DSAA in C to write code, summed up the various data structures are probably the routine:

The first is the definition:

typedef struct XXXXX
{
    //数据结构包含的元素
} XXX

Then allocate space malloc(sizeof(XXX))
for the array used to store data elements, but also the extra space allocated to the array, such as queue:
Q->array = malloc(sizeof(int) * max);
last used up must remember free!

This routine is widely used, such as the picture you want as a data structure, so first of all be defined:

typedef struct ImgStruct
{
    uint8_t *data;
    int width;
    int height;
} Image;

Then import pictures (here with the OpenCV, is C ++, need to look at the packaging function can be called C):

Image *image_import(char *img_name)
{
    Image *img;
    img = (Image *)malloc(sizeof(Image));
    cv::Mat img_mat = cv::imread(img_name, cv::IMREAD_GRAYSCALE);
    img->width = img_mat.cols;
    img->height = img_mat.rows;
    int img_size = img_mat.cols * img_mat.rows;
    img->data = (uint8_t *)malloc(img_size);
    //cv::imshow("image", img);
    for (int i = 0; i < img_mat.rows; i++)
    {
        for (int j = 0; j < img_mat.cols; j++)
        {
            img->data[i * img_mat.rows + j] = img_mat.at<uchar>(i, j);
        }
    }
    return img;
}

Thus only the main function either:

    Image *src;
    src = image_import("demo.bmp");

Finally free, NULL is set to prevent dangling pointer:

void image_free(Image *img)
{
    free(img->data);
    img->data = NULL;
    free(img);
    img = NULL;
}

Emphasize the problem of dangling pointers, reference dangling pointer (Dangling pointer) to avoid method :

What is a dangling pointer?
A pointer of the memory is released, the pointer is a vacant.
Dangling pointer harm?
Access dangling pointer, the result of random. May cause the program does not function properly, it may cause the program to crash. If other functions are affected, the problem is often difficult to locate.
How to avoid dangling pointers?
The basic idea: a memory when released, will point to this memory pointer variable is set to NULL. Before the visit pointer variable, first determine whether to NULL.
Advanced: When there are multiple pointer variables point to the same time a memory, this memory is released, all the required value of the pointer variables are set to NULL, it points to the need to maintain information about all of this memory pointer variable, but this big spending ways, they are usually rarely used. Frequency of use of the object is not very high, you can find an index based on the id, etc. Prior to use, if not, then do not use. If there is a user, you can not release this memory, we can use the reference count only if the reference count is 0, really release the memory, otherwise, just the reference count is decremented.

BTW, the various functions of the data structure of a plurality DSAA is passed as a parameter a pointer, of course also be passed by value, the difference can be referred to the C language structure by value and pass-Analysis :

Structure of the object as a parameter, the compiler be a copy, (we can find different by the incoming address and main in). In this case the operation is a function of its copy operation, does not affect the origin value main function. The disadvantage is that, when the structure variables is very large, the compiler be copied, large overhead. When the structure of the address as a parameter, and the main function operation subroutine operation is the same structure, this time passing a parameter address. The advantage is that no copy, but be careful when using, if you do not want to change its value, the const keyword required modification.

Guess you like

Origin www.cnblogs.com/lyrich/p/11110596.html