Milos9304
Milos9304

Reputation: 3

C++ vector.push_back crashes after pointer definition

my C++ compiler behaves very weird.

My vector is defined globally:

vector<int> values;

This does not cause any error:

void locate( int r, int s, bool newCheck, int from = 0 ){

static int A = 255;
static int U = 1;
static int D = 2;
static int R = 3;
static int L = 4;

values.push_back(0);

kocka *temp;
*temp = pole[r][s];    

values.push_back(1);

.........................

But, this causes an error Segmentation Fault:

void locate( int r, int s, bool newCheck, int from = 0 ){

static int A = 255;
static int U = 1;
static int D = 2;
static int R = 3;
static int L = 4;

//values.push_back(0);

kocka *temp;
*temp = pole[r][s];    

values.push_back(1);

...........................

Compiled with g++ -O2 -std=c++11 Do you have any idea why it might be behaving this strange way? Thx

Upvotes: 0

Views: 651

Answers (1)

chwarr
chwarr

Reputation: 7202

temp is never initialized to anything, so when you assign to it via *temp = pole[r][s], you get undefined behavior, as your trying to write to some random memory location. Initialize temp to a sensible value.

Upvotes: 6

Related Questions