当前位置:网站首页>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");
}
边栏推荐
- Introduction to sap s/4hana OData mock service
- Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
- 新手必看,点击两个按钮切换至不同的内容
- 彻底搞懂基于Open3D的点云处理教程!
- promise 和 Observable 的区别
- LightGroupButton* sender = static_cast<LightGroupButton*>(QObject::sender());
- 从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
- 【JVM调优实战100例】03——JVM堆调优四例
- R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
- The difference between promise and observable
猜你喜欢

LightGroupButton* sender = static_cast<LightGroupButton*>(QObject::sender());

材质UV遮罩的技巧
![[daily question] the next day](/img/8a/18329bd9b4a3a4445c8fbbc1ce562b.png)
[daily question] the next day

Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour

Hongmeng's fourth learning

UML 类图

Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent

Responses of different people in technology companies to bugs | daily anecdotes

Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比

Hospital online inquiry source code hospital video inquiry source code hospital applet source code
随机推荐
深度学习数学基础
Introduction to sap s/4hana OData mock service
【JVM调优实战100例】03——JVM堆调优四例
reduce--遍历元素计算 具体的计算公式需要传入 结合BigDecimal
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
Thoroughly understand the point cloud processing tutorial based on open3d!
故障排查:kubectl报错ValidationError: unknown field \u00a0
Redis (7) -- database and expiration key
距离度量 —— 杰卡德距离(Jaccard Distance)
The second bullet of AI development and debugging series: the exploration journey of multi machine distributed debugging
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
新加坡暑假旅游攻略:一天玩转新加坡圣淘沙岛
Troubleshooting: kubectl reports an error validationerror: unknown field \u00a0
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
promise 和 Observable 的区别
Progress-进度条
R语言使用epiDisplay包的lrtest函数对多个glm模型(logisti回归)执行似然比检验(Likelihood ratio test)对比两个模型的性能是否有差异、广义线性模型的似然比检
options should NOT have additional properties
How to copy and paste interlaced in Excel