passing ptr to function to another function

  • Thread starter Pushkar Pradhan
  • Start date
P

Pushkar Pradhan

I want a function to execute another function, which I pass to it,
sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style.

double exec_basecase(void (*func)(double *a, double *b, double *c),
double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB)
{
...../*other code*/
func(a, b, c);
.....
}

This is the call in main():
double *a = NULL;
double *b = NULL;
double *c = NULL;

/* allocate memory and initialize matrix */
a = gen_matrix(10, 10, 0);
b = gen_matrix(10, 10, 0);
c = gen_matrix(10, 10, 0);

mflops1[12] = exec_basecase(mm_6r6c_6r6c_bc(a, b, c), a, b, c, 6, 6, 6);

During compile it gives following warning (in the above call) and then
during execution crashes inside this function with message: Illegal
Instruction (core dumped).
exec_basecases.c: In function `main':
exec_basecases.c:46: warning: passing arg 1 of `exec_basecase' makes
pointer from integer without a cast

Can anyone tell what's wrong with my function call?
Pushkar Pradhan
 
C

Charles Harrison Caudill

Pushkar Pradhan said:
I want a function to execute another function, which I pass to it,
sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style.
double exec_basecase(void (*func)(double *a, double *b, double *c),
double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB)
{
..../*other code*/
func(a, b, c);
....
}
This is the call in main():
double *a = NULL;
double *b = NULL;
double *c = NULL;
/* allocate memory and initialize matrix */
a = gen_matrix(10, 10, 0);
b = gen_matrix(10, 10, 0);
c = gen_matrix(10, 10, 0);
mflops1[12] = exec_basecase(mm_6r6c_6r6c_bc(a, b, c), a, b, c, 6, 6, 6);
During compile it gives following warning (in the above call) and then
during execution crashes inside this function with message: Illegal
Instruction (core dumped).
exec_basecases.c: In function `main':
exec_basecases.c:46: warning: passing arg 1 of `exec_basecase' makes
pointer from integer without a cast
Can anyone tell what's wrong with my function call?
Pushkar Pradhan

The problem lies in the call to exec_basecase

Here's what you do:

exec_basecase(mm...(a, b, c), ...);

Here's what you should do:

exec_basecase(mm..., ...);

By placing the parens and arguments there, c assumes that you're
passing the return value of that function call. The pointer from
integer warning is because mm_... returns an int, which at least on
intel architecture, is the representation of a memory address or a
pointer. c assumes the returned value is the first instruction in
the function. When exec_basecase tries to execute the function, it
tries to execute code starting at the location in memory specified
by the return value of mm_...
 
T

The Real OS/2 Guy

I want a function to execute another function, which I pass to it,
sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style.

double exec_basecase(void (*func)(double *a, double *b, double *c),
double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB)
{
..../*other code*/
func(a, b, c);
....
}

This is the call in main():
double *a = NULL;
double *b = NULL;
double *c = NULL;

/* allocate memory and initialize matrix */
a = gen_matrix(10, 10, 0);
b = gen_matrix(10, 10, 0);
c = gen_matrix(10, 10, 0);

mflops1[12] = exec_basecase(mm_6r6c_6r6c_bc(a, b, c), a, b, c, 6, 6, 6);

This makes the steps:
calling mm_6r6c_6r6c_ba with the parameters a, b and c, then calling
exec_basecases with the parameters: result from the prior step, a, b,
c, 6,6,6

That is not that you have described above and the protoype says.

change it to
exec_basecases(mm_6r6c_6rc6c_bca, a,b,c, 6, 6, 6);
This delivers to exec_basecases the address of a function that gets 3
parameters. Which parameter that function gets is defined on the point
exec_basename calls the funtion its address it gets here.
During compile it gives following warning (in the above call) and then
during execution crashes inside this function with message: Illegal
Instruction (core dumped).
exec_basecases.c: In function `main':
exec_basecases.c:46: warning: passing arg 1 of `exec_basecase' makes
pointer from integer without a cast

Can anyone tell what's wrong with my function call?

Clearly you delivers the result of the fuction instead its address to
exec_basecases.

A call of a function is function_name(parameter list).
The address of a fuction is its naked name. Remove the parameterlist
and the brackets from the function name you want that ecec_basecases
should call itself.
 
P

Pushkar Pradhan

Both the responses to my post were helpful to solve and make me
understand, however I have this example from Kernighan and Richie:

void qsort(void *lineptr[], int left, int right, int (*comp)(void*, void
*));

int main()
{
/*other code*/
if((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort((void **) lineptr, 0, nlines-1,
int (*)(void*, void*))(numeric ? numcmp : strcmp));

comp() is a comparison function and numeric variable decides which
function to use.

How come they pass the return type in the function call? Just a curiousity.
The said:
I want a function to execute another function, which I pass to it,
sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style.

double exec_basecase(void (*func)(double *a, double *b, double *c),
double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB)
{
..../*other code*/
func(a, b, c);
....
}

This is the call in main():
double *a = NULL;
double *b = NULL;
double *c = NULL;

/* allocate memory and initialize matrix */
a = gen_matrix(10, 10, 0);
b = gen_matrix(10, 10, 0);
c = gen_matrix(10, 10, 0);

mflops1[12] = exec_basecase(mm_6r6c_6r6c_bc(a, b, c), a, b, c, 6, 6, 6);


This makes the steps:
calling mm_6r6c_6r6c_ba with the parameters a, b and c, then calling
exec_basecases with the parameters: result from the prior step, a, b,
c, 6,6,6

That is not that you have described above and the protoype says.

change it to
exec_basecases(mm_6r6c_6rc6c_bca, a,b,c, 6, 6, 6);
This delivers to exec_basecases the address of a function that gets 3
parameters. Which parameter that function gets is defined on the point
exec_basename calls the funtion its address it gets here.

During compile it gives following warning (in the above call) and then
during execution crashes inside this function with message: Illegal
Instruction (core dumped).
exec_basecases.c: In function `main':
exec_basecases.c:46: warning: passing arg 1 of `exec_basecase' makes
pointer from integer without a cast

Can anyone tell what's wrong with my function call?


Clearly you delivers the result of the fuction instead its address to
exec_basecases.

A call of a function is function_name(parameter list).
The address of a fuction is its naked name. Remove the parameterlist
and the brackets from the function name you want that ecec_basecases
should call itself.
 
M

Mike Wahler

Pushkar Pradhan said:
Both the responses to my post were helpful to solve and make me
understand, however I have this example from Kernighan and Richie:

void qsort(void *lineptr[], int left, int right, int (*comp)(void*, void

Note that this is not the prototype for the standard library
function 'qsort'. I see that there are a couple example
implementations of a 'qsort' funtion in the book, but
they're not the standard library function, where the
meanings of the second and third arguments are not
the same.

For the standard library qsort, the second and
third parameters have type 'size_t', not 'int'.
The second parameter is the number elements of
the array to be sorted, the third is the size of
each array element.
*));

int main()
{
/*other code*/
if((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort((void **) lineptr, 0, nlines-1,
int (*)(void*, void*))(numeric ? numcmp : strcmp));

comp() is a comparison function

'comp' is the name of a parameter which has a pointer type.
It's not a function.
and numeric variable decides which
function to use.

Yes, 'numeric' is being used as a selector for
which function to invoke.
How come they pass the return type in the function call?

Types are not passed as arguments, values are.
The address of one of the two functions 'numcmp'
and 'strcmp' is being passed, dependent upon the
'truth value' of 'numeric'.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top