梦回连营 发表于 2021-10-20 16:33:50

C语言内存空间问题

如图所示,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 为零,则返回值取决于特定的库实现(它可能是也可能不是空指针),但不应取消引用返回的指针。

人造人 发表于 2021-10-20 16:41:39

字符常量默认会被自动转换成 int 类型

梦回连营 发表于 2021-10-20 16:45:27

人造人 发表于 2021-10-20 16:41
字符常量默认会被自动转换成 int 类型

明白了
页: [1]
查看完整版本: C语言内存空间问题