数据结构入门(C语言复习)malloc开辟free释放

/*void* malloc(size_t)

如果成功,会返回从堆内存上分配的内存指针

如果失败,会返回空指针*/

#include<stdio.h>

#include<stdlib.h>//malloc要用

#include<string.h>

typedef struct

{

int x;//如果是char x,内存对齐sizeof(po)仍然是8个字节

int y;

}po;

int main()

{

/*int *p;

p=(int*)malloc(sizeof(int));//开辟整型空间,强制转换int

*p=15;//给堆内存赋值

printf("%d\n",*p);

free(p);//释放内存空间*/

/*char *s;

s=(char*)malloc(10);//强制转换存字符

strcpy(s,"Hello");

printf("%s\n",s);*/

/*int i;

int*arr=(int*)malloc(5*sizeof(int));

for(i=0;i<5;i++)

{

arri=i;

}

for(i=0;i<5;i++)

{

printf("%d\n",arri);

}*/

po *p;

p=(po*)malloc(sizeof(po));

p->x=5;

p->y=10;

printf("%d\n",p->x);

printf("%d\n",p->y);

return 0;

}

相关推荐
爱写代码的小朋友2 小时前
从零开始学 Win32 API:C++ 窗口编程实战(VS Code + MinGW-w64 命令行详解)
开发语言·c++
果汁华2 小时前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
小小晓.2 小时前
C++:语句和作用域
开发语言·c++
wanderist.3 小时前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
海天鹰4 小时前
PHP上传文件
android·开发语言·php
稚南城才子,乌衣巷风流4 小时前
动态开点:原理、实现与应用场景
数据结构·算法
yyds_yyd_100865 小时前
1464. 数组中两元素的最大乘积(2026.07.27)
数据结构·c++·算法·leetcode
Yeauty6 小时前
渲染成图再 CLI 拼接,还是进程内直推?Rust 帧到视频的两条路
开发语言·rust·音视频
geovindu6 小时前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
库克克7 小时前
【C++】set 与multiset
开发语言·c++