如果什么计数器超过由malloc或释放calloc分配的内存

如果什么计数器超过由malloc或释放calloc分配的内存

本文介绍了如果什么计数器超过由malloc或释放calloc分配的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用用C以下命令来分配80个字节(在64位系统)至d。

I use following command in C to allocates 80 bytes (in a 64bit system) to d.

double *d = calloc(10, sizeof(double));

和使用下面的循环来初始化ð

And using following loop to initialize d

for (k=0;k<11;k++){
d[k] = k;
}

当我运行程序,没有错误。但我既然对 K 上限手感 11 ,应该有什么不对的,d是长度为10的数组。
请让我知道为什么程序没有错误执行。
先谢谢了。

When I run the program, there is no error. but I feel since the upper limit on k is 11, there should be something wrong as d is array of length 10.Please let me know why the program is executed with no error.Thanks in advance.

推荐答案

这是。有可能是一个错误,它可能由操作系统被忽略,当你打破规则 - 所有的赌注都关闭

This is undefined behavior. There might be an error, and it might be silently ignored by the OS, when you break the rules - all bets are off.

什么是真正发生在$ C $,c取决于操作系统,编译器和运行它的架构,这可能会容忍这种违规行为,崩溃或做别的事情上,关键是 - 所产生的行为是不确定的

What actually happens in the code depends on the OS, the compiler and the architecture you run it on, which might be tolerant to this violation, crash or do something else, the point is - the resulting behavior is undefined.

这篇关于如果什么计数器超过由malloc或释放calloc分配的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:50