你应该知道,C语言是基于指针的
指针是指向内存里一个地址的
C语言程序在运行的时候,它直接往指针指向的地址赋值,去指针指向的地址取值,而不管这个地址到底是不是你的程序申请的
当然很多时候这样做会抛错误,因为系统不会允许一个进程随便访问另一个进程占据的内存块
malloc返回了一个内存地址,只是分配的大小为0.
如果分配大小为0,为什么还可以a->data=0;甚至可以打印出来?
malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return from malloc, even if the amount of memory requested is small.
其实电脑开机后物理内存的每个字节中都有值且都是可读写的,从来不会因为所谓的new、delete或malloc、free而被创建、销毁。区别仅在于操作系统内存管理模块在你读写时是否能发现并是否采取相应动作而已。操作系统管理内存的粒度不是字节而是页,一页通常为4KB。