do you know why this is calling segfault in qt?

M

Milan Krejci

int nejmensi;
for (int i=1;i<pole.size();i++) {
if (pole.at(i)<pole.at(i-1)) nejmensi=i;
}
return nejmensi;
 
V

Victor Bazarov

Milan said:
int nejmensi;
for (int i=1;i<pole.size();i++) {
if (pole.at(i)<pole.at(i-1)) nejmensi=i;
}
return nejmensi;

"Calling segfault"? As in "undefined behaviour"? <shrug> No,
we don't know. What is 'pole'? Read the FAQ 5.8 before you
proceed.

V
 
J

Jim Langston

Milan Krejci said:
int nejmensi;
for (int i=1;i<pole.size();i++) {
if (pole.at(i)<pole.at(i-1)) nejmensi=i;
}
return nejmensi;

Could be many reasons, need to see more code.

What happens when pole.size() is 0?
 
A

Alf P. Steinbach

* Milan Krejci:
int nejmensi;
for (int i=1;i<pole.size();i++) {
if (pole.at(i)<pole.at(i-1)) nejmensi=i;
}
return nejmensi;

As Jim Langston remarked else-thread, pole.size() 0 is not covered by
your logic.

In that case you're returning an indeterminate value, which could be
anything.

Most likely that indeterminate value causes the calling code to do
something Bad.
 
V

Victor Bazarov

Jim said:
Could be many reasons, need to see more code.

What happens when pole.size() is 0?

The 'for' loop is not going to be executed. 'nejmensi' is
not initialised. Even returning its value is undefined
behaviour, right?

V
 
J

Jim Langston

Victor Bazarov said:
The 'for' loop is not going to be executed. 'nejmensi' is
not initialised. Even returning its value is undefined
behaviour, right?

Exactly. And whatever used the value of nejmensi will be using an undefined
value. Which may be whats causing the error, we don't know. Need to see
more code. Need to know the actual line the error is occuring on, etc...

It may be the operator< for whatever is stored in pole. Need to see more
code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top