I am new to C . In the code below, I am probably doing something wrong, because in the terminal I get Process returned -1073741819 (0xC0000005) execution time : 1.533 s
main.cpp
#include <iostream>
#include "foo.h"
int main() {
Baz* quuz;
quuz->quux();
return 0;
}
foo.h
#include <vector>
class Bar {
public:
bool boolean_val;
};
class Baz {
private:
std::vector<Bar> qux;
public:
void quux();
};
foo.cpp
#include "foo.h"
#include <iostream>
void Baz::quux()
{
qux[0].boolean_val = true;
}
Could you please highlight what I am doing wrong?
CodePudding user response:
Baz* quuz;
quuz->quux();
Calling a function on an uninizialized pointer is no bueno.
void Baz::quux()
{
qux[0].boolean_val = true;
}
Follows uninitiliazed this
pointer to access qux
. Undefined behavior invoked. You're lucky to get a crash. 0xC0000005 is accessed memory that is not mapped.