c/c++语言开发共享消除VS中动态申请二维数组C6011,C6385,C6386的警告

VS,警告,C语言,二维数组,动态申请,C6011,C6385,C6386,消除,指针,malloc,stdlib.h,free,释放内存,NULL,缓冲区,防伪标识。 介绍如何规范的在Visual Studio 2019中用C语言动态申请二维数组,避免各种令人讨厌的警告。 …

动态申请二维数组,无非就是通过指针来实现。@wowph
过程分三步:1、申请内存,2、使用数组,3、释放内存。
代码如下:

/********************************************************************     description: 动态申请二维数组     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("输入行数和列数:");     scanf_s("%d %d", &row, &col);          int** matrix = null;// 初始化为null          // 动态申请内存     matrix = (int**)malloc(row * sizeof(int*));     for (int i = 0; i < row; ++i) {         matrix[i] = (int*)malloc(col * sizeof(int));     }          // 给二维数组赋值     for (int i = 0; i < row; ++i) {         for (int j = 0; j < col; ++j) {             matrix[i][j] = i * col + j;         }     }      // 输出二维数组     for (int i = 0; i < row; ++i) {         for (int j = 0; j < col; ++j) {             printf("%2d", matrix[i][j]);             putchar((j < col - 1) ? ' ' : 'n');         }     }          // 释放内存     for (int i = 0; i < row; ++i) {         free(matrix[i]);     }     free(matrix);     return 0; }

这是最中规中矩的vs代码了。但是这段代码会出现 5 个警告(分 3 类):

严重性 代码  说明                                                                                  行 警告  c6011   取消对 null 指针“matrix[i]”的引用。                                                      16 警告  c6386   写入到“matrix”时缓冲区溢出: 可写大小为“row*sizeof(int *)”个字节,但可能写入了“8”个字节。        11 警告  c6386   写入到“matrix[i]”时缓冲区溢出: 可写大小为“col*sizeof(int)”个字节,但可能写入了“8”个字节。   16 警告  c6385   从“matrix[i]”中读取的数据无效: 可读大小为“col*sizeof(int)”个字节,但可能读取了“8”个字节。   22 警告  c6385   从“matrix”中读取的数据无效: 可读大小为“row*sizeof(int *)”个字节,但可能读取了“8”个字节。        28

如何去掉这些警告呢?

安全的写法如下所示:

/********************************************************************     description: 动态申请二维数组     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("输入行数和列数:");     scanf_s("%d %d", &row, &col);          int** matrix = null;// 初始化为null      // 消除c6085和c6086警告     if (row <= 0 || col <= 0) {         printf("行数或列数不合法!n");         exit(-1);     }      // 动态申请内存     matrix = (int**)malloc(row * sizeof(int*));          // 消除c6011警告     if (null == matrix) {         printf("无法动态申请内存!n");         exit(-1);     }      for (int i = 0; i < row; ++i) {         matrix[i] = (int*)malloc(col * sizeof(int));          // 消除c6011警告         if (null == matrix[i]) {             printf("无法动态申请内存!n");             exit(-1);         }     }      // 给二维数组赋值     for (int i = 0; i < row; ++i) {         for (int j = 0; j < col; ++j) {             matrix[i][j] = i * col + j;         }     }      // 输出二维数组     for (int i = 0; i < row; ++i) {         for (int j = 0; j < col; ++j) {             printf("%2d", matrix[i][j]);             putchar((j < col - 1) ? ' ' : 'n');         }     }          // 释放内存     for (int i = 0; i < row; ++i) {         free(matrix[i]);     }     free(matrix);     return 0; }

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


– end – wowph – pfdvnah –

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/601000.html

(0)
上一篇 2021年5月9日
下一篇 2021年5月9日

精彩推荐