当前位置:网站首页>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");
}
边栏推荐
- @Component cannot get Dao layer
- 学生抖音宣传母校被吐槽“招生减章”,网友:哈哈哈哈哈哈
- Installation of thingsboard, an open source IOT platform
- #gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
- STM32G0 USB DFU 升级校验出错-2
- promise 和 Observable 的区别
- 从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
- How to write controller layer code gracefully?
- sql训练2
- R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
猜你喜欢
距离度量 —— 杰卡德距离(Jaccard Distance)
Stm32g0 USB DFU upgrade verification error -2
新手必看,点击两个按钮切换至不同的内容
Ali was wildly asked by the interviewer on three sides. Redis dared not write 'proficient' on his resume anymore
A simple PHP personal card issuing program v4.0
[daily question] first day
Exness in-depth good article: dynamic series - Case Analysis of gold liquidity (V)
Hongmeng's fourth learning
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference
聊聊电商系统中红包活动设计
随机推荐
材质UV遮罩的技巧
在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
Imitation Jingdong magnifying glass effect (pink teacher version)
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
[100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
全链路数字化转型下,零售企业如何打开第二增长曲线
页面标题组件
The difference between interceptor and filter
鸿蒙第四次学习
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
M2DGR:多源多场景 地面机器人SLAM数据集(ICRA 2022 )
Competence of product manager
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
Progress-进度条
Stm32g0 USB DFU upgrade verification error -2
R语言使用epiDisplay包的lsNoFunction函数列出当前空间中的所有对象、除了用户自定义的函数对象
A simple PHP personal card issuing program v4.0
Industrial software lecture - core technology analysis of 3D CAD design software - the second lecture of the Forum
从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式