using unallocated memory without error?

前端 未结 5 1567
礼貌的吻别
礼貌的吻别 2020-12-06 18:37

Why does that work?

#include 
using namespace std;

int main() {
    float* tab[3];

    int i = 0;
    while(i < 3) {
        tab[i] = ne         


        
5条回答
  •  独厮守ぢ
    2020-12-06 18:58

    Use of unallocated memory results in undefined behaviour. You can have no expectations of what will happen when you do this even on the same system and compiler, let alone across different combinations of hardware and compiler.

    The program might crash immediately, it might work for a while and then fail later, it might even appear to work perfectly.

    Accessing memory you don't own is always a programming error, though. Don't think of the appearance of correct operation as "it sometimes works", think of it as "I got really unlucky and my bug does not show up quickly".

提交回复
热议问题