Eliminate the warning of dynamic request 2D array C6011, C6385, C6386 in VS

Dynamic application of two-dimensional array is realized by pointer. @wowpH
The process is divided into three steps: 1. Apply for memory; 2. Use array; 3. Release memory.
The code is as follows:

/********************************************************************
    description: Dynamic application 2D array
    author: wowpH
    csdnid: pfdvnah
    date  : 2019-11-9 15:38:25
    from  : https://blog.csdn.net/pfdvnah/article/details/102987174
********************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(void) {
    int row, col;
    printf("Enter the number of rows and columns:");
    scanf_s("%d %d", &row, &col);
    
    int** matrix = NULL;// Initialize to NULL
    
    // Dynamic request memory
    matrix = (int**)malloc(row * sizeof(int*));
    for (int i = 0; i < row; ++i) {
        matrix[i] = (int*)malloc(col * sizeof(int));
    }
    
    // Assign a value to a two-dimensional array
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            matrix[i][j] = i * col + j;
        }
    }

    // Output 2D array
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            printf("%2d", matrix[i][j]);
            putchar((j < col - 1) ? ' ' : '\n');
        }
    }
    
    // Free memory
    for (int i = 0; i < row; ++i) {
        free(matrix[i]);
    }
    free(matrix);
    return 0;
}

This is the most proper VS code. But there are five warnings in this code (divided into three categories):

Severity code description line
 Warning C6011 dereference NULL pointer 'matrix[i]' Sixteen
 Warning C6386 buffer overflow when writing to "matrix": writable size is "row*sizeof(int *)" bytes, but "8" bytes may have been written Eleven
 Warning C6386 buffer overflow when writing to "matrix[i]": writable size is "col*sizeof(int)" bytes, but "8" bytes may have been written Sixteen
 Warning C6385 invalid data read from 'matrix[i]': readable size is' col*sizeof(int) 'bytes, but may have read' 8 'bytes Twenty-two
 Warning C6385 invalid data read from "matrix": read size is "row*sizeof(int *)" bytes, but may have read "8" bytes Twenty-eight

How to remove these warnings?

Security is written as follows:

/********************************************************************
    description: Dynamic application 2D array
    author: wowpH
    csdnid: pfdvnah
    date  : 2019-11-9 15:38:25
    from  : https://blog.csdn.net/pfdvnah/article/details/102987174
********************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(void) {
    int row, col;
    printf("Enter the number of rows and columns:");
    scanf_s("%d %d", &row, &col);
    
    int** matrix = NULL;// Initialize to NULL

    // Remove C6085 and C6086 warnings
    if (row <= 0 || col <= 0) {
        printf("Illegal number of rows or columns!\n");
        exit(-1);
    }

    // Dynamic request memory
    matrix = (int**)malloc(row * sizeof(int*));
    
    // Remove C6011 warning
    if (NULL == matrix) {
        printf("Unable to dynamically request memory!\n");
        exit(-1);
    }

    for (int i = 0; i < row; ++i) {
        matrix[i] = (int*)malloc(col * sizeof(int));

        // Remove C6011 warning
        if (NULL == matrix[i]) {
            printf("Unable to dynamically request memory!\n");
            exit(-1);
        }
    }

    // Assign a value to a two-dimensional array
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            matrix[i][j] = i * col + j;
        }
    }

    // Output 2D array
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            printf("%2d", matrix[i][j]);
            putchar((j < col - 1) ? ' ' : '\n');
        }
    }
    
    // Free memory
    for (int i = 0; i < row; ++i) {
        free(matrix[i]);
    }
    free(matrix);
    return 0;
}

Original link: https://blog.csdn.net/pfdvnah/article/details/102987174

- End - wowpH - pfdvnah -

Keywords: C

Added by vikramjeet.singla on Sun, 10 Nov 2019 17:11:52 +0200