How to pass several objects to a function as parameter in c++ - c++

I have a class as below that its method accept an object as its parameter.
template <typename T> class Matrix{
private:
matrixNode<T>* rear=0;
std::vector<int> rows;
std::vector<int> cells;
std::vector<matrixNode<T>*> listofAddedNodes;
void generatetableRowCellArrays(int rowSize,int cellSize){
for (int i=0;i<rowSize;i++){
rows.push_back(i);
}
for (int i=0;i<cellSize;i++){
cells.push_back(i);
}
}
public:
Matrix(int rowSize,int cellSize){
generatetableRowCellArrays(rowSize,cellSize);
}
void ceateSparsMatrix(matrixNode<T> *node){
matrixNode<T> *mxNode=(matrixNode<T>*)malloc(sizeof(matrixNode<T>));
mxNode=node;
if(rear!=0){
rear->PL=(matrixNode<T>*)&rows[mxNode->line];
rear->PC=(matrixNode<T>*)&cells[mxNode->column];
}
rear=mxNode;
listofAddedNodes.push_back(mxNode);
}
void ceateSparsMatrix(matrixNode<T> node,int SpecificRow,int specificCell){
matrixNode<T> *mxNode=(matrixNode<T>*)malloc(sizeof(matrixNode<T>));
mxNode=node;
if(rear!=0){
rear->PL=&rows[SpecificRow];
rear->PC=&cells[specificCell];
}
rear=mxNode;
listofAddedNodesPointers.push_back(&rear->value);
}
void showAllAddedNodes(){
for(int i=0;i<listofAddedNodes.size();i++){
printf("|----------------------------|\n");
printf("|Value | %d |\n",listofAddedNodesPointers[i]->value);
printf("|Coll | %d |\n",listofAddedNodesPointers[i]->column);
printf("|Line | %d |\n",listofAddedNodesPointers[i]->line);
printf("|PC | %d |\n",listofAddedNodesPointers[i]->PC);
printf("|PL | %d |\n",listofAddedNodesPointers[i]->PL);
printf("|----------------------------|\n");
}
}};};
When i want to test this function , i create several objects of the "matrixNode" class and pass to the function but it doesn't work!
Matrix<int> matrix(5,6);
matrixNode<int> *matNode();
matNode->column=2;
matNode->line=0;
matNode->value=2;
matNode->PC=NULL;
matNode->PL=NULL;
matrixNode<int> *matNode2;
matNode2->column=5;
matNode2->line=5;
matNode2->value=5;
matNode2->PC=NULL;
matNode2->PL=NULL;
matrix.ceateSparsMatrix(matNode);
matrix.ceateSparsMatrix(matNode2);
It doesn't pass the objects well!
Can you please help me?
Thanks

You need to either dynamically allocate your matrix objects or pass pointers to existing objects.
Matrix<int> matrix(5,6);
matrixNode<int> * matNode = new matrixNode<int>;
matNode->column=2;
matNode->line=0;
matNode->value=2;
matNode->PC=NULL;
matNode->PL=NULL;
matrixNode<int> * matNode2 = new matrixNode<int>;
matNode2->column=5;
matNode2->line=5;
matNode2->value=5;
matNode2->PC=NULL;
matNode2->PL=NULL;
matrixNode<int> matNode3;
matNode3.column=5;
matNode3.line=3;
matNode3.value=17;
matNode3.PC=NULL;
matNode3.PL=NULL;
matrix.ceateSparsMatrix(matNode);
matrix.ceateSparsMatrix(matNode2);
matrix.ceateSparsMatrix(&matNode3);
This one of your issues.

Related

Passing array of class objects

I've been trying for a long time to pass an array of objects to another class object.
In settingUp.cpp:
//** Status classes and their functions **//
void settingUp(){
dataClass prueba0;
dataClass prueba1;
dataClass prueba2;
const dataClass * arrayPrueba[3];
prueba0.setValues(1);
prueba1.setValues(2);
prueba2.setValues(3);
arrayPrueba[0] = &prueba0;
arrayPrueba[1] = &prueba1;
arrayPrueba[2] = &prueba2;
statusClass status;
status.setValues(1, arrayPrueba);
status.printValues();
}
In classData.cpp:
//** dataClass and their functions **//
void dataClass::setValues(int _length){
length = _length;
}
void dataClass::printValues() const{
printf("TP: dataClass: length = %d\n", &length);
};
In statusClass.cpp:
//** Status classes and their functions **//
void statusClass::setValues (uint8_t _statusSelectorByte, const dataClass **_array){
newStatusSelectorByte = _statusSelectorByte;
array = *_array;
};
void statusClass::printValues(){
printf("TP: statusClass -> printValues: Prueba = %d\n", newStatusSelectorByte);
printf("TP: statusClass -> printValues: arrayPrueba = %d\n", array[1].length);
}
When I call:
status.printValues();
I can read only the fist element of the arrayPrueba.
In statusClass::setValues(), *_array is the same as _array[0]. You are storing only the first dataClass* pointer from the input array.
Later, when using array[1], you are mistreating array as-if it were a pointer to an array of objects, when it is really a pointer to a single object instead. You are thus reaching past that object into surrounding memory, which is undefined behavior (but may "work" in this case because an object may happen to actually exist at that location, but this is bad behavior to rely on).
You need to store the original array pointer, not a single element taken from the array.
private:
const dataClass **array; // <-- add an *
void statusClass::setValues (uint8_t _statusSelectorByte, const dataClass **_array){
newStatusSelectorByte = _statusSelectorByte;
array = _array; // <-- get rid of the *
};
void statusClass::printValues(){
printf("TP: statusClass -> printValues: Prueba = %d\n", newStatusSelectorByte);
printf("TP: statusClass -> printValues: arrayPrueba = %d\n", array[1]->length); // use -> instead of .
}
On a side note: in dataClass::printValues(), you need to drop the & when printing the value of length:
printf("TP: dataClass: length = %d\n", length);

Call Function From An Vector of Pointers of Abstract Class

I have an abstract class with 2 pure virtual functions:
class CCobjectBase
{
public:
CCobjectBase();
virtual void setCordFigure(double *, int) = 0;
virtual double* getCord() = 0;
};
And a class that is derived from the abstract one:
class CTriangle : public CCobjectBase
{
public:
CTriangle();
~CTriangle();
void setCordFigure(double *, int);
double* getCord();
private:
double *m_cord;
int m_size;
}
void CTriangle :: setCordFigure(double *cord, int size)
{
m_cord = cord;
m_size = size;
}
double * CTriangle :: getCord()
{
return m_cord;
}
I used a vector of pointers to the abstract class:
std::vector<CCobjectBase *> m_objectBaseList;
And i create and store some values in this vector as follow:
m_objectBaseList.push_back(new CTriangle());
m_objectBaseList.at(m_objectBaseList.size() - 1) -> setCordFigure(coordonate, size);
The problem occurs when i try to display the values from the vector:
for(size_t i=0; i< m_objectBaseList.size(); i++)
{
double * cord = m_objectBaseList.at(i) -> getCord();
out<<cord[0]<<" "<<cord[1]<<" "<<cord[2]<<" "<<cord[3]<<" "<<cord[4]<<" "<<cord[5]<<endl;
}
It seems that all the objects have the same values stored. I tried multiple solution but i can not simply figure it out. If i give inputs as follow:
Object1: 1 2 3 4 5 6
Object2: 3 2 3 3 5 5
...
Objectn: 1 1 2 2 1 1
All objects have the value of Objectn:
Object1: 1 1 2 2 1 1
Object2: 1 1 2 2 1 1
...
Objectn: 1 1 2 2 1 1
The problem is not from the parameter coordonate because i tested in the setCordFigure(double *cord, int size){} function and it shows fine.
Any possible sugestions?
EDIT:
The portion of the code where I set the values is actually like this:
void MainWindow :: creazaObiecte(int numarOrdine, double *coordonate, int size)
{
QTextStream out(stdout);
switch(numarOrdine)
{
case 1:
m_objectBaseList.push_back(new CTriangle());
m_objectBaseList.at(m_objectBaseList.size() - 1) -> setCordFigure(coordonate, size);
break;
...
}
}
I use this function from each line of a file what I read. The portion of the code where I call this function is something like:
while(--condition--)
{
double coordonate[30]; //coordonate is used to store values as an array Ex: 20 30 12 2 32 12 etc
//fill coordonate with values from the line of the file
creazaObiecte(ordinClasa, coordonate, size); //ordinClasa is a variable that stores the index of the object (1 stands for triangle)
}
EDIT: My original answer was in a context where I didn't realise m_coord is meant to be an array, so was inappropriate.
Your operation setCordFigure just sets your m_coord member pointer to the passed argument. It does not not copy the underlying array. So in the end all your objects end up pointing to the same array.
You need to actually copy the array in your setCordFigure function. You could do this with naked arrays, but its better to use a vector. So this is possible code:
class CCobjectBase
{
public:
CCobjectBase();
virtual void setCordFigure(const std::vector<double>&) = 0;
virtual std::vector<double>& getCord() = 0;
};
class CTriangle : public CCobjectBase
{
public:
CTriangle();
~CTriangle();
void setCordFigure(const std::vector<double>&);
std::vector<double>& getCord();
private:
std::vector<double> m_cord;
int m_size;
}
void CTriangle :: setCordFigure(const std::vector<double>& cord)
{
m_cord = cord;
}
std::vector<double>& CTriangle :: getCord()
{
return m_cord;
}
Then your creation function becomes:
void MainWindow :: creazaObiecte(int numarOrdine, const std::vector<double>& coordonate)
{
QTextStream out(stdout);
switch(numarOrdine)
{
case 1:
m_objectBaseList.push_back(new CTriangle());
m_objectBaseList.at(m_objectBaseList.size() - 1) -> setCordFigure(coordonate);
break;
...
}
}
And your population function becomes:
while(--condition--)
{
std::vector<double> coordonate(30); //coordonate is used to store values as an array Ex: 20 30 12 2 32 12 etc
//fill coordonate with values from the line of the file
creazaObiecte(ordinClasa, coordonate); //ordinClasa is a variable that stores the index of the object (1 stands for triangle)
}
double *m_cord;
You are storing a pointer to the coordinate in your class.
void CTriangle :: setCordFigure(double *cord, int size)
Then, probably, you are reading the "input" into some variables, which are then referenced in your CTriangle objects. So as the values of the variables are changed, all of the objects are really pointing to the same stuff.

Creating an object in device code

I want to create an object on the device and allocate it to a pointer available on the host. Is there something I'm doing wrong in here?
__global__ void createAProduction(DeviceProduction* production) {
production = new AProduction();
}
DeviceProduction * devAProduction = NULL;
cudaMalloc(&devAProduction, sizeof(AProduction));
createAProduction<<<1, 1>>>(devAProduction);
deviceProductions["A"] = devAProduction;
Somewhere further in the code I'd like to do sth. like:
BatchOperation ** devBatchOperations;
cudaMalloc((void **) &devBatchOperations, sizeof(BatchOperation *) * operationCount);
Then I populate that pointer array with that:
void DeviceBatchExecutor::execute(vector<BatchOperation> operationsToPerform) {
BatchOperation ** devBatchOperations;
cudaMalloc((void **) &devBatchOperations, sizeof(BatchOperation *) * operationsToPerform.size());
int i = 0;
for(batchOperationIt it = operationsToPerform.begin(); it != operationsToPerform.end(); ++it) {
BatchOperation * devBatchOperation;
cudaMalloc(&devBatchOperation, sizeof(BatchOperation));
cudaMemcpy(&devBatchOperation, &it, sizeof(BatchOperation), cudaMemcpyHostToDevice);
Vertex * devInputNode = it->inputNode->allocateToDevice();
cudaMemcpy(&(devBatchOperation->inputNode), &devInputNode, sizeof(Vertex *), cudaMemcpyDeviceToDevice);
cudaMemcpy(&(devBatchOperation->production), &(it->production), sizeof(Production *), cudaMemcpyDeviceToDevice);
cudaMemcpy(&devBatchOperations[i], &devBatchOperation, sizeof(BatchOperation *), cudaMemcpyDeviceToDevice);
i++;
}
int operationCount = operationsToPerform.size();
executeOperations<<<operationCount, 1>>>(devBatchOperations);
}
where production is a pointer to the device memory holding that created object AProduction. Then I finally invoke processing via
executeOperations<<<operationCount, 1>>>(devBatchOperations);
So I'm relying on virtual method calls. As those DeviceProduction objects were created on the device, there is also a virtual pointer table so it should work. See example here. But it doesn't since the received batch operations seem random... crashes on invocation.
__global__ void executeOperations(BatchOperation ** operation) {
operation[blockIdx.x]->production->apply(operation[blockIdx.x]->inputNode);
}
Batch operation is a struct holding the production to be executed.
struct BatchOperation {
Production * production;
Vertex * inputNode;
Vertex * outputNode;
};
Is there something I'm doing wrong in here?
Yes, probably. The pointer production is passed to the kernel by value:
createAProduction<<<1, 1>>>(devAProduction);
It points to a location in device memory somewhere, since you've already run cudaMalloc on it. This line of kernel code:
production = new AProduction();
overwrites the pass-by-value copy of the production pointer with a new one, returned by in-kernel new. That is almost certainly not what you had intended. (And you haven't defined what AProduction is.). At the completion of that kernel call, the pass-by-value "copy" of the pointer will be lost anyway. You might be able to fix it like this:
*production = *(new DeviceProduction());
Now your production pointer points to a region in device memory that holds an instantiated (on the device) object, which appears to be your intent there. Creating a new object just to copy it may not be necessary, but that is not the crux of the issue I'm trying to point out here. You can probably also "fix" this issue by passing a pointer-to-pointer to the kernel instead. You would then need to allocate for an array of pointers, and assign one of the individual pointers using the in-kernel new directly, as you have shown.
The remainder of your code has a great many items undefined. For example in the above code it's not clear why you would declare that production is a pointer to a DeviceProduction type, but then try to allocate an AProduction type to it. Presumably that is some form of object inheritance which is unclear.
Since you haven't really provided anything approaching a complete code, I've borrowed some pieces from here to put together a complete worked example, showing object creation/setup in one kernel, followed by another kernel that invokes virtual methods on those objects:
$ cat t1086.cu
#include <stdio.h>
#define N 4
class Polygon {
protected:
int width, height;
public:
__host__ __device__ void set_values (int a, int b)
{ width=a; height=b; }
__host__ __device__ virtual int area ()
{ return 0; }
};
class Rectangle: public Polygon {
public:
__host__ __device__ int area ()
{ return width * height; }
};
class Triangle: public Polygon {
public:
__host__ __device__ int area ()
{ return (width * height / 2); }
};
__global__ void setup_f(Polygon ** d_polys) {
int idx = threadIdx.x+blockDim.x*blockIdx.x;
if (idx < N) {
if (idx%2)
d_polys[idx] = new Rectangle();
else
d_polys[idx] = new Triangle();
d_polys[idx]->set_values(5,12);
}};
__global__ void area_f(Polygon ** d_polys) {
int idx = threadIdx.x+blockDim.x*blockIdx.x;
if (idx < N){
printf("area of object %d = %d\n", idx, d_polys[idx]->area());
}};
int main () {
Polygon **devPolys;
cudaMalloc(&devPolys,N*sizeof(Polygon *));
setup_f<<<1,N>>>(devPolys);
area_f<<<1,N>>>(devPolys);
cudaDeviceSynchronize();
}
$ nvcc -o t1086 t1086.cu
$ cuda-memcheck ./t1086
========= CUDA-MEMCHECK
area of object 0 = 30
area of object 1 = 60
area of object 2 = 30
area of object 3 = 60
========= ERROR SUMMARY: 0 errors
$
Robert's suggestion seems to made it work:
__global__ void createAProduction(DeviceProduction** production) {
int idx = threadIdx.x+blockDim.x*blockIdx.x;
if(idx == 0) {
production[0] = new AProduction();
}
}
Called like this:
DeviceProduction ** devAProduction = NULL;
cudaMalloc(&devAProduction, sizeof(AProduction *));
createAProduction<<<1, 1>>>(devAProduction);
gpuErrchk( cudaPeekAtLastError() );
gpuErrchk( cudaDeviceSynchronize() );
But if I want to keep single pointer structure for deviceProductions array would it be ok to do sth. like this?
deviceProductions["A"] = (DeviceProduction *) malloc(sizeof(AProduction *));
gpuErrchk(cudaMemcpy(deviceProductions["A"], devAProduction, sizeof(AProduction *), cudaMemcpyDeviceToHost));
My intention was to copy the pointer (address) to the host memory from the device memory. Am I doing it right?

C++ dereferencing an instance

I'm new to C++ and I'm trying to dry up my code, for example:
void gethit () {
Gert.hitbox(AA.x, AA.damage);
Gert.hitbox(AB.x, AB.damage);
Gert.hitbox(AC.x, AC.damage);
Gert.hitbox(AD.x, AD.damage);
Gert.hitbox(Terbil.x, Terbil.damage);
}
AA, AB, AC, AD and Terbil are all instances of a class called Entity with variables x and damage.
Every time I want to add a new instance I'll have to come into this function and add it manually. I'm trying to add all of the address of the instances to an array like so:
void * p_enemys[10];
p_enemys[0] = &AA;
p_enemys[1] = &AB;
p_enemys[2] = &AC;
p_enemys[3] = &AD;
p_enemys[4] = &Terbil;
Just wondering how I could call a function from the instance via the array, I tried to do
for(int i = 0; i < 10; i++;) {
Gert.hitbox(p_enemys[i].x, p_enemys[i].damage);
}
and g++ compiler spits out: "request for member `damage' in `p_enemys[i]', which is of non-aggregate type `void *'"
I don't really need to use arrays specifically any help is very appreciated.
Changes made, thanks #gldraphael!
vector <Entity*> p_Enemys(10);
void gethit () {
for (int i = 0; i < 10; ++i) {
Entity * ienemy = (Entity*) p_Enemys[i];
Gert.hitbox((ienemy->x), (ienemy->damage));
}
}
You can make an a std::vector as follows:
std::vector <Entity*> p_Enemys(10);
The assigning part remains the same:
p_enemys[0] = &AA;
p_enemys[1] = &AB;
p_enemys[2] = &AC;
p_enemys[3] = &AD;
p_enemys[4] = &Terbil;
You can then loop through the p_enemys as follows:
for(auto i : p_enemys) {
Gert.hitbox(i->x, i->damage);
}
So what was it that you missed?
The array was declared as an array of void*
So, in the loop, p_enemys[i] returned a void*.
Also class/struct members are accessed using a dereferencing operator ->. You used the membership operator . instead.
So this code should have worked instead:
for(int i = 0; i < p_enemys.size(); i++;) { //
Entity * ienemy = (Entity*) p_enemys[i]; // cast the void* into a Entity*
Gert.hitbox(ienemy->x, ienemy->damage);
}
As a general rule, avoid void*s whenever possible.

Code requires too much memory

I'm porting some code to another structure:
class EnvironObject
{
protected:
vector<float> mX, mY, mXSpeed, mYSpeed;
int mMaxObjects;
public:
virtual void init(int maxObjects);
virtual void setLimit(int limit);
virtual int getLimit();
virtual void update(float arg) = 0;
};
void EnvironObject::setLimit(int limit)
{
mMaxObjects = limit;
mX.resize(limit, 0); mY.resize(limit, 0);
mXSpeed.resize(limit, 0); mY.resize(limit, 0);
}
int EnvironObject::getLimit()
{
return mMaxObjects;
}
void EnvironObject::init(int maxObjects)
{
mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects);
fill(mX.begin(), mX.end(), 0);
fill(mY.begin(), mY.end(), 0);
fill(mXSpeed.begin(), mXSpeed.end(), 0);
fill(mYSpeed.begin(), mYSpeed.end(), 0);
/*mX.reserve(mMaxObjects * 1.5); mY.reserve(mMaxObjects * 1.5);
mXSpeed.reserve(mMaxObjects * 1.5); mYSpeed.reserve(mMaxObjects * 1.5);*/
mMaxObjects = maxObjects;
}
This is some basic class, now it's usage:
class Rain : public EnvironObject
{
public:
Rain(int maxDrops = 150);
void update(float windPower);
};
Rain::Rain(int maxDrops)
{
srand(time(NULL));
IEnvironObject::init(maxDrops);
}
void Rain::update(float windPower)
{
for (int i=0; i < mMaxObjects; i++)
{
mX[i] += mXSpeed[i];
mY[i] += mYSpeed[i];
mXSpeed[i] += windPower;
mYSpeed[i] += G;
// Drawing
}
}
The objects Rain creates with default constructor (so, each array is 150 elements size) and then I'm calling setLimit(50).
The problem is that code fails almost each running with exception:
terminate called after throwing an instance of 'std::bad_alloc'
And sometimes it segfaults at line:
mY[i] += mYSpeed[i];
I can't image what could it be, because the code is old and it worked. The new one is only base class.
And when I'm looking at RAM usage when starting app, I see almost +600 mb!
Look again at that function of yours:
void EnvironObject::init(int maxObjects)
{
mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects);
// ^
// ...
mMaxObjects = maxObjects;
}
You're using a not yet initialized variable.
A big problem with your class is that you are doing what's called two-phase construction. Your class EnvironObject has a compiler-supplied default constructor that creates an object with random values for all POD types (mMaxObjects). Users then need to call the init() method to really initialize the object. But that's what constructors are there for!
void EnvironObject::EnvironObject(int maxObjects)
: mMaxObjects(maxObjects)
, mX(maxObjects), mY(maxObjects), mXSpeed(maxObjects), mYSpeed(maxObjects)
{
/* these aren't necessary, std::vector automatically does this
fill(mX.begin(), mX.end(), 0);
fill(mY.begin(), mY.end(), 0);
fill(mXSpeed.begin(), mXSpeed.end(), 0);
fill(mYSpeed.begin(), mYSpeed.end(), 0);
*/
}
Derived classes can then use this constructor:
Rain::Rain(int maxDrops)
: EnvironObject(maxDrops)
{
srand(time(NULL));
}
Regarding this crash in the subscription mY[i] += mYSpeed[i]:
This might happen when you are calling this function through a pointer that's pointing to nowhere.
You're using mMaxObjects in init() before initializing it. So it has a random value.
void EnvironObject::init(int maxObjects)
{
mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects); // you mean maxObjects here
I think you want to replace
void EnvironObject::init(int maxObjects)
{
mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects);
with
void EnvironObject::init(int maxObjects)
{
mX = mY = mXSpeed = mYSpeed = std::vector<float>(maxObjects);
Notice the replacement of mMaxObject to maxObjects in the vector creation.
One comment, though it won't likely fix your memory error, is that since the fields mX, mY, mXSpeed, and mYSpeed seem related and the vectors are all the same size, you should consider merging them into one structure with four members, and having a single vector containing several of those structure instances.