vector of vectors into a struct

Hello everybody.
I'm running a bit short of ideas and an help would be really appreciated. I have looked a bit in previous topics and haven't found anything similar to my problem.
So...here it is:
I have in a header called LightpathDB.h the following structure declared into the class LightpathDB:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct cell {
		int source;
		int destination;
		int nr_paths;
		int nr_clockwise;
		int nr_cntclockwise;
		int shortest; /* 1-> clockwise
						 2-> counterclockwise */
		int* connectionMatrix;
		LINK_COST* costMatrix;

		//vector of vectors of int

		std::vector<vector<int>> vVectorFirstFs;
		std::vector<vector<int>> vVectorLpId;

		void clearMatrix();
	};


as you can see, I have two vector of vectors of int: vVectorFirstFs and vVectorLpId.
I have to manage them in a very dynamic way. I don t know at the beginning how many of this structs cell I have to create: I don't know how many elements are in both vectors (if we compare the vector of vectors to a matrix, I don't knwo how many rows and how many columns).

At the beginning of my program, I call this function: (here the code, only the important lines)
1
2
3
4
5
6
7
8
9
10
11
12
13
struct LightpathDB::cell** LightpathDB::initializeGrooming(int nodes)
{
struct cell **data=NULL;

	data = (struct cell **)realloc(data, (nodes*nodes) * sizeof(struct cell*));

	for(i=0;i<nodes*nodes;i++)
		data[i] = (struct cell *)malloc(sizeof(struct cell));

	for (i=0; i<nodes; i++)
	{
		for(j=0; j<nodes; j++)
		{


When I here try to add anything in any way to the two vectors vVectorFirstFs and vVectorLpId I get the error:

Access violation reading location
0xcdcdcdc1

Before I was getting an error like if i was trying to acces something beyond the boundaries of ther vector...
any hint on how can I manage this vector of vectors within a struct?

Thanks a lot

Francesco
I forgot:
when I try to add, I add like this for instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   vector<int> p;
    p.push_back(1);
    p.push_back(2);
    p.push_back(3);

    vector<int> q;
    q.push_back(10);
    q.push_back(20);
    q.push_back(30);
    q.push_back(40);

	
	(*data[i*nodes+j]).vVectorFirstFs.push_back(p);
	(*data[i*nodes+j]).vVectorFirstFs.push_back(q);
because you use C++, you should use 'new' and 'delete' operators instead of 'malloc' , 'realloc' and 'free'.
For the number of elements in the vectors, there is the 'size()' function of 'vector'.
Last edited on
I was just guessing my mistake was there.
How would you define it then?
1
2
3
4
5
6
7
	cell **data=NULL;

	data = new cell* [nodes*nodes];
	for(i=0; i<nodes*nodes; i++)
	{
		data[i]= new cell;
	}

for vectors of vectors... please leave a space (see below)

vector< vector<int> > myVector;
In C++11, space is not needed.
closed account (zb0S216C)
Vins3Xtreme wrote:
"you should use 'new' and 'delete' operators instead of 'malloc' , 'realloc' and 'free'."

This is not entirely true. std::malloc() and new could reserve memory from different locations. Either can be used. Of course, std::malloc() is not type-safe.

Edit:

Differences between new and malloc():

Failure:
- new throws an exception on failure unless std::nothrow is specified.
- malloc() returns NULL on failure.

Invocation:
- new requires the amount of objects to allocate, not the size. new does have an implicit parameter for the size, but it's not required explicitly.
- malloc() requires the amount of objects & the size of each.

Allocation:
- new invokes the default constructor of each allocated object implicitly.
- malloc() returns uninitialised objects.

While malloc() is available for use, new performs just as well, but it's safer. It's good that malloc() returns NULL of failure, but exceptions are something that you cannot ignore.

Wazzak
Last edited on
@framework:absolutely, the way people keep pointing it out, its not wrong to use c functions in c++, but just u have better alternatives in c++ library.
Topic archived. No new replies allowed.