当前位置:网站首页>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");
}
边栏推荐
- Crypto usage in nodejs
- Singapore summer tourism strategy: play Singapore Sentosa Island in one day
- ORA-01455: converting column overflows integer datatype
- promise 和 Observable 的区别
- 学生抖音宣传母校被吐槽“招生减章”,网友:哈哈哈哈哈哈
- SLC、MLC、TLC 和 QLC NAND SSD 之间的区别:哪个更好?
- Deep learning mathematics foundation
- Distance measurement - Jaccard distance
- CDN acceleration and breaking J anti-theft chain function
- 如何清理废弃pv和其对应的文件夹
猜你喜欢
鸿蒙第四次学习
yolov3 训练自己的数据集之生成train.txt
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
Kubernetes three open interfaces first sight
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference
[fluent] dart data type (VaR data type | object data type)
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
电商系统中常见的 9 大坑,你踩过没?
新手必看,点击两个按钮切换至不同的内容
仿京东放大镜效果(pink老师版)
随机推荐
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
Leetcode (81) -- search rotation sort array II
What is cloud primordial? This time, I can finally understand!
UML 类图
Meta universe chain game system development (logic development) - chain game system development (detailed analysis)
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
SQL training 2
How can retail enterprises open the second growth curve under the full link digital transformation
IPtable port redirection masquerade[easy to understand]
Web version 3D visualization tool, 97 things programmers should know, AI frontier paper | information daily # 2022.07.01
MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
学生抖音宣传母校被吐槽“招生减章”,网友:哈哈哈哈哈哈
R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
在Tensorflow2中使用mnist_784数据集进行手写数字识别
@Component cannot get Dao layer
第一次去曼谷旅游怎么玩?这份省钱攻略请收好
ORA-01455: converting column overflows integer datatype
新加坡暑假旅游攻略:一天玩转新加坡圣淘沙岛
性能测试如何创造业务价值