mercredi 30 octobre 2019

Undefined behaviour during compile time and run time

I still can't pinpoint exactly on which level undefined behaviour is actually defined. Assume the following code:

int i = value;
unsigned int x = static_cast<unsigned int>(i);

This is valid C++ code and defined if e.g. i is 1. But on i = -1 it becomes undefined behaviour, so the application is in UB state during runtime.

In the code smell below UB is already apparent during compile time. So my question is, is it correct that UB can be either during compile time or runtime? What is the right terminology here?

void foo(int* p)
{
    int v = *p;
    if (p == nullptr)
        return;
}

Aucun commentaire:

Enregistrer un commentaire