简体   繁体   中英

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

Lets say i have an array A[10] and some other variables

And i am initializing it as

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

so when you run this , 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) ,它将初始化数组而没有任何错误,并且也可以进行访问,但是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. 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. You are invoking undefined behaviour . That being said prefer std::vector or std::array to raw (C-style) arrays in C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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