简体   繁体   English

核心转储,但gdb无法找到确切位置

[英]Core Dumps but gdb is not able its find the exact location

Lets say i have an array A[10] and some other variables 可以说我有一个数组A [10]和其他一些变量

And i am initializing it as 我正在将其初始化为

for(int i=0;i<20;i++) //intentionally 20
    A[i]=0;

so when you run this LIBRARY , it will initialize the array without any error and access is also possible but the gdb gives core dump on some other place (In my case, it was showing memory for other variable as changed) 因此,当您运行此LIBRARY时 ,它将初始化数组而没有任何错误,并且也可以进行访问,但是gdb在其他地方提供了核心转储(在我的情况下,它显示了其他变量的内存已更改)
Why it is not giving core dump during initialization of array? 为什么在数组初始化期间没有给出核心转储?

IN c/c++ you have no protection for running out of bounds of the array, the crash happens as soon as you access memory that does not belong to the process. 在c / c ++中,您无法保护超出数组范围,一旦访问不属于该进程的内存,就会发生崩溃。 so as long as you go out of bounds and only write over your own memory on stack or in heap like other variables... the program will not crash, but the other variables will get changed, and if you change a pointer by this this might cause a future crash since the pointer then will point at some random memory address 因此只要您越界并且仅像其他变量一样在堆栈或堆中写出自己的内存...程序将不会崩溃,但是其他变量将被更改,并且如果您以此更改指针可能会导致将来崩溃,因为指针随后将指向某个随机内存地址

There is no out of bounds error checking for PODs such as raw arrays during compile time, only during run time. 在编译期间,仅在运行期间,不会对POD(例如原始数组)进行越界错误检查。 You are invoking undefined behaviour . 您正在调用未定义的行为 That being said prefer std::vector or std::array to raw (C-style) arrays in C++. 话虽这么说,但在C ++中,首选std :: vectorstd :: array而不是原始(C风格)数组。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM