I am studying the vector array with my assignment, and with this assignment I have some question that is about the two dimension vector array.
THE ASSIGNMENT is : List the current process with show up their parent-child relation.
Question
I want to pass the vector value just ppids[i] to do this I write it only write
vector<process*>
If my function parameter declare like
vector<vector<process*>>
it must write "ppids" so it is not my intent.
Error
Cannot convert 1 parameter from 'std::vector<process *,std::allocator<_Ty>>'to 'std::vector &'
So, here is my codes. (Just a part of my code)
Struct Vector :
struct process {
string procName;
DWORD procPid;
DWORD procPpid;
};
main vector, get process information on msdn API
std::vector <process*> myProcess;
part of main below...
//
// Group Vector by PPid
//****(here is 2 dimension vector)****
std::vector< std::vector< process* > > ppids;
int n = 0;
int index = 1;
for (int i = 0, size = tempPid.size(); i < size; ++i) {
ppids.push_back(vector<process*>());
for (int j = 0, size2 = myProcess.size(); j < size2; ++j) {
if (myProcess[j]->procPpid == tempPid[i]) {
ppids[n].push_back(myProcess[j]);
};
}
for (int k = 0, size3 = ppids[n].size(); k < size3; ++k)
{
_tprintf(TEXT("%d \t"), index);
index++;
_tprintf(TEXT("[%s]"), ppids[n][k]->procName.c_str());
_tprintf(TEXT("[%d]"), ppids[n][k]->procPid);
_tprintf(TEXT("[%d] \n"), ppids[n][k]->procPpid);
}
n++;
}
myProcess.clear();
the function is called on here.
// Combine vector
myProcess = ppids[0];
std::vector <process*> tmpProcess;
for (int i = 1, size = ppids.size(); i < size; ++i) {
tmpProcess = combine(myProcess, ppids[i]);
myProcess.clear();
myProcess = tmpProcess;
}
and finally, this is my function.
vector<process*> combine(vector<process*> tempA, vector<process*> tempB) {
std::vector <process*> alloProcess;
for (int i = 0, size = tempA.size(); i < size; ++i) {
if (tempA[i]->procPid == tempB[1]->procPpid)
{
alloProcess.push_back(tempA[i]);
for (int j = 0, size2 = tempB.size(); j < size2; ++j) {
alloProcess.push_back(tempB[j]);
}
}
else {
alloProcess.push_back(tempB[i]);
}
}
return alloProcess;
}
Full codes on here:
https://gist.github.com/anonymous/25e636086bbfacbec78508736935d3af
void function(vector< vector<process> > *matrix)
Specifies a pointer, it is essentially passed by reference. However, in C++, it's better to avoid pointers and pass a reference directly:
void function(vector< vector<process> > &matrix)
and
function(matrix1); // Function call
Related
This is a member function:
Circle returnlargetcircle(Circle obj[], int size)
{
int radi[size];
for (int i = 0; i < size; i++)
{
radi[i] = obj[i].getradius();
}
for (int i = 0; i < size; i++)
{
if (radi[0] < radi[i])
{
radi[0] = radi[i];
}
}
}
expression must have a constant value --the value of parameter "size "(declared in line 61) can not be used as constant
What should be done in this case. I can't do this since my compiler is not allowing me to do this. What is an alternate way for this?
Array sizes must be compile time constants. You can use std::vector for dynamically sized arrays.
However, you don't need an array in the first place. Use std::max_element with a custom comparator, and don't forget to return a circle:
Circle returnlargetcircle(Circle obj[], int size) {
return *std::max_element(obj,obj+size,
[](const Circle& a, const Circle& b) {
return a.getradius() < b.getreadius();
});
}
You should also handle the case when obj is empty. You cannot return a Circle when there is none.
And if this is for an exercise and you are not allowed to use any std:: stuff then you still do not need the extra array:
Circle returnlargetcircle(Circle obj[],int size)
{
int max_radius = obj[0];
size_t max_index = 0;
for (size_t i = 1; i < size; i++) {
if (obj[i].getradius() > max_radius) {
max_radius = obj[i].getradius();
max_index = i;
}
}
return obj[i];
}
(again this assumes that obj has at least one element)
I'm writing a simple program within which a dynamic array is to be created. The function that is being used to create said array is in a second .cpp file, attached as a user-made library. Unfortunatelly Visual Studio pops an error saying that the program can't use uninitialized variable. I feel like it's a really easy problem to solve, but I don't know how to get through it. Here is the code:
int main()
{
int i = 5, j = 6;
string** Array;
createDefStruct(Array, i, j);
/*for (int k = 0; k < i; k++)
{
for (int m = 0; m < j; m++)
{
Array[i][j] = "YIKES";
cout << Array[i][j] << '\t';
}
cout << endl;
}*/
deleteDefStruct(Array, i);
return 0;
}
The createDefStruct function:
void createDefStruct(string** Arr, int varAttribCount, int varCount)
{
Arr = new string * [varAttribCount+1];
for (int i = 0; i < varAttribCount+1; i++)
Arr[i] = new string[varCount];
}
How do I go about initilizing a variable?
Thank you in advance!
So the problem is that instead of returning your array from the function you passed the array into the function as parameter. This mean that the variable is uninitialised in main (even though it is initiialised in createDefStruct). Rewrite like this
string** createDefStruct(int varAttribCount, int varCount)
{
string** Arr = new string * [varAttribCount+1];
for (int i = 0; i < varAttribCount+1; i++)
Arr[i] = new string[varCount];
return Arr;
}
int main()
{
int i = 5, j = 6;
string** Array = createDefStruct(i, j);
...
In general when you want a function to return a value you use return from inside the function to return that value. When you want to pass a value into a function you use a parameter. In your createDefStruct function varAttribCount and varCount are the parameters but the array should be a return value.
I have a stack-allocated fixed-sized 3D array declared as such:
ofVec2f geometry[24][30][4];
I need to pass this to a function to updates all the ofVec2f values, with a procedure along the lines of...
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 30; j++) {
ofVec2f verts[4];
for (int k = 0; k < 4; k++) {
verts[k] = foo;
}
geometry[i][j] = verts;
}
}
My question is, how do I pass this data structure to a function to update these values and have the array point to this new array of ofVec2f values? I imagine I will need to pass them via pointers but I'm not sure how to do it, especially since I have a fixed array on the stack.
Thanks! let me know if you need to see anything else.
You can do it:
1) By reference:
void function(ofVec2f (&array)[24][30][4]);
2) By pointer:
void function(ofVec2f (*array)[30][4]);
3) Using templates, to pass array of any size:
template <size_t X, size_t Y, size_t Z>
void function(ofVec2f (&array)[X][Y][Z]);
You can pass it by reference or pointer. If, like me, you find the syntax for reference-to-array a bit toxic you might like to use a using alias or a typedef:
using GeometryType = ofVec2f[24][30][4]; // C++11
//typedef ofVec2f GeometryType[24][30][4]; // C++98
void fillGeometry(GeometryType& geometry) {
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 30; j++) {
for (int k = 0; k < 4; k++) {
geometry[i][j][k].setX(0.0);
}
}
}
}
Have you kept a reference to the old arrays anywhere? If not, why are you creating a new set of arrays and then pointing to them (and thus marking the old arrays for garbage collection)? Why not just assign the new values to the existing arrays?
for (int k = 0; k < 4; k++) { (and thus
geometry[i][j][k] = foo;
}
I am trying to resize a vector element of a structure and it causes segv. But when I did it individually for some small struct it worked fine. I am curious to know how it allocates memory to structure in which there is a vector element that could be resized. The below comment line causes segv in first iteration (type_index = 0).
Structure:-
struct thread_data {
dbPointer_t pObj;
dbObjId_t objId;
dbObjTypeId_t type;
dbObjId_t topCellId;
dbIteratorId_t objsIterId;
int precision;
int64_t shape_objs;
vector<vector<vector<PL_trp_header_t *> > > ps_hdrs;
int pool;
int num_layers;
int to_cell_id;
};
Below is the snippet of code:-
thread_data *t_data[types_length];
for(int type_index=0; type_index < types_length; ++type_index) {
t_data[type_index] = (thread_data*)malloc(sizeof(thread_data));
t_data[type_index]->pObj = NULL;
t_data[type_index]->objId = objId;
t_data[type_index]->type = shape_types[type_index];
t_data[type_index]->topCellId = topCellId;
t_data[type_index]->objsIterId = objsIterId;
t_data[type_index]->precision = nparams.unit_precision;
t_data[type_index]->shape_objs = 0;
t_data[type_index]->ps_hdrs.resize(num_layers); //this line causes segv
t_data[type_index]->pool = pool;
t_data[type_index]->num_layers = num_layers;
t_data[type_index]->to_cell_id = tocell_id;
for (int num = 0; num < num_layers; num++) {
t_data[type_index]->ps_hdrs[num].resize(index_limit);
for (int rows = 0; rows < index_limit; rows++)
t_data[type_index]->ps_hdrs[num][rows].resize(index_limit);
}
for(int i = 0; i < num_layers; i++) {
for (int rows = 0; rows < index_limit; rows++) {
for (int cols = 0; cols < index_limit; cols++) {
t_data[type_index]->ps_hdrs[i][rows][cols] = alloc_hdr(pool);
}
}
}
printf("In main: creating thread %d \n", type_index);
rc_thread = pthread_create(&threads[type_index], NULL, thread_fn, (void *) &t_data[type_index]);
if (rc_thread){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
free(t_data[type_index]);
}
I think you are allocating your data with malloc. In this case no constructors for your objects and theier members are called. This works with PODs but not with classes like vector. In the line with the error you try to access some unitialised memory like an vector. Try new and delete instead of mallac and free to solve this isue.
i have to implement a small and simple game in c++ (a maze) and I have some problems right now.
Some snippets:
I've got an array of object pointers which represents my fields in the maze
Field*** maze;
init of the maze:
for (n = 0; n < MAZE_WIDTH; n++) {
this->maze[n] = new Field*[MAZE_HEIGHT];
for (p = 0; p < MAZE_HEIGHT; p++) {
this->maze[n][p] = new Field();
this->maze[n][p]->x = n;
this->maze[n][p]->y = p;
}
}
When creating the maze i need a list of already visited fields and a stack
so I did:
std::vector<Field*> visited;
std::vector<Field*> stack;
Then later I want to put a Field* into my stack
stack.push_back(neighbour);
But after this push all values in the object are wrong.
Even if i try
neighbour = stack.back();
all the values are completly different
I already red some threads about this topic and that's why i chose a vector of pointers and not objects.
Where is my fault?
Edit:
Some more snippets as requested:
Of course I allocate memory for the mate itself
this->maze = new Field**[MAZE_WIDTH];
Field is a simple class which looks like:
class Field {
public:
Field();
~Field();
bool w_left;
bool w_right;
bool w_front;
bool w_back;
unsigned int x;
unsigned int y;
private:
};
Since, you didn't posted the code of how you are obtaining the values,
compare to this, and try to find your problem...
std::vector<std::vector<Field*> > maze;
// Ini
for(int i = 0; i < MAZE_WIDTH; i++)
{
maze.push_back(std::vector<Field*>());
for(int j = 0; j < MAZE_HEIGHT; j++)
{
maze[i].push_back(new Field());
maze[i][j]->x = i;
maze[i][j]->y = j;
}
}
std::vector<Field*> visited;
// push the field [4,5] in a visited vector
visited.push_back(maze[4][5]);
// Clean up
for(size_t i = 0; i < maze.size(); i++)
{
for(size_t j = 0; j < maze[i].size(); j++)
delete maze[i][j];
}
Why declare the maze as Field***?
The C++ alternative is std::vector<std::vector<Field*> > maze;, and that's what you should use.