当前位置:网站首页>C的内存管理
C的内存管理
2022-07-02 17:50:00 【悠然少年心】
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
typedef struct
{
int x;
int y;
}FTest;
int main() {
#if 0
char p[128];
strcpy(p,"tom");
printf("%s \n",p);
char* ptr = malloc(128*sizeof(char));//在堆栈分配一个空间
FTest*pName=calloc(10, sizeof(FTest));//分配十个内存并且全部为0
printf("%d \n\r", pName[4].x);
pName=realloc(pName,10+2*sizeof(FTest));//重新分配在后面添加两个FTest大小的内存
pName[12].x = 10;
printf("%d \n\r", pName[12].x);
if (ptr == NULL)
{
fprintf(strerror,"内存不够");
exit(EXIT_FAILURE);
}
else
{
strcpy(ptr,p);
}
free(ptr);
#endif // 0
//memset 和memcpy
char str[] = "IIII";
memset(str,'k', strlen(str));//参数:设置的数组,替换字符,替换前面几位
printf("%s \r\n", str);
//中文字符一个汉字占两个字节所以拷贝3个只能拷贝到第二个的一半
char dest[20] = "秋水连天";
char dest1[20] = {
0};
memcpy(dest1, dest,3);//秋?
printf("%s \r\n", dest1);
system("pause");
}
边栏推荐
- How to set vscode to delete the whole line shortcut key?
- R语言使用epiDisplay包的lsNoFunction函数列出当前空间中的所有对象、除了用户自定义的函数对象
- Which securities company has a low, safe and reliable online account opening commission
- R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的X轴标签信息
- ICDE 2023|TKDE Poster Session(CFP)
- 第一次去曼谷旅游怎么玩?这份省钱攻略请收好
- 学生抖音宣传母校被吐槽“招生减章”,网友:哈哈哈哈哈哈
- Leetcode (81) -- search rotation sort array II
- [100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
- Stretchdibits function
猜你喜欢
![27: Chapter 3: develop Passport Service: 10: [registration / login] interface: after the registration / login is OK, save the user session information (uid, utoken) to redis and cookies; (one main poi](/img/b9/2066a13b160252114c2881007094f8.png)
27: Chapter 3: develop Passport Service: 10: [registration / login] interface: after the registration / login is OK, save the user session information (uid, utoken) to redis and cookies; (one main poi

The text editor hopes to mark the wrong sentences in red, and the text editor uses markdown

Troubleshooting: kubectl reports an error validationerror: unknown field \u00a0
![[daily question] first day](/img/8c/f25cddb6ca86d44538c976fae13c6e.png)
[daily question] first day

新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島

仿京东放大镜效果(pink老师版)

Novice must see, click two buttons to switch to different content

距离度量 —— 杰卡德距离(Jaccard Distance)

MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format

彻底搞懂基于Open3D的点云处理教程!
随机推荐
Page title component
AI开发调试系列第二弹:多机分布式调测探索之旅
options should NOT have additional properties
性能测试如何创造业务价值
从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
Web实时通信技术之Websocket
Is it safe to buy funds on Alipay account
全链路数字化转型下,零售企业如何打开第二增长曲线
R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
Stretchdibits function
@Component 拿不到dao层
Redis (6) -- object and data structure
鸿蒙第四次学习
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
故障排查:kubectl报错ValidationError: unknown field \u00a0
拦截器与过滤器的区别
Responses of different people in technology companies to bugs | daily anecdotes
Novice must see, click two buttons to switch to different content
Which securities company has a low, safe and reliable online account opening commission
昨天阿里学长写了一个责任链模式,竟然出现了无数个bug