当前位置:网站首页>关于指针、地址的大小的问题(以及malloc的用法)
关于指针、地址的大小的问题(以及malloc的用法)
2022-08-09 09:01:00 【Mike峰】
任何变量的指针大小都是一样大小的空间,例如在64位机中是4个字节,在32位机中是2个字节。
而我们分配一个地址,就比如
力扣145题
https://leetcode-cn.com/problems/binary-tree-postorder-traversal/solution/er-cha-shu-de-hou-xu-bian-li-by-leetcode-solution/
该题其中一句:
int *matrix = malloc(sizeof(int) * 2001);
给res这个指针分配了2001的int大小的空间,实际上就是给这里分配了可以装下2001个int类型变量的空间, 并且空间的首地址分配为res。

虽然分成了块,但是实际上空间是连续的,会意即可。
接下来,我们插入一个程序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char aa[10];
}Node;
int main()
{
Node* fat = (Node*)malloc(sizeof(Node*));
strcpy(fat->aa, "aaa");
printf("%s", fat->aa);
}
有什么问题呢,我们看到
Node* fat = (Node*)malloc(sizeof(Node*));
Node*表示分配的是Node*(也就是指针4字节)这么大的空间,
实际上我们需要的是Node这么大的空间分给fat这个指针。
边栏推荐
- js在for循环中按照顺序响应请求
- SQL server中的数据类型
- 大学四年不努力,出社会后浑浑噩噩深感无力,辞去工作,从头开始
- Dark Horse 2022 latest redis course notes and knowledge points (for interview)
- [漏洞复现]CVE-2018-12613(远程文件包含)
- Go语言技巧之正确高效使用slice(听课笔记总结--简单易懂)
- makefile的foreach、filter、filter-out函数
- 支付宝小程序禁止页面弹性下拉或上拉
- Module模块化编程的优点有哪些
- CPU主频 外频 芯片组 倍频 cache FSB PCI简介
猜你喜欢
随机推荐
SQL server中的数据类型
C#获取网卡地址
【LeetCode每日一题】——225.用队列实现栈
elder blind date
正则表达式基础介绍
【场景化解决方案】搭建数据桥梁,Dslink打通泛微系统连接流
leetcode 32. 最长有效括号 (困难)
The difference between big-endian and little-endian storage is easy to understand at a glance
XCTF College War "Epidemic" Network Security Sharing Competition Misc wp
ctf misc picture questions knowledge points
gin中模型中增删改查+搜索分页
The working principle of switch
uva11624 Fire! (双bfs)
无符号整数文法和浮点数文法
nyoj58 最少步数(DFS)
jdbctemplate connects to sql server, the data found in the code is inconsistent with the database, how to solve it?
【场景化解决方案】构建设备通讯录,制造业设备上钉实现设备高效管理
【培训课程专用】Secureboot
常用SQL server语句
困扰很久的问题。今天下午搞定了。









