# Passing a 2D Vector (of Structs) to a Function

Page 1 of 1

## 8 Replies - 2835 Views - Last Post: 06 May 2013 - 04:08 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=320419&amp;s=d0590c8f158852757941dcc1c6e35387&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 otm

Reputation: 0
• Posts: 27
• Joined: 18-April 10

# Passing a 2D Vector (of Structs) to a Function

Posted 06 May 2013 - 11:36 AM

Hi,

This is either a really simple problem with a simple answer, or a simple question with a massively complicated answer />

I am trying to pass a 2d vector to a function. The vector in question (line) is a Vector of a Vector of type Struct.

Its defined like this:

```//Struct definition

struct cache {
int valid;
int dirty;
int tag;
string cacheline[16];
cache():valid(0), dirty(0), tag(0) {}
};

//Vector Initialisation
vector<cache> page(Ways);
vector<vector<cache> > line(IndexLength, page);

```

where ways, and index length are user inputted variables. In my head, the only way I can visualise this is that similar to a Matrix, but where each Matrix element (a11 or a21 for example) is itself a struct with the cache data held within.

My issue comes when trying to pass the Vector Line to one of my functions...

my function prototype, function call, and relevant sections of the function are as follows:

```//Function Prototype

int Mode_Write (ostream& logoutput, const unsigned int AddressTag, const unsigned int AddressIndex, const unsigned int AddressOffset, const double Ways, vector <vector<cache> > line, vector<int> FIFO, const vector<cache> page, char data);

//Function Call

//Function

int Mode_Write (ostream& logoutput, const unsigned int AddressTag, const unsigned int AddressIndex, const unsigned int AddressOffset, const double Ways, vector <vector<cache> > line, vector<int> FIFO, const vector<cache> page, char *pdata)
{
int WriteLoop = 1, WriteHit = 0, WriteMiss = 0;

for (int i=0; i<page.size(); i++)
{
{
WriteHit ++;
logoutput << "A cache hit occurred" << endl;

{
//cout << "Data " << data << " was written to Index " << AddressIndex << " in way " << i << endl;
logoutput << "Way " << i << " is being accessed during this Write operation" << endl;
logoutput << "The index of the cache line: " << AddressIndex << endl;
logoutput << "A writeback was performed of the data " << line[AddressIndex][i].cacheline << " into main memory" << endl << endl;

WriteLoop = 0;
}
else
{
//cout << "Data " << data << " was written to Index " << AddressIndex << " in way " << i << endl;
logoutput << "Way " << i << " is being accessed during this Write operation" << endl;
logoutput << "The index of the cache line: " << AddressIndex << endl;
logoutput << "A writeback was not performed" << endl << endl;

WriteLoop = 0;
}
if (WriteLoop == 0) break;
}
}

if (WriteLoop != 0)
{

WriteMiss ++;
logoutput << "A cache miss occurred" << endl;

{

logoutput << "Way " << FIFO[AddressIndex] << " is being accessed during this Write operation" << endl;
logoutput << "The index of the cache line: " << AddressIndex << endl;
logoutput << "A writeback was performed of the data " << line[AddressIndex][FIFO[AddressIndex]].cacheline << " into main memory" << endl << endl;

{
}

WriteLoop = 0;
}
else
{
logoutput << "Way " << FIFO[AddressIndex] << " is being accessed during this Write operation" << endl;
logoutput << "The index of the cache line: " << AddressIndex << endl;
logoutput << "A writeback was not performed" << endl << endl;

{
}

}
}
// Return Value

}

```

I've been trying all sorts of different ways of passing the vector, I keep getting errors based around the same thing:

error C2664: 'Mode_Write' : cannot convert parameter 6 from 'std::vector<_Ty>' to 'std::vector<_Ty>'

with
[
_Ty=std::vector<cache>
]
and
[
_Ty=std::vector<int>
]

if I try to use * or & the error is pretty much the same.

I'm massively lost with this, any help would be appreciated.

Cheers,

Owen.

PS. Sorry if the code isn't clear, or not the best c++ but it functions (or at least it did before attempting to put things in functions)as required to simulate a cache.

Is This A Good Question/Topic? 0

## Replies To: Passing a 2D Vector (of Structs) to a Function

### #2 jimblumberg

Reputation: 5537
• Posts: 17,143
• Joined: 25-December 09

## Re: Passing a 2D Vector (of Structs) to a Function

Posted 06 May 2013 - 01:25 PM

Also in the following snippet:
```int Mode_Write (ostream& logoutput, const unsigned int AddressTag,
const double Ways, vector <vector<cache> > line, vector<int> FIFO,
const vector<cache> page, char *pdata)
```

There is really no need for the const qualifiers, you are passing these variables by value, which makes a copy of the data so making changes to the data in the function only affects the variables in the function, not the calling function.

But one question I do have is why are you passing copies of the vectors into the function and why are you passing pdata by pointer? You're passing line by value but yet you seem to be trying to change values in this vector, when your function returns these changes are lost.

Why are you passing page into the function, you only seem to be using it's size in the function, so why not just pass the size instead of the entire vector?

You may want to study the function tutorials contained in my signature.

Jim

### #3 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Passing a 2D Vector (of Structs) to a Function

Posted 06 May 2013 - 01:41 PM

An error usually gives a line number beside the filename, which would be useful.

You have a couple of lines where you try and print cacheline which is an array of string.

```   logoutput << "A writeback was performed of the data "
<< " into main memory" << endl << endl;

```

### #4 otm

Reputation: 0
• Posts: 27
• Joined: 18-April 10

## Re: Passing a 2D Vector (of Structs) to a Function

Posted 06 May 2013 - 02:06 PM

The code I'm working on is complicated to say the least (correction, complicated to me!). It would be difficult to post a complete code that is short, but I will endeavour to do so.

A sum up of the code, it reads data in from a .txt file. within the .txt file are lines of data that simulate a CPU taking to cache on an embedded system. The line in the trace file look like this <Mode> <Address> <Data>:

R 0x06E90FFC
W 0x06E90FFC 0xA5A5A5A5

Where R|W ditactes whether it is asked to read data at the given address, or to write the given data to the supplied address. A few extra variables are selected by the user at runtime, mainly the size of the cache itself, the size of the Main system Memory, the number of ways (1 assumes direct mapped, any other number means a set associative cache) and the cache Line Length (in words).

Our code, from the supplied information, extracts (through bitwise operations) the cache Tag, the Index and the Offset. this then gets fed into our vectors in essence simulating a cache. if the data is not there, it places the data there (changing the relevant flags, Dirty or Valid) from Main memory (or at least it would if we had a main memory storage with the data in it), if the data is there then it is passed to the CPU. if it wants to write, it checks if the data at the relevant cache line is valid/dirty and does the required evictions etc.

Long code

I tried to put the code back to a simple starting point by removing pointers and the like that I had been trying to use to fix the issues. as for page.size yes I could save time by parsing a variable that is page.size so I can edit that.

I don't really want to be using call by value as I want to change the data within the structs held within the vectors, but When I tried pointers I got the same kind of error but couldn't change between vector ty and vector *ty. as for pdata, Data is a character array that holds the data information (It is provided by the CPU trace file in Hex Form, hence we are using a character array to keep it that way, at least functionally). we need to, if the conditions are right, write the data held within the data array into the relevant cache line.

I was always told to add const anyway to make it clearer that the data wasn't being edited (assuming that if you developed the code it needed to be able to be read and understood by others quickly), even if you were doing call by value.

I appreciate its all a bit hashed up, and I'm trying to make the issues as clear as I can without posting 500 lines of code

The main question irrespective of what the code is meant to be doing is how would your properly pass a vector of type vector of structs to a function? assuming that I need to change the data within the struct inside the functions (in this case, the write function, but also likewise for the read function).

I hope this helps understand my issues, and where I need to progress. As for call by value, reference or address for the other data, I can work on that once I've succeeded with the main issue.

Hope this helps!!

Sorry to ask such a complicated question

Owen.

### #5 otm

Reputation: 0
• Posts: 27
• Joined: 18-April 10

## Re: Passing a 2D Vector (of Structs) to a Function

Posted 06 May 2013 - 02:13 PM

#define, on 06 May 2013 - 08:41 PM, said:

An error usually gives a line number beside the filename, which would be useful.

You have a couple of lines where you try and print cacheline which is an array of string.

```   logoutput << "A writeback was performed of the data "
<< " into main memory" << endl << endl;

```

The lines the complier picks up on are the function calls, and specifically, parameter 6 of the function call which is bit where I send Line which is a vector<vector<cache> >

as for .cacheline... .cacheline is a string, and we are displaying and storing the data (which is a hex number in reality) as a sting for easy reading etc. in the log file that this program outputs. cacheline is an array held within the struct so I can't see where the problem is with outputting it like that assuming I've sent the vector to the function correctly

Owen.

### #6 otm

Reputation: 0
• Posts: 27
• Joined: 18-April 10

## Re: Passing a 2D Vector (of Structs) to a Function

Posted 06 May 2013 - 02:34 PM

The other Errors which I've only just noticed, which might help relate to the function prototypes themselves.....

```int Mode_Read (ostream& logoutput, const unsigned int AddressTag, const unsigned int AddressIndex, const double Ways, vector <vector<cache> > line, vector<int> &FIFO, const int sizepage);
int Mode_Write (ostream& logoutput, const unsigned int AddressTag, const unsigned int AddressIndex, const unsigned int AddressOffset, const double Ways, vector <vector<cache> > line, vector<int> &FIFO, const int sizepage, char data);

```

error C2065: 'cache' : undeclared identifier
'std::vector' : 'cache' is not a valid template type argument for parameter '_Ty'
error C2065: 'cache' : undeclared identifier
error C2923: 'std::vector' : 'cache' is not a valid template type argument for parameter '_Ty'

I believe this might be because the vector line is made up of struct cache, but I'm not sure how to get around this? why doesn't it come up with the same error when I create the line vector using the same syntax?

Sorry,

Owen.

PS. The code has been altered to reflect the input from Jim regarding the Vector Page, I created a variable in Main that is a value relating to Page.size() thus not passing a whole vector simply for it's size value. Plus I called by reference the FIFO vector using the & allowing me to change the data in FIFO. Applying the same logic to the line vector simply changes the main error to :std::vector<_Ty>' to 'std::vector<_Ty> &' which is next to no different from the first one...

This post has been edited by otm: 06 May 2013 - 02:38 PM

### #7 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Passing a 2D Vector (of Structs) to a Function

Posted 06 May 2013 - 02:59 PM

error C2065: 'cache' : undeclared identifier

Is cache declared before the function?

### #8 otm

Reputation: 0
• Posts: 27
• Joined: 18-April 10

## Re: Passing a 2D Vector (of Structs) to a Function

Posted 06 May 2013 - 03:27 PM

#Define,

you probably wont believe this, or may you would......

Stupid me (kind of) placing the Struct definition Before the function prototypes fixed the issues.... why didn't I think of that??

Seems a little weird putting the function prototypes further down..... I now get an error relating to the char array 'data' but I fixed that by passing char* in the prototype.

This has fixed it..... I've tested the program, and all the cache stats match those of the same program without the functions so it works!

I feel a little stupid, but I've learnt something

Owen.

My new function prototypes and function calls and functions::

```//function prototypes
int Mode_Read (ostream& logoutput, const unsigned int AddressTag, const unsigned int AddressIndex, const double Ways, vector <vector<cache> > &line, vector<int> &FIFO, const int sizepage);
int Mode_Write (ostream& logoutput, const unsigned int AddressTag, const unsigned int AddressIndex, const unsigned int AddressOffset, const double Ways, vector <vector<cache> > &line, vector<int> &FIFO, const int sizepage, char * data);

//function calls
sizepage = page.size();

//functions

int Mode_Read (ostream& logoutput, const unsigned int AddressTag, const unsigned int AddressIndex, const double Ways, vector <vector<cache> > &line, vector<int> &FIFO, const int page)
{
//Code
}

int Mode_Write (ostream& logoutput, const unsigned int AddressTag, const unsigned int AddressIndex, const unsigned int AddressOffset, const double Ways, vector <vector<cache> > &line, vector<int> &FIFO, const int page, char* pdata)
{

//code

}

```

Any errors / bad C++ you can see from that?

### #9 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Passing a 2D Vector (of Structs) to a Function

Posted 06 May 2013 - 04:08 PM

That's good you figured out the problem, as usual, it's simple when you know.