c_cpp alloc 2d array,memset和malloc

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp alloc 2d array,memset和malloc相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;

// alloc 2d array
int main() {
	int *a = (int*)malloc(sizeof(int)*16);
	memset(a, 0, sizeof(int)*16);

	// case 2
	int a[4][4];
	memset(a, 0, sizeof(int)*16;

	// case 3, alloc mem for char a[10][20]
	char **a;
	a = (char**)malloc(10*sizeof(char*));
	for(int i=0; i<10; i++)
		a[i] = (char*)malloc(sizeof(char)*20);

}

以上是关于c_cpp alloc 2d array,memset和malloc的主要内容,如果未能解决你的问题,请参考以下文章