鱼C论坛

 找回密码
 立即注册
查看: 926|回复: 3

[已解决]C语言内存空间问题

[复制链接]
发表于 2021-10-20 16:33:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如图所示,char的存储大小是1个字节,为什么'a'的存储大小又是4个字节了呢?
为什么我用malloc分配0的内存空间,也还是可以正常存储数据呢?
最佳答案
2021-10-20 16:36:47
void* malloc (size_t size);
Allocate memory block
Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.

The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.

If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced.
如果 size 为零,则返回值取决于特定的库实现(它可能是也可能不是空指针),但不应取消引用返回的指针。
字符内存大小问题.png
内存分配问题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-20 16:36:47 | 显示全部楼层    本楼为最佳答案   
void* malloc (size_t size);
Allocate memory block
Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.

The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.

If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced.
如果 size 为零,则返回值取决于特定的库实现(它可能是也可能不是空指针),但不应取消引用返回的指针。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-20 16:41:39 | 显示全部楼层
字符常量默认会被自动转换成 int 类型
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-20 16:45:27 | 显示全部楼层
人造人 发表于 2021-10-20 16:41
字符常量默认会被自动转换成 int 类型

明白了
Quicker_20211020_164402.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 17:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表