简体   繁体   English

为什么我可以写一个已分配0空间的内存?

[英]why can I write over a piece of memory which has been allocated 0 space?

Why is it that I allocate a space of size 0 to array but i can still write over that piece of memory? 为什么我将大小为0的空间分配给数组但我仍然可以写入那段内存?

#include<stdio.h>

int main(int argc, char** argv)
{
   int * array = malloc((sizeof(int)) * 0);
   int i;
   for(i = 0; i < 10; i++)
      array[i] = i;

   for(i = 0; i < 10; i++)
      printf("%d ", array[i]);
}

You code invokes undefined behaviour as you access index out of bounds - 您访问索引越界时代码调用未定义的行为 -

 for(i = 0; i < 10; i++)
 array[i] = i;

You won't get any warning or error about such thing but this is documented in standards that it is UB . 您不会收到有关此类事情的任何警告或错误,但这是在UB的标准中记录的。

And in that case output could be anything. 在这种情况下,输出可以是任何东西。

And for this line - 而对于这一行 -

int * array = malloc((sizeof(int)) * 0);

C Standard says - C标准说 -

If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object. 如果请求的空间大小为零,则行为是实现定义的:返回空指针,或者行为就像大小是非零值一样,但返回的指针不应用于访问对象。

Here it's return may or may not be NULL pointer . 这里它的返回可能是也可能不是NULL pointer But it is clear that this pointer should not be used to access any object. 但很明显,这个指针不应该用于访问任何对象。

malloc with an argument of 0 returns either NULL or a unique pointer that can be passed to free . 参数为0 malloc返回NULL或可以传递给free的唯一指针。 If it does return a non-null value and that pointer points to memory that's within a page that's valid for your program and writable, the operating system won't zap you if you try to write to it, but you might end up rewriting some parts of your program's data (=> undefined behavior). 如果它确实返回一个非null值,并且该指针指向一个对你的程序有效并且可写的页面内的内存,那么如果你试图写入它,操作系统将不会摧毁你,但你最终可能会重写一些程序数据的一部分(=>未定义的行为)。

C is an unsafe language . C是一种不安全的语言 as an unsafe language it allows you to do risky actions as the following: 作为一种不安全的语言,它允许您执行以下有风险的操作:

  • Writing/Reading to/from unallocated memory. 写入/读取未分配的内存。
  • Implicity casting types ( void* to other types and vica versa). 外观铸造类型( void*为其他类型,反之亦然)。
  • No bound checks. 没有绑定检查。
  • Memory is Unmanaged . 内存Unmanaged
  • Etc... 等等...

Since those actions are risky and might lead to Memory Leak , Memory Corruption and other unwanted results, you should avoid doing such things when they are not neccesary and keep clean and conventioned code (make this 10 a constant and avoid working on unallocated memory ). 由于这些操作存在风险并且可能导致Memory LeakMemory Corruption和其他不需要的结果,因此您应该避免在不需要时执行此类操作并保持干净和常规代码(使此10 保持不变并避免处理未分配的内存 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我没有分配空间时,为什么可以读写内存? - Why can I write and read memory when I haven't allocated space? 如何释放一个函数从另一个函数动态分配的内存? - How can I free memory that has been dynamically allocated by one function from another function? 释放已分配给char指针(字符串)数组的内存。 我需要释放每个字符串还是仅释放“主”指针? - Freeing memory which has been allocated to an array of char pointers (strings). Do I have to free each string or just the “main” pointer? 为什么我不能在我用 calloc 创建的分配内存中写入? - Why am I not able to write in the allocated memory that I created with calloc? Malloc - &gt;分配了多少内存? - Malloc -> how much memory has been allocated? Pipe 的写入覆盖了分配的空间 memory - Pipe's write overwrites an allocated space of memory 为什么我不能以这种方式释放分配的内存? - Why can't I free the allocated memory this way? 如何调试为什么未分配内存或未存储分配结果? - How can I debug why memory is not allocated or the result of the allocation is not stored? 如何将已动态分配的字符串复制到已动态分配的另一个字符串? - How do i copy a string that has been dynamically allocated to another string that has been dynamically allocated? C:如何检查是否已写入 memory 地址? - C: How can I check if a memory address has been written?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM