PDA

View Full Version : vector causing system error/crash



babygal
11th October 2010, 11:12
I am using vector in my code but when application is running it causes system error/crash.
Code in .h file :

#include <vector>
using namespace std;

bool callVector(vector<float>&);
void func();

vector <float> vectorname;

Code in .cpp file:

void myClass::func()
{
if(!callVector(vectorname))
{
qDebug() << " vector error" ;
}
else
{
qDebug()<<"vector success" ;
qDebug()<<"vector [0]" : << vectorname[0]; //**system error/crash HERE **
}
}
System error/crash at line 10 in .cpp.
Application output:

vector success
.exe exited with code -1073741819

ChrisW67
11th October 2010, 12:00
Do you ever add anything to the vector?

Zlatomir
11th October 2010, 12:06
This line:

vector <float> vectorname;
Creates an empty vector of floats (and you try to access the first of them)
If you create empty vector you can use the push_back(...) member function to add elements to that vector

Or use it like this:

vector <float> vectorname(10); //this construct a vector of floats with 10 elements
Some documentation for std::vector (http://www.cppreference.com/wiki/stl/vector/start)
Note that the operator[] doesn't check if the number you write there is in vectorname range, you have to make sure that you "stay" in range (else you get an error), you have the at() member function that checks that.

Or you can use QVector instead, documentation for QVector (http://doc.qt.nokia.com/4.6/qvector.html) or QList (http://doc.trolltech.com/4.6/qlist.html)

babygal
11th October 2010, 12:09
Do you ever add anything to the vector?
Yes. Contents are added into the vector container. The size is supposed to be around 2000 . But what I get , the size amounts to around 50 000 000 .

Zlatomir
11th October 2010, 12:20
Then show us the code where you add elements to the vector, and also the error you get might be useful (if you run the debug build you might get a "nicer" error message)

babygal
13th October 2010, 05:32
using push_back(.....)

ChrisW67
14th October 2010, 00:00
In the absence of your actual code this is what people assume you are doing:


#include <iostream>
#include <vector>
using namespace std;

bool callVector(vector<float>& v)
{
v.push_back(1.2);
v.push_back(2.3);
return true;
}

int main(int argc, char *argv[])
{
vector<float> vectorname;
cout << vectorname.size() << endl;

// The following line is probably what you are doing
// There is no element 0 and ranges are not checked
// cout << "vector [0]\t" << vectorname[0]; //**system error/crash HERE **

bool res = callVector(vectorname);
cout << res << "\t" << vectorname.size() << endl;

// This works now the vector has something in it
cout << "vector [0]\t" << vectorname[0];
}


Are you returning true from your callVector() routine when you should not perhaps?

babygal
14th October 2010, 12:04
The code :
.h


#include <vector>
using namespace std;

bool callVector(vector<float>&);
void func();

vector <float> vectorname;
QString filename = "vector.txt";
QFile file(filename);
QString val;
float v;
.cpp


void myClass::func(){
if(!callVector(vectorname))
{
qDebug() << " vector error" ;
}
else
{
qDebug()<<"vector success" ;
}
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
for ( int i =0; i<vectorname.size() ; i++)
{
v = vectorname.at(i); //system crash here
val = QString::number(v);
qDebug()<< "val" << val;
char *valtoAsciidata = val.toAscii().data();
file.write(valtoAsciidata);


}
qDebug()<<"front =" << vectorname.front();
qDebug()<<"back =" << vectorname.back();
qDebug()<< "vector write finish!:" << "success";
file.close();


}
else
{
qDebug()<<"error write vector.txt file";
}
}

The system crash with error as below when debugging:

<Signal received>
The inferior stopped because it received a signal from the Operating System
Signal name : SIGSEGV
Signal meaning : Segmentation fault

The callVector is an API and it is called if dll is loaded. The code is in another program and the code is currently not transparent to me.

The qDebug() returns "vector success" message when callVector is called in my program.

ChrisW67
14th October 2010, 23:14
Are you absolutely sure that the callVector function is expecting a std::vector and not just space for a a plain old array of floats (i.e a float*)? Is vectorname.size() sensible when callVector returns? When the seg fault occurs what is the value of i?

If you have the API prototype correct then you need to speak to the author or provider of the DLL.

babygal
21st October 2010, 08:48
Q: "When the seg fault occurs what is the value of i?"
A : When the segmentation fault occurs, the value of i is always 0.