Phuoc Nguyen
Phuoc Nguyen

Reputation: 37

Why don't visual studio code throw error when handle with null value?

I have the code below:

class A
{
public:
    int *b;
};
int main()
{
    A *a = new A();
    a->b = new int(1);
    cout << *a->b;
    cout << "done";
    return 0;
}

I run it and get the expected output 1done but when I comment the a->b = new int(1); It just print nothing. How to make it throw something for me to identify where the error is in my code. My VS Code version is 1.60.1.

Thanks in advance.

Upvotes: 0

Views: 613

Answers (1)

zedfoxus
zedfoxus

Reputation: 37099

It is not the fault of Visual Studio Code. Your code will have undefined behavior (it might throw a segmentation fault, or it might not) if you comment out a->b = new int(3) line. See I get a segmentation fault instead of an exception about how you cannot throw an exception (which VS Code can show) when a segmentation fault is happening.

You could go to the terminal of VS Code and then run:

g++ yourfile.cpp
./a.out

That showed me a segmentation fault like so:

./a.out
Segmentation fault (core dumped)

Upvotes: 1

Related Questions