I'm passing a pointer to a function. I'd like to assign a new address to the passed pointer inside the function, and I'd like that address to be used after the function returns. I'm not sure if this is possible, but I'd like to do:
int main()
{
int i = 100, j = 200;
int * intPtr = &i;
foo(intPtr, j);
// I want intPtr to point to j, which contains 200 after returning from foo.
}
void foo( int * fooPtr, int & newInt )
{
int * newIntPtr = &newInt;
fooPtr = newIntPtr;
}
Is this possible, or will intPtr not maintain the new assignment after returning from foo? Could this work (if it doesn't: why)?
Pass a reference to the pointer:
void foo( int *& fooPtr, int & newInt )
The reason why your method does not work is that you're passing the pointer by-value. Passing by-value creates a temporary within the function, so as soon as the function returns, any changes to the temporary go away.
It is no different than this:
void foo(int x)
{
x = 10;
}
int main()
{
int a = 0;
foo( a );
// a is still 0, not 10
}
The a is passed by value, so the foo() function changes the parameter to 10 within the function. However, you will see that a in main does not change to 10 after the function returns.
To change a, you need to pass the int by reference:
void foo(int& x)
{
x = 10;
}
int main()
{
int a = 0;
foo( a );
// a is now 10
}
Pass a pointer of the pointer and assign to it
int main()
{
int i = 100, j = 200;
int * intPtr = &i;
foo( &intPtr, j );
// I want intPtr to point to j, which contains 200 after returning from foo.
}
void foo( int ** fooPtr, int & newInt )
{
int * newIntPtr = newInt;
*fooPtr = newIntPtr;
}
If you programing in pure C you can do like this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void foo(int **, int *);
int main()
{
int i = 100, j = 200;
int * intPtr = &i;
int *intPtr2=&j;
foo( &intPtr, intPtr2 );
// I want intPtr to point to j, which contains 200 after returning from foo.
printf("%d",*intPtr);
}
void foo( int ** fooPtr, int * newInt )
{
int * newIntPtr = newInt;
*fooPtr = newIntPtr;
}
Related
Suppose I have a pointer to pointer to function taking int and returning int*.
int* (**ptr)(int) //i hope i'm not wrong here
How should I alloc memory for that pointer using new? And how can I create an array of pointers to functions with new?
I was trying something like this:
int* (**ptr)(int) = new int* (*)(int);
but it shows "expected primary-expression before ‘)’ token"
Here is a demonstrative program that shows how the array can be declared with a typedef and without a typedef.
#include <iostream>
int * func1(int value)
{
static int x = value;
return &x;
}
int * func2(int value)
{
static int x = value;
return &x;
}
int * func3(int value)
{
static int x = value;
return &x;
}
int main()
{
const int N = 3;
typedef int * (*PFunc)(int);
PFunc *ptr = new PFunc[N] { func1, func2, func3 };
int* (**ptr1)(int) = new ( int* (*[N])(int) ){ func1, func2, func3 };
for (int i = 0; i < N; i++)
{
std::cout << *ptr[i]( i ) << std::endl;
}
std::cout << std::endl;
for (int i = 0; i < N; i++)
{
std::cout << *ptr1[i]( i ) << std::endl;
}
std::cout << std::endl;
return 9;
}
The correct syntax to create an array of functions pointers is as follows:
int* (**ptr)(int) = new (int*(*[5])(int));
This creates an array of 5 function pointers, where each function pointer is of type int *(*)(int).
This can be simplified with a typedef:
typedef int *(*fp)(int);
fp *ptr2 = new fp[5];
I wish to
create an array of class/struct items (c1)
then create an array of pointer to the original array (*cp1), which can be sorted
then access members of the class from within a function.
However I'm getting stuck at the initial function call.
Here's my basic code:
struct Car
{
int speed;
};
Car c1[5];
Car *cp1[5];
int main() {
for (int i=0;i<5;i++) {
c1[i].speed = i;
cp1[i] = &c1[i];
}
garage(cp1, 5);
}
void garage(Car **ar, int n) {
int p = (*ar[n / 2])->speed;
}
First of all, your garage function is not known to the compiler at the place where you call it, since it is defined below main. To fix it, either place the function definition above main, or introduce it with a prototype.
Second, at the line int p = (*ar[n / 2])->speed;, *ar[n/2] is not a pointer, so you should use . instead of ->, as in int p = (*ar[n / 2]).speed;
Funcion garage must be declared before you can refer it.
void garage(Car **ar, int n);
int main()
{
//...
}
void garage(Car **ar, int n) {
//...
}
Function main in C++ shall have return type int
int main()
{
//...
}
And within the function the correct expression will look
void garage(Car **ar, int n) {
int p = (*ar )[n / 2]).speed;
}
Or
void garage(Car **ar, int n) {
int p = ar[n / 2]->speed;
}
Or
void garage(Car **ar, int n) {
int p = ( *ar[n / 2] ).speed;
}
struct Car
{
int speed;
};
Car c1[5];
Car *cp1[5];
void garage(Car **ar, int n); // forward declare garage
int main()
{
for (int i=0;i<5;i++) {
c1[i].speed = i;
cp1[i] = &c1[i];
}
garage(cp1, 5);
}
void garage(Car **ar, int n) {
int p = ar[n / 2]->speed; // -> dereferences the pointer, you don't need to
}
Finally, i have been able to pass a host function as a function pointer in CUDA kernel function (__global__ function). Thanks to Robert Crovella and njuffa for the answer. I have been able to pass a class member function(cpu function) as a function pointer to a CUDA kernel. But, the main problem is, I can only pass the static class member function. I am not being able to pass the function not declared as static.
My question is: How to pass non static member function into CUDA kernel
For Example:
__host__ __device__
static int CellfunPtr(void*ptr, int a);
The above function work because this member function is declared as static member function. If i do not declare this member function as a static member as ,
__host__ __device__
in CellfunPtr(void*ptr, int a);
then it does not work.
The complete code has four files.
fundef.h
typedef int (*pFunc_t)(void* ptr, int N);
solver.h file
class CalcVars {
int eqnCount;
int numCell;
int numTri;
int numTet;
public:
double* cellVel;
double* cellPre;
/** Constructor */
CalcVars(
const int eqnCount_,
const int numCell_,
const int numTri_,
const int numTet_
);
/** Destructor */
~CalcVars(void);
public:
void
CalcAdv();
__host__ __device__
static int
CellfunPtr(
void*ptr, int a
);
};
solver.cu
#include "solver.h"
#include "fundef.h"
#include <stdio.h>
__device__ pFunc_t pF1_d = CalcVars::CellfunPtr;
pFunc_t pF1_h ;
__global__ void kernel(int*a, pFunc_t func, void* thisPtr_){
int tid = threadIdx.x;
a[tid] = (*func)(thisPtr_, a[tid]);
};
/* Constructor */
CalcVars::CalcVars(
const int eqnCount_,
const int numCell_,
const int numTri_,
const int numTet_
)
{
this->eqnCount = eqnCount_;
this->numCell = numCell_;
this->numTri = numTri_;
this->cellVel = (double*) calloc((size_t) eqnCount, sizeof(double));
this->cellPre = (double*) calloc((size_t) eqnCount, sizeof(double));
}
/* Destructor */
CalcVars::~CalcVars(void)
{
free(this->cellVel);
free(this->cellPre);
}
void
CalcVars::CalcAdv(
){
/*int b1 = 0;
b1 = CellfunPtr(this, 1);*/
int Num = 50;
int *a1, *a1_dev;
a1 = (int *)malloc(Num*sizeof(int));
cudaMalloc((void**)&a1_dev, Num*sizeof(int));
for(int i = 0; i <Num; i++){
a1[i] = i;
}
cudaMemcpy(a1_dev, a1, Num*sizeof(int), cudaMemcpyHostToDevice);
//copy addresses of device functions to host
cudaMemcpyFromSymbol(&pF1_h, pF1_d, sizeof(pFunc_t));
kernel<<<1,42>>>(a1_dev, pF1_h, this);
cudaDeviceSynchronize();
cudaMemcpy(a1, a1_dev, Num*sizeof(int), cudaMemcpyDeviceToHost);
};
int
CalcVars::CellfunPtr(
void* ptr, int a
){
//CalcVars* ClsPtr = (CalcVars*)ptr;
printf("Printing from CPU function\n");
//int eqn_size = ClsPtr->eqnCount;
//printf("The number is %d",eqn_size);
return a-1;
};
main.cpp file
#include "solver.h"
int main(){
int n_Eqn, n_cell, n_tri, n_tetra;
n_Eqn = 100;
n_cell = 200;
n_tri = 300;
n_tetra = 400;
CalcVars* calcvars;
calcvars = new CalcVars(n_Eqn, n_cell, n_tri, n_tetra );
calcvars->CalcAdv();
system("pause");
}
The type of a member function is different:
typedef int (CalcVars::*MethodPtr)(int N);
__device__ MethodPtr pF1_d = &CalcVars::CellfunPtr;
You would then call it using:
__global__ void kernel(int*a, MethodPtr func, void* thisPtr_)
{
int tid = threadIdx.x;
CalcVars* c = ((CalcVars*)thisPtr_);
a[tid] = (c->*func)(a[tid]);
};
BUT the this pointer you are passing to the kernel is a host pointer:
kernel<<<1,42>>>(a1_dev, pF1_h, this);
This will result in invalid memory access in the kernel.
You would have to pass a device pointer of a CalcVars instance to the kernel in order to make it work.
As requested, a complete compilable example, which is a condensed version of your's and still suffers from the this pointer issue I wrote above.
demo.cu
#include <stdio.h>
struct CalcVars
{
void CalcAdv();
__host__ __device__
int CellfunPtr(int a);
};
typedef int (CalcVars::*MethodPtr)(int N);
__device__ MethodPtr pF1_d = &CalcVars::CellfunPtr;
MethodPtr pF1_h;
__global__ void kernel(int* a, MethodPtr func, void* thisPtr_)
{
int tid = threadIdx.x;
CalcVars* c = ((CalcVars*)thisPtr_);
a[tid] = (c->*func)(a[tid]);
};
voidCalcVars::CalcAdv()
{
int Num = 50;
int *a1, *a1_dev;
a1 = (int *)malloc(Num*sizeof(int));
cudaMalloc((void**)&a1_dev, Num*sizeof(int));
for (int i = 0; i <Num; i++)
{
a1[i] = i;
}
cudaMemcpy(a1_dev, a1, Num*sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpyFromSymbol(&pF1_h, pF1_d, sizeof(MethodPtr));
// DON'T pass the host this pointer here in real code
kernel<<<1,42>>>(a1_dev, pF1_h, this);
cudaDeviceSynchronize();
cudaMemcpy(a1, a1_dev, Num*sizeof(int), cudaMemcpyDeviceToHost);
};
int CalcVars::CellfunPtr(int a)
{
printf("Printing from CPU function\n");
return a-1;
};
int main()
{
CalcVars calcvars;
calcvars.CalcAdv();
}
I have a little question about understand how pointers and functions work.
I want to see how a function looks like qsort(), but I need to use my own function to swap elements and to compare elements. I am very surprised to know that my function does not swap data...
My code:
//prototypes file: other.h
void Sort(char* pcFirst, int nNumber, int size, void (*Swap)(void*, void*), int (*Compare)(void*, void*) ); //sorts any arrays
void SwapInt(void* p1, void* p2); // swap pointers
int CmpInt(void* p1, void* p2); // compare poineters
//realisation file: other.cpp
#include "other.h"
void Sort(char* pcFirst, int nNumber, int size,
void (*Swap)(void*, void*), int (*Compare)(void*, void*) )
{
int i;
for( i = 1; i < nNumber; i++)
for(int j = nNumber - 1; j >= i; j--)
{
char* pCurrent = pcFirst + j * size;
char* pPrevious = pcFirst + (j - 1) * size;
if( (*Compare)( pPrevious, pCurrent ) > 0 )// if > 0 then Swap
{
(*Swap)( pPrevious, pCurrent );
}
}
}
void SwapInt(void* p1, void* p2)
{
int * ptmp1 = static_cast<int*>(p1);
int * ptmp2 = static_cast<int*>(p2);
int * ptmp = ptmp1;
ptmp1 = ptmp2;
ptmp2 = ptmp;
}
int CmpInt(void* p1, void* p2)
{
int nResult;
int * ptmp1 = static_cast<int*>(p1);
int * ptmp2 = static_cast<int*>(p2);
nResult = (*ptmp1 - *ptmp2);
return nResult;
}
//main file: lab.cpp
#include <tchar.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include "other.h"
int _tmain()
{
int nAr[] = {33,44,55,22,11}; //array for sort
int nTotal = sizeof(nAr) / sizeof(int); //number of elements
for ( int i = 0; i < nTotal; i++)
{
printf("%d ",nAr[i]); // result of cycle is 33 44 55 22 11
}
Sort(reinterpret_cast<char*>(&nAr[0]), nTotal, sizeof(int), SwapInt, CmpInt);
for ( int i = 0; i < nTotal; i++)
{
printf("%d ",nAr[i]); // result of cycle is 33 44 55 22 11 too =(
}
}
Why does the array not change?
In the debugger I can see that all pointers change, and get correct values, but in main my array is not changed.
pointers point to objects
the code
int * ptmp = ptmp1;
ptmp1 = ptmp2;
ptmp2 = ptmp;
changes some pointer values locally in the function, and that's all.
in order to swap the values of two objects, pass them by reference:
void swap_values_of( int& a, int& b )
{
int const original_a = a;
a = b;
b = original_a;
}
you can also do that, less safely, with pointer arguments, then taking care to swap the values pointed to instead of the pointers themselves.
but except for purposes of learning, use std::swap instead
not asked for, but... if you change the current Microsoft-specific
int _tmain()
to just standard
int main()
then the code will (much more likely) work also in e.g. Linux.
just a tip
You may look at various combinations as these.....
#include<iostream>
#include<stdio.h>
#include<malloc.h>
//Call by Address
void SwapIntAddr(int* ptmp1, int* ptmp2)
{
int ptmp;
ptmp = *ptmp1;
*ptmp1 = *ptmp2;
*ptmp2 = ptmp;
}
//Call by Reference
void SwapIntRef(int& ptmp1, int& ptmp2)
{
int ptmp;
ptmp = ptmp1;
ptmp1 = ptmp2;
ptmp2 = ptmp;
}
//Call by Reference but in pointer level
void SwapPtrRef(int*& ptmp1, int*& ptmp2)
{
int* ptmp;
ptmp = ptmp1;
ptmp1 = ptmp2;
ptmp2 = ptmp;
}
//Call by Address but in Pointer level.
void SwapPtrAddr(int** ptmp1,int** ptmp2)
{
int** ptmp = (int**) malloc(sizeof(int*));
*ptmp = *ptmp1;
*ptmp1 = *ptmp2;
*ptmp2 = *ptmp;
}
int main(){
int a = 3, b= 5;
int* p1 = &a;
int* p2 = &b;
SwapIntAddr(p1,p2);
printf("%d %d\n",*p1,*p2);
SwapIntRef(*p1,*p2);
printf("%d %d\n",*p1,*p2);
SwapPtrRef(p1,p2);
printf("%d %d\n",*p1,*p2);
SwapPtrAddr(&p1,&p2);
printf("%d %d\n",*p1,*p2);
return 0;
}
Your SwapInt function swaps some pointers, not ints. Since all those pointers are local to SwapInt, it has no actual effect. Probably you meant to do something with the ints *ptmp1 and *ptmp2.
What you are actually doing is swapping pointers. What you are trying to do is to swap values, where that pointers point to. At least that comes from your program logic.
So your code could be something like this:
void SwapInt(void* p1, void* p2)
{
int * ptmp1 = static_cast<int*>(p1);
int * ptmp2 = static_cast<int*>(p2);
int ptmp = *ptmp1;
*ptmp1 = *ptmp2;
*ptmp2 = ptmp;
}
Lets say in my main method, I declare a const int array pointer pointing to an array created on the heap. I then want to initialize it's values (using the memory address) in a constructor TryInitialize() and then print them out. This is not working and I'm wondering what I'm doing wrong? Thanks!
#include "stdafx.h"
#include "part_one.h"
#include <string>
#include <iostream>
using namespace std;
string createTable(unsigned int* acc, double* bal, int n) {
string s;
char buf[50];
for (int i = 0; i < n; i++) {
sprintf_s(buf,"%7u\t%10.2f\n",acc[i], bal[i]);
s += string(buf);
}
return s;
}
int _tmain(int argc, _TCHAR* argv[])
{
const int *tempInt = new const int[4];
TryInitialize(tempInt);
std::cout << tempInt[1] << endl;
system("pause");
return 0;
}
And here is my code for my constructor:
#include "part_one.h"
TryInitialize::TryInitialize(void) {
}
TryInitialize::TryInitialize(int constInt[]) {
constInt[0] = 8;
constInt[1] = 0;
constInt[2] = 0;
constInt[3] = 8;
}
You should not change a const value.
For what you trying to accomplish I'd recommend declaring a non-const pointer and a const pointer and assigning the non-const one to the const one after the initialization:
int _tmain(int argc, _TCHAR* argv[])
{
const int *tempTempInt = new int[4];
TryInitialize(tempInt);
const int* const tempInt = tempTempInt;
std::cout << tempInt[1] << endl; //this is now constant.
system("pause");
return 0;
}
Also pay attention where you put the const in the pointer declaration:
const int* const tempInt = tempTempInt;
In the declaration above the second const means that you cannot change the pointer; the first const means that you cannot change the pointed value(s).
You declare the pointer as const int*. The const modifier means that you cannot change the array values.
Either remove the const, or create an initializer method for it that can allocate the array and return it (unlike a constructor).
const int* init_my_array()
{
int * ret = new int[4];
ret [0] = 8;
ret [1] = 0;
ret [2] = 0;
ret [3] = 8;
return ret;
}
...
const int *tempInt = init_my_array();
You don't. Why? Because if it's const, then it can't be changed once the object has been constructed. Note: Even setting it is effectively changing its value from its uninitialized value, which goes against the definition of const to begin with.