Troubles with vector

Hello all,

I have defined the following data structure

1
2
3
4
5
6
struct volSurfData
{
	vector<double> maturities;
	vector<double> strikes;
	vector<vector<double>> volatilities;
};


I would like to use pointer to a vector of the data structure in one of my function.

1
2
3
4
5
6
7
void myVolSurf::readVolSurfData(string volSurfFile, myGlobSet * set, volSurfDef * def, vector<volSurfData> * vol)
{
volSurfData vol_aux;
// some code that loads value for vol_aux from txt file - works fine
(*vol).push_back(vol_aux); // HERE I get a programm execution break

}


The code is compiled without problems. However when I run it, some embeded vector code is generated and program breaks on

1
2
3
4
size_type size() const
{	// return length of sequence
   return (_Mylast - _Myfirst);
}


Do you have any idea how to get around?

Thx,

Macky
What is vol pointing to?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class myVolSurf
{
	myGlobSet * set;
	volSurfDef * def;
	vector<volSurfData> * vol;

public:

	// function declarations
	void readVolSurfDef(string curvesFile, volSurfDef * def);
	void readVolSurfData(string volSurfFile, myGlobSet * set, volSurfDef * def, vector<volSurfData> * vol);

	// object constructors
	myVolSurf(myGlobSet * _set)
	{
		// read volatilitiy surfaces from file
		set = _set;
		def = new volSurfDef;
                              readVolSurfDef("volSurf", def);
		readVolSurfData("volSurf", set, def, vol);
	}
}
The obvious thing to check is whether vol is actually pointing to a legitimate vector. The easiest way to do this is to step through using a debugger.
I have added

 
vol = new vector<volSurfData>;


after
 
def = new volSurfDef;


But it did not help at all...
Well, it certainly would fail if you hadn't allocated any memory for the vector.

Are you also allocating space for whatever the set argument is pointing to? Do you use that pointer at all in readVolSurfData? Remember that, just because you declare a pointer, C++ doesn't magically allocate memory for the object being pointed to. You still have to create the actual object, one way or another.

(Edited because I didn't read your code properly the first time)
Last edited on
Why not have the class hold a vector object rather then a pointer? It also doesn't seem necessary to pass the private members to the public method of the same class.
Hello all,

set is a function parameter and defined / initialized elsewhere. However def is treated in a similar manner without any problems. Anyway, the problem is definitely with a declaration / initialization as pointed out. As suggested by naraku9333, replacing

1
2
3
vector<volSurfData> * vol;
vol = new vector<volSurfData>;
readVolSurfData("volSurf", set, def, vol);


with

1
2
vector<volSurfData> vol;
readVolSurfData("volSurf", set, def, &vol);


solved the problem without touching function readVolSurfData(). So, the problem seems to be solved (I will check once more tomorrow when back at work). However, I still wonder how it should have been using pointer. How should have I allocated a memory when I do not know the object size at its initialization?

Thx,

Macky
OK - no suggestion, so I close the thread as solved :).
Topic archived. No new replies allowed.