function pointer question (weird behaviour)

B

BA

Hello all,

I need some help understand this ..

On Debian, using gcc :-

/* I have a structure defined as */
struct struImpl {
int unroll;
char *type;
void (*comp_func)(double *M, double *C);
void (*mvp_func)(double *C, double *X, double *Y);
}


/* Next I create an array of this struct as follows: */
struct struImpl Impls[] = {
{ 1, "CRS", compress2crs, mvp_crs },
{ 1, "CDS", compress2cds, mvp_cds }
};

/* also declare another function pointer */
void (*fptr)(double *, double *) = compress2crs;


printf(" Impls[0].comp_func = %x\n", Impls[0].comp_func); /* 1 */
printf(" compress2crs = %x\n", compress2crs); /* 2 */
printf(" fptr = %x\n", fptr); /* 3 */

now surprisingly,

/* 1 */ prints

Impls[0].comp_func = 8058d48
while /* 2 */ prints
compress2crs = 80497a8
and /* 3 */ prints
fptr = 80497a8

Any call made through fptr calls the correct function, while the
pointer in the structure does not seem to work.

Thank you,
 
B

BA

Hello all,

I need some help understand this ..

On Debian, using gcc :-

/* I have a structure defined as */
struct struImpl {
int unroll;
char *type;
void (*comp_func)(double *M, double *C);
void (*mvp_func)(double *C, double *X, double *Y);

}

Sorry, it was my mistake : I had it defined as
struct struImpl {
int unroll;
char *type;
void (*mvp_func)(double *C, double *X, double *Y);
void (*comp_func)(double *M, double *C);
}
/* Next I create an array of this struct as follows: */
struct struImpl Impls[] = {
{ 1, "CRS", compress2crs, mvp_crs },
{ 1, "CDS", compress2cds, mvp_cds }
};

/* also declare another function pointer */
void (*fptr)(double *, double *) = compress2crs;

printf(" Impls[0].comp_func = %x\n", Impls[0].comp_func); /* 1 */
printf(" compress2crs = %x\n", compress2crs); /* 2 */
printf(" fptr = %x\n", fptr); /* 3 */

now surprisingly,

/* 1 */ prints

Impls[0].comp_func = 8058d48
while /* 2 */ prints
compress2crs = 80497a8
and /* 3 */ prints
fptr = 80497a8

Any call made through fptr calls the correct function, while the
pointer in the structure does not seem to work.

Thank you,
 
A

A. Bolmarcich

Hello all,

I need some help understand this ..

On Debian, using gcc :-

/* I have a structure defined as */
struct struImpl {
int unroll;
char *type;
void (*comp_func)(double *M, double *C);
void (*mvp_func)(double *C, double *X, double *Y);

}

Sorry, it was my mistake : I had it defined as
struct struImpl {
int unroll;
char *type;
void (*mvp_func)(double *C, double *X, double *Y);
void (*comp_func)(double *M, double *C);
}
/* Next I create an array of this struct as follows: */
struct struImpl Impls[] = {
{ 1, "CRS", compress2crs, mvp_crs },
{ 1, "CDS", compress2cds, mvp_cds }
};

The initialization of the first array element is equivalent to the
assignment statements

Impls[0].unroll = 1;
Impls[0].type = "CRS";
Impls[0].mvp_func = compress2crs;
Impls[0].comp_func = mvp_crs;

The first field of the struct is initilizated to the first value in
the list of initial values, the second field to the second vaule,
and so on.
/* also declare another function pointer */
void (*fptr)(double *, double *) = compress2crs;

printf(" Impls[0].comp_func = %x\n", Impls[0].comp_func); /* 1 */
printf(" compress2crs = %x\n", compress2crs); /* 2 */
printf(" fptr = %x\n", fptr); /* 3 */

now surprisingly,

/* 1 */ prints

Impls[0].comp_func = 8058d48
while /* 2 */ prints
compress2crs = 80497a8
and /* 3 */ prints
fptr = 80497a8

It is not suprising that that Impl[0].comp_func which was initilized
to mvp_crs is a different value than compress2crs and fptr which was
assigned to compress2cr.
 
B

Barry Schwarz

Hello all,

I need some help understand this ..

On Debian, using gcc :-

/* I have a structure defined as */
struct struImpl {
int unroll;
char *type;
void (*comp_func)(double *M, double *C);
void (*mvp_func)(double *C, double *X, double *Y);

}

Sorry, it was my mistake : I had it defined as
struct struImpl {
int unroll;
char *type;
void (*mvp_func)(double *C, double *X, double *Y);
void (*comp_func)(double *M, double *C);
}
/* Next I create an array of this struct as follows: */
struct struImpl Impls[] = {
{ 1, "CRS", compress2crs, mvp_crs },
{ 1, "CDS", compress2cds, mvp_cds }
};

/* also declare another function pointer */
void (*fptr)(double *, double *) = compress2crs;

printf(" Impls[0].comp_func = %x\n", Impls[0].comp_func); /* 1 */
printf(" compress2crs = %x\n", compress2crs); /* 2 */
printf(" fptr = %x\n", fptr); /* 3 */

now surprisingly,

/* 1 */ prints

Impls[0].comp_func = 8058d48
while /* 2 */ prints
compress2crs = 80497a8
and /* 3 */ prints
fptr = 80497a8

Any call made through fptr calls the correct function, while the
pointer in the structure does not seem to work.

Since you have not shown us any code that attempts to execute any of
the three functions, it is going to be a little difficult for us to
tell you what you did wrong. Post a small, complete, compilable
program that you believe demonstrates the problem. Tell us what you
observed that was different than what you expected. Try to make the
code portable so that those of us on a different system can attempt
analysis.

And while you have received an answer regarding the output, you need
to be aware that there is no printf conversion specification that will
portably process a function pointer. Using %x, which expects an
unsigned int, only appears to work on systems where the pointer has
the same size and representation as an unsigned int and is passed to
printf in the same way an unsigned int would be passed. One common
approach, though still outside the scope of the standard and therefore
also undefined behavior, is to cast the pointer to void* and use %p.
To do it in a conforming manner, you can copy the contents of the
pointer to an array of unsigned char and print the elements of the
array.


Remove del for email
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top