Hi:
I have the following problem: im trying to use std:vector, but im having some troubles.
I declare an estructure typedef struct{
float a;
float b;
int c;
}x;
and in my main i declare a std vector of the struct above:
vector<x> v;
Then i try to pass it as a reference to the next function:
void fun(char *f , int &c , vector<x> &y);
then in main i just call it: fun(f,c,v);
but it gives me a segmentation fault. Then i used ddd and the error printed was the following
Program received signal SIGSEGV, Segmentation fault.
0x0809b251 in std::__convert_to_v<float> ()
Current language: auto; currently asm
I have no idea what its going on here. I insist, im getting the mistake in the function call, not during the function.
Thanks for the help.
f is initialized but c no, just declared. but the problem is the vector std, cause i tried with a standar vector before,just like that and didnt complained. so...
thanks anyway. im still listening.
Post a complete example that demonstrates the problem. There is no way to know what is happening without seeing the code completely. What does the fun function do with the inputs? What does the main function do prior to calling fun? I do not see where you ever inserted data into the vector.
its simple. i get a file name from the main args, with a format:
float float int char*
i build the std::vector with the empty constructor cause i dont know the file size and i want to use only dinamic memory. then i just call the function wich reads from the text file and put it into vector. would be like this:
the .h file contains this: using std::vector;
typedef struct{ /*alamacenar ciudades leidas de fichero*/
float a;
float b;
int c;
char n[20];
}d;
void z(char *f ,int &e,vector<d> &f);
then the main file contains similar to this: #include "x.h"
using namespace std;
using std::vector;
int main(int argc, char *argv[]){
vector<d> g;
int h; z("p.txt",h,g);
return 0;
}
that would be all. the overflow is on the underline part of text. the error pinted by ddd is posted above. well if it is in any asistance im compiling using g++ standar with the usual options, -Wall -pedantic and the no so usual -static( its a requirement for the aplication). i think it may be an error on declaration or parameters, but i have no idea. if u need anything else let m know.
thanks
except for point 3 others are covered. first is typing mistake. my bad, sorry. im gonna remove using std::vector; to see what happens. i let you know. one question:
if im going to read from the file, is the same sintaxis that with static vectors? if the problem persist, ill post the entire code so you can have more data.
thanks.
im gonna remove using std::vector; to see what happens
Nothing is going to happen, I only pointed it out because its use is redundant.
if the problem persist, ill post the entire code so you can have more data.
I think this would be the best solution.
Just to be sure: Are you aware that char n[20] can hold only strings 19 characters long?
(sorry for this stupid question but sometimes one can overlook such minor bugs)
yes yes dont worry. im aware of it. i think that by tomorrow ill post the entire code if i dont find an answer. anyway, if it doesnt work ill have to implement something similar with list and overload [] operator. do you know where i can find some sourcecode so i dont have to do it all? (just in case, lets try to fix this problem). thanks