数据结构复习之C语言malloc()动态分配内存概述
#include <stdio.h>
#include <malloc.h> int main(void)
{
int a[] = {, , , , };
// 计算数组元素个数
int len = sizeof(a)/sizeof(a[]);
int i; //printf("%d", len); // sizeof(int) int类型的字节数
// 动态分配内存
// malloc返回第一个字节的地址
int *pArr = (int *)malloc(sizeof(int) * len); for(i = ; i < len;i++)
scanf("%d", &pArr[i]); for(i = ; i < len;i++)
printf("%d\n", *(pArr + i)); free(pArr); // 把pArr所代表的动态分配的20个字节的内存释放 return ;
}
跨函数使用内存
函数内的局部变量,函数被调用完之后,变量内存就没有了。
如果是一个动态的变量,动态分配的内存必须通过free()进行释放,不然只有整个程序彻底结束的时候
才会释放。
跨函数使用内存实例:
#include <stdio.h>
#include <malloc.h> struct Student
{
int sid;
int age;
}; struct Student *CreateStudent(void); int main(void)
{
struct Student *ps; ps = CreateStudent();
ShowStudent(ps); // 输出函数 return ;
} struct Student *CreateStudent(void)
{
// sizeof(struct Student) 结构体定义的数据类型所占用的字节数
struct Student *p = (struct Student *)malloc(sizeof(struct Student)); // 创建
// 赋值
p->sid = ;
p->age = ;
return p;
}; void ShowStudent(struct Student *pst)
{
printf("%d %d\n", pst->sid, pst->age);
}
数据结构复习之C语言malloc()动态分配内存概述的更多相关文章
- 数据结构与算法基础之malloc()动态分配内存概述
动态内存分配和释放: 动态构造一维数组: 假设动态构造一个Int型数组: int *p = (int *)malloc(int len); //还可以写作: int *p = (int *)mallo ...
- C语言之动态分配内存
1. malloc()函数和free()函数 首先,我们应该知道.所有的程序都必须留出足够的内存空间来存储所使用的数据,所以我们常常会预先给程序开辟好内存空间,然后进行操作,但事实上另一种选择,能够让 ...
- malloc 动态分配内存
很久没有学习C了,复习下,有时候觉的C特别优美,学习算法和数据结构最佳选择. #include "stdafx.h" #include<stdlib.h> int ma ...
- 数据结构复习之C语言指针与结构体
数据结构指针复习: #include <stdio.h> void main() { ] = {, , , , }; // a[3] == *(3+a) printf(+a)); // a ...
- malloc的使用、用malloc动态分配内存以适应用户的需求的源代码实例
int len; ; printf("please enter the size that you want: "); scanf("%d", &len ...
- 复习C++_指针、动态分配内存
注意:++i指的是先计算i+1,然后将其赋给i cout<<str[7]<<endl; //输出a 注:交换失败 注意:delete释放之后,变为迷途指针. 注:n--> ...
- 数据结构基础(1)--数组C语言实现--动态内存分配
数据结构基础(1)--数组C语言实现--动态内存分配 基本思想:数组是最常用的数据结构,在内存中连续存储,可以静态初始化(int a[2]={1,2}),可以动态初始化 malloc(). 难点就是数 ...
- C语言中堆内存的开辟和释放与内存处理函数
C语言动态分配内存,malloc的出现就是来弥补静态内存分配的缺点 比如说我们在定义数组的时候,数组的长度必须是一个常量,不能改变的值,假如我事先定义了数组,一旦业务需求发生改变,那么这个数组就不能再 ...
- 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域
[源码下载] 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 动态分配内存 链 ...
随机推荐
- 使用过多的递归出现错误,“System.StackOverflowException”类型的未经处理的异常在 mscorlib.dll 中发生
class Program { static void Main(string[] args) { sub(0); } private static void sub(int count) { ...
- AtCoder - 2565 枚举+贪心
There is a bar of chocolate with a height of H blocks and a width of W blocks. Snuke is dividing thi ...
- jquery遇到的坑
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【转】Cannot add or update a child row: a foreign key constraint fails 解决办法
原因:设置的外键和对应的另一个表的主键值不匹配.解决方法:找出不匹配的值修改.或者清空两表数据. 转自https://blog.csdn.net/qq_29405421/article/details ...
- Permutation(构造+思维)
A permutation p is an ordered group of numbers p1, p2, ..., pn, consisting of ndistinct positi ...
- git 下载 安装
1.下载Git,官网地址:https://git-scm.com/,进入官网首页 在右下方的显示器中找到最新的版本下载,点击下载,跳转到下载页面 下载完成 2.安装Git 双击刚刚下载完成的安装文件, ...
- 通过 flume 上传数据到hive
目标: 通过接受 1084端口的http请求信息, 存储到 hive数据库中, osgi为hive中创建的数据库名称 periodic_report6 为创建的数据表, flume配置如下: a1.s ...
- [转] Linux命令——timeout
[From] https://blog.csdn.net/xiaqunfeng123/article/details/54315390 Linux命令——timeout 命令简介 运行指定的命令,如果 ...
- Oracle PL/SQL之GROUP BY GROUPING SETS
[转自] http://blog.csdn.net/t0nsha/article/details/6538838 使用GROUP BY GROUPING SETS相当于把需要GROUP的集合用UNIO ...
- pyquery的简单操作
一.初始化 1.html初始化 html = ''' <div> <ul> <li class="item-0">first item</ ...