数据结构入门(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;

}

相关推荐
1000世界小札1 小时前
《大话数据结构》第9章精读:归并排序与快速排序完整 C++ 实现
数据结构·c++·算法
丰锋ff4 小时前
基于 Qt 的智能门禁系统(二)
开发语言·qt
fpcc4 小时前
跟我学C++中级篇——编译期的条件选择
开发语言·c++
SomeB1oody4 小时前
【RustyML入门】6.3. 并行归约
开发语言·后端·机器学习·rust·教程
mqiqe5 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南
java·开发语言·ui
lzhdim5 小时前
12、JavaScript常见的内存泄露问题 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
脉动数据行情5 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)
java·开发语言·twse·tpex·台股
爱学习的小邓同学5 小时前
Golang --- (1)第一个Golang程序
开发语言·后端·golang
zx_741484816 小时前
【Python 入门】面向对象基础:类、对象、成员变量与构造方法
开发语言·python
liangshanbo12158 小时前
虚拟列表深度面试题整理
java·开发语言·前端