10.动态内存管理(2)

栈区∶我们知道栈区在函数被调时分配,用于存放函数的参数值,局部变量等值。在windows中栈的默认大小是1M,在vs 中可以设置栈区的大小。在Liunx中栈的默认大小是10M,在gcc编译时可以设置栈区的大小。
堆区︰程序运行时可以在堆区动态地请求一定大小的内存,并在用完之后归还给堆区。在Liunx系统中堆区的大小接近3G。
一般情况下我们需要大块内存或程序在运行的过程中才知道所需内存大小,我们就从堆区分配空间。

int a = 10;
int b = 1;
int& c = a;
const int& x = a; //常引用

typedef int ElemType;  //指针(法一)
struct Array2   // 开辟二维数组
{
	ElemType* data;
	int row;
	int col;
};
void Init_Ar(struct Array2* par, int row, int col)
{
	assert(par != nullptr);
	(*par).row = row;
	(*par).col = col;
	(*par).data = (ElemType*)malloc(sizeof(ElemType) * row * col);
	if ((*par).data == nullptr) exit(1);

}
ElemType GetItem(struct Array2* par, int r, int c)
{
	assert(par != nullptr);
	assert(r < par->row || c < par->col);
	return par->data[r * par->col + c];
}
int main()
{
	Array2 ar;
	int row, col;
	scanf_s("%d %d", &row, &col);
	Init_Ar(&ar, row, col);
	int x = GetItem(&ar, 2, 3);

	return 0;
}
typedef int ElemType;  //引用(法二)
struct Array2
{
	ElemType* data;
	int row;
	int col;
};
void Init_Ar(struct Array2& par, int row, int col)
{

	par.row = row;
	par.col = col;
	par.data = (ElemType*)malloc(sizeof(ElemType) * row * col);
	if (par.data == nullptr) exit(1);

	for (int i = 0;i < row * col;++i)
	{
		par.data[i] = i + 1;
	}
}
ElemType GetItem(const struct Array2& par, int r, int c)
{
	assert(r < par.row || c < par.col);
	return par.data[r * par.col + c];
}
int main()
{
	Array2 ar;
	int row, col;
	scanf_s("%d %d", &row, &col);
	Init_Ar(ar.row.col);
	int x = ElemType GetItem();

	return 0;
}
void Init_2Ar(int** s, int row, int col)
{
	assert(s != nullptr);
	for (int i = 0;i < row;++i)
	{
		for (int j = 0;j < col;++j)
		{
			s[i][j] = i + j;
		}
	}
}
void Print_2Ar(int** s, int row, int col)
{
	assert(s != nullptr);
	for (int i = 0;i < row;++i)
	{
		for (int j = 0;j < col;++j)
		{
			printf("%5d", s[i][j]);  //*(*(s+i)+j)
		}
		printf("\n");
	}
	printf("\n");
}

int** Get2Array(int row, int col)
{
	int** s = (int**)malloc(sizeof(int*) * row);
	if (s == nullptr) exit(1);
	for (int i = 0;i < row;++i)
	{
		s[i] = (int*)malloc(sizeof(int*) * col);
		if (s == nullptr) exit(1);
	}
	return s;
}
void Free_2Ar(int** s, int row)
{
	assert(s != nullptr);
	for (int i = 0;i < row;++i)
	{
		free(s[i]);
	}
	free(s);
}

int main()
{
	int row = 0, col = 0;
	int** s = nullptr;
	scanf_s("%d %d", &row, &col);  // 3  4
	s = Get2Array(row, col);
	Init_2Ar(s, row, col);
	Print_2Ar(s, row, col);
	Free_2Ar(s, row);
	s = nullptr;
}
typedef int ElemType;
struct Array2
{
	ElemType* data;
	int row;
	int col;
};
void Init_Ar(struct Array2& par, int row, int col)  //引用
{
	par.row = row;
	par.col = col;
	par.data = (ElemType*)malloc(sizeof(ElemType) * row * col);
	if (par.data == nullptr) exit(1);
	for (int i = 0;i < row * col;++i)
	{
		par.data[i] = i + 1;
	}
}
ElemType GetItem(const struct Array2& par, int r, int c)
{
	assert(r < par.row || c < par.col);
	return par.data[r * par.col + c];
}
void SetItem(struct Array& par, int r, int c, ElemType val)
{
	assert(r < par.row && c < par.col);
	par.data[r * par.col + c] = val;
}
ElemType& Item(struct Array2& par, int r,int c)
{
	assert(r < par.row || c < par.col);
	return par.data[r * par.col + c];
}
int main()
{
	Array2 ar;
	int row, col;
	scanf_s("%d %d", &row, &col);
	Init_Ar(ar,row,col);
	for (int i = 0;i < row;++i)
	{
		for (int j = 0;j < col;++j)
		{
			Item(ar, i, j) = i + j;
		}
	}

	for (int i = 0;i < row;++i)
	{
		for (int j = 0;j < col;++j)
		{
			printf("%5d", Item(ar, i, j));
		}
		printf("\n");
	}
	printf("\n");

	Destory_Ar(ar);
	return 0;
}

typedef int ElemType; //开辟n维数组
struct Arran
{
	ElemType* data;
	int *index;
	int n;
};

柔型数组(数组大小待定)结构体的最后一个元素可以是大小待定的数组

//数组大小声明为0或者不给出大小,称之为柔型数组。
//(全局数组和局部数组不能这样定义)
struct sd_node
{
	int num;
	int size;
	char data[];  //柔型数组(数组大小待定)结构体的最后一个元素可以是大小待定的数组
};
//或
struct sd_node
{
	int num;
	int size;
	char data[0];
};
int main()
{
	int n = 5, m = 10;
	int i = 0;
	int* ip = (int*)malloc(sizeof(int) * n);
	for (i = 0;i < n;++i)
	{
		ip[i] = i + 10;
	}
	ip = (int*)realloc(ip, sizeof(int) * m);
	if (nullptr == ip) exit(1);
	for (i = 0;i < m;++i)
	{
		printf("%d", ip[i]);
	}
	printf("\n");
	free(ip);
	ip = nullptr;
	return 0;
}
int main()
{
	int i = 0;
	int n = 0;
	int* ip = nullptr;
	scanf_s("%d", &n);
	ip = (int*)malloc(sizeof(int) * n);
	if (nullptr == ip) exit(1);
	for (int i = 0;i < n;++i)
	{
		ip[i] = i + 10;
	}
	//使用ip指向的堆内存空间
	free(ip);  //空悬指针
	ip = nullptr;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值