C语言动态内存分配
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C语言动态内存分配,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3027字,纯文字阅读大概需要5分钟。
内容图文
C动态内存分配
数组是固定数量的值的集合,在声明数组的大小之后,无法更改。有时,数组大小可能不够,就需要动态扩容。解决此问题,可以在运行时手动分配内存。这在C编程中称为动态内存分配。
动态分配存储器涉及到的库函数有
-
malloc()
-
calloc()
-
realloc()
-
free()
这些函数在<stdlib.h>
头文件中定义。
1.malloc()
名称“ malloc”代表内存分配,memory allocation。
该malloc()
函数保留指定字节数的内存块。并且,它返回一个指针的void
可铸造成任何形式的指针。
malloc()的语法
ptr = (castType*) malloc(size);
例
ptr = (int*) malloc(100 * sizeof(float));
上面的语句分配了400个字节的内存。这是因为float
的大小为4个字节。并且,指针ptr保存分配的存储器中的第一个字节的内存地址。
如果无法分配内存,则表达式将产生一个NULL
指针。
2.calloc()
名称“ calloc”代表连续分配,contiguous allocation。
malloc()
函数分配内存,但不初始化内存。而calloc()
函数分配内存并将所有位初始化为零。
calloc()的语法
ptr = (castType*)calloc(n, size);
例:
ptr = (float*) calloc(25, sizeof(float));
上面的语句为float
类型的25个元素在内存中分配了连续的空间。
3.free()
使用calloc()
或malloc()
不单独释放创建的动态分配内存,必须明确使用free()
释放空间。
free()的语法
free(ptr);
该语句释放由指向的内存中分配的空间ptr
。
示例1: malloc()和free()
// Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
// deallocating the memory
free(ptr);
return 0;
}
在这里,我们已为n个数字动态分配了内存
示例2: calloc()和free()
// Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) calloc(n, sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
4.realloc()
如果动态分配的内存不足或超出要求,则可以使用该realloc()
功能更改以前分配的内存的大小。
realloc()的语法
ptr = realloc(ptr, x);
在这里,ptr以新的大小x重新分配。
示例3: realloc()
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf("%d", &n1);
ptr = (int*) malloc(n1 * sizeof(int));
printf("Addresses of previously allocated memory: ");
for(i = 0; i < n1; ++i)
printf("%u\n",ptr + i);
printf("\nEnter the new size: ");
scanf("%d", &n2);
// rellocating the memory
ptr = realloc(ptr, n2 * sizeof(int));
printf("Addresses of newly allocated memory: ");
for(i = 0; i < n2; ++i)
printf("%u\n", ptr + i);
free(ptr);
return 0;
}
运行该程序时,输出为:
输入大小:2
先前分配的内存的地址:26855472
26855476
输入新的尺寸:4
新分配的内存地址:26855472
26855476
26855480
26855484
总结
malloc动态分配内存,不初始化
int n, *ptr = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
如果无法分配内存,则返回NULL
calloc动态分配内存,初始化所有bit为0
int n, *ptr = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) calloc(n, sizeof(int));
如果无法分配内存,则返回NULL
free释放内存
free(ptr);
realloc重新分配内存
ptr = realloc(ptr, n * sizeof(int));
原文:https://www.cnblogs.com/ericling/p/11746972.html
内容总结
以上是互联网集市为您收集整理的C语言动态内存分配全部内容,希望文章能够帮你解决C语言动态内存分配所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。