problems with std::vector

Mar 18, 2010 at 2:58pm
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.
Mar 18, 2010 at 3:34pm
It seems to be OK although someone would prefer to name the struct instead of using typedef.

Are f and c initialized?
Mar 18, 2010 at 4:31pm
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.
Mar 18, 2010 at 7:25pm
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.
Last edited on Mar 18, 2010 at 7:43pm
Mar 19, 2010 at 12:47pm
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


Mar 19, 2010 at 4:11pm
Your code is still somewhat incomplete. So I can only guess.

1.
void z(char *f ,int &e,vector<d> &f);
Two arguments can't have the same name. Or is it only a typing error?

2. I presume x.h includes <vector> for it's not included in your .cpp file.

3. Why to use using std::vector; after using namespace std;?

4. Is char n[20] sufficiently long? There could be a possible overflow.

5. Is your .txt file located at the right spot?

Moreover I don't see z definition anywhere.
Mar 19, 2010 at 5:06pm
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.
Mar 20, 2010 at 11:36am
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)
Mar 20, 2010 at 6:03pm
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
Topic archived. No new replies allowed.