function pointer to itself

K

kalculus

Hello

Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_pointer = (void **)get_gp_value;
return function_pointer[1];
}

Thanks
 
E

Eric Sosman

kalculus wrote On 02/17/06 13:01,:
Hello

Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_pointer = (void **)get_gp_value;
return function_pointer[1];
}

It's some kind of non-portable hack, something
specific to one particular compiler. It seems to be
trying to retrieve the second "word" of the compiled
code of the function. The meaning of that "word" is
not defined by C; it's something to do with the way
functions are compiled on a particular machine. C
doesn't even guarantee that the "word" can be retrieved
at all; on some machines "code" cannot be treated as
"data."

As far as C is concerned, the only thing this code
does is break the rules.
 
K

Keith Thompson

kalculus said:
Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_pointer = (void **)get_gp_value;
return function_pointer[1];
}

Invoking undefined behavior.

HTH, HAND.
 
J

John F

Keith Thompson said:
kalculus said:
Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_pointer = (void **)get_gp_value;
return function_pointer[1];
}

Invoking undefined behavior.

OT:
I wonder how the compiler handles

(void*)[1] ... since sizeof void is not defined...

--
John

kalculus, please email me what type of compiler you are using and an object
file with a void main(...) and the function.
I wanna have a look at it :)

johnny.f "at" gmx.at
 
C

Chris Torek

kalculus said:
Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_pointer = (void **)get_gp_value;
return function_pointer[1];
}

(which it is indeed doing - it also returns nothing at all useful
on most machines)

OT:
I wonder how the compiler handles

(void*)[1] ... since sizeof void is not defined...

This is a syntax error, even in most non-C-but-kind-of-like-C languages.

Assuming you mean:

void *x = <some valid expression>;
... x[1] ...

Standard C requires a diagnostic.

A number of languages that vaguely resemble, but are not in fact, C,
allow this. What they do depends on the language.

Note that gcc by default implements a language almost but not entirely
unlike C :) and you have to tell it to use "-std=c89" (or "-ansi" in
older versions of gcc), or "-std=c99" for C99, and "-pedantic", to
get it to implement Standard C.

Note that:

void **x = ...
... x[1] ...

is very different from:

void *x = ...
... x[1] ...

The former actually means something in C, provided "x" points to the
first of at least two objects of type "void *". For instance, consider
the following somewhat silly code fragment:

int a;
double b;
struct S { char *p; char *q; } c;

void *three_void_stars[3] = { &a, &b, &c };

void **x = three_void_stars;

void *pick(int i) {
return x;
}

A call to pick() at this point is valid if it supplies an argument
between 0 and 2 inclusive. The value returned is &a, &b, or &c
(converted to "void *"), correspondingly. Thus, we can finish this
code off with:

#include <stdio.h>

int main(int argc, char **argv) {
int *ip;
double *dp;

ip = pick(0);
*ip = 42;
dp = pick(1);
*dp = 4.2;

if (argc > 1)
printf("pick(1): %f\n", *(double *)pick(1));
else
printf("pick(0): %d\n", *(int *)pick(0));
return 0;
}

This program is quite useless, but its output is well-defined.
 
J

John F

tmp123 said:
John said:
:
Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_pointer = (void **)get_gp_value;
return function_pointer[1];
}

OT:
I wonder how the compiler handles

OT? pointer usage in C?

Pointer usage not, but how a compiler does implement it is OT.

I was referring to the use of [] on a void* since it can't be dereferenced
according to my knowledge of the standard. (I actually misread the OPs **,
which is indeed different)
(void*)[1] ... since sizeof void is not defined...

sizeof (void *) IS defined.

I know. Should be sizeof(char*) then. But it is not obliged to fit that.

John
 
J

John F

Chris Torek said:
:
Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_pointer = (void **)get_gp_value;
return function_pointer[1];
}

(which it is indeed doing - it also returns nothing at all useful
on most machines)

that's why I asked the OP to email an object file or disassembly on his
machine, that's the only way to tell what it _actually_ does. It is unlikely
to be useful... (I don't see a rationale for that...)
John said:
OT:
I wonder how the compiler handles

(void*)[1] ... since sizeof void is not defined...

This is a syntax error, even in most non-C-but-kind-of-like-C languages.

I didn't mean it litterally (sic!) :)
Assuming you mean:

void *x = <some valid expression>;
... x[1] ...
indeed.

Standard C requires a diagnostic.

A number of languages that vaguely resemble, but are not in fact, C,
allow this. What they do depends on the language.

Well, yes. It violates the (void*)-not-dereferenceable part of the std.
Note that gcc by default implements a language almost but not entirely
unlike C :) and you have to tell it to use "-std=c89" (or "-ansi" in
older versions of gcc), or "-std=c99" for C99, and "-pedantic", to
get it to implement Standard C.

As does -a for Open Watcom.
Note that:

void **x = ...
... x[1] ...

is very different from:

void *x = ...
... x[1] ...

I see. I misread the OP (putting the "function pointer"-topic into my
input-filter...). I should go to sleep.
The former actually means something in C, provided "x" points to the
first of at least two objects of type "void *". For instance, consider
the following somewhat silly code fragment:

I see the difference now.

A call to pick() at this point is valid if it supplies an argument
between 0 and 2 inclusive. The value returned is &a, &b, or &c
(converted to "void *"), correspondingly. Thus, we can finish this
code off with:

This program is quite useless, but its output is well-defined.

I agree.
 
J

Jordan Abel

I know. Should be sizeof(char*) then. But it is not obliged to fit that.

It's not? As far as i know, it's guaranteed by the standard that char *
and void * have identical representation [same size, same alignment, can
be passed interchangeably to variadic functions or functions with no
prototype, same bit pattern represents the same address]
 
J

John F

Jordan Abel said:
I know. Should be sizeof(char*) then. But it is not obliged to fit that.

It's not? As far as i know, it's guaranteed by the standard that char *
and void * have identical representation [same size, same alignment, can
be passed interchangeably to variadic functions or functions with no
prototype, same bit pattern represents the same address]

Well yes... Just reread... you are right with that!
Sorry for not checking in the first place.
 
R

Rod Pemberton

kalculus said:
Hello

Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_pointer = (void **)get_gp_value;
return function_pointer[1];
}

From the two links below, my guess is that it code for the DEC Alpha,
although there is a GP value in the COFF or ELF formats. I'm not familiar
with the Alpha. But according to the information from the links, it is
loading the address of that function which is self-specified by an 'ldgt'
instruction. You'll need to ask someone who is familiar with the Alpha's.

Rod Pemberton

http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PS31D-TET1_html/asm7.html
http://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/gnu-assembler/alpha-directives.html
 
R

Rod Pemberton

kalculus said:
Hello

Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_pointer = (void **)get_gp_value;
return function_pointer[1];
}
<repost>

From the two links below, my guess is that it code for the DEC Alpha,
although there is a GP value in the COFF or ELF formats. I'm not familiar
with the Alpha. But according to the information from the links, it is
loading the address of that function which is self-specified by an 'ldgt'
instruction. You'll need to ask someone who is familiar with the Alpha's.

Rod Pemberton

http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PS31D-TET1_html/asm7.html
http://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/gnu-assembler/alpha-directives.html
 

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
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top