471,585 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

function pointer to itself

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

Feb 17 '06 #1
12 1781


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.

--
Er*********@sun.com

Feb 17 '06 #2
"kalculus" <su***********@yahoo.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 17 '06 #3

"Keith Thompson" wrote:
"kalculus" wrote:
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
Feb 18 '06 #4
>> "kalculus" wrote:
Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_pointer = (void **)get_gp_value;
return function_pointer[1];
}
"Keith Thompson" wrote:
Invoking undefined behavior.

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

In article <43***********************@tunews.univie.ac.at>,
John F <sp**@127.0.0.1> wrote: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[i];
}

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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 18 '06 #5

John F wrote:
"kalculus" wrote:
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?
(void*)[1] ... since sizeof void is not defined...


sizeof (void *) IS defined.

Feb 18 '06 #6

"tmp123" wrote:
John F wrote:
> "kalculus" wrote:
>> 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
Feb 18 '06 #7

"Chris Torek" wrote:
"kalculus" wrote:
Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_pointer = (void **)get_gp_value;
return function_pointer[1];
}
"Keith Thompson" wrote:
Invoking undefined behavior.


(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 F wrote:
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.

<snip suspicious lines of code>
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:
<snip>
This program is quite useless, but its output is well-defined.


I agree.

--
John
Feb 18 '06 #8
On 2006-02-18, John F <sp**@127.0.0.1> wrote:
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]
Feb 18 '06 #9

"Jordan Abel" wrote:
On 2006-02-18, John F wrote:
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.

--
John
Feb 18 '06 #10
kalculus wrote:
Can someone explain what the following code is doing?

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


Waaay off-topic for comp.lang.c :-)

http://gcc.gnu.org/ml/gcc/2002-08/msg00654.html
Feb 19 '06 #11

"kalculus" <su***********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.h...html/asm7.html
http://www.redhat.com/docs/manuals/e...irectives.html

Feb 19 '06 #12

"kalculus" <su***********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.h...html/asm7.html
http://www.redhat.com/docs/manuals/e...irectives.html
Feb 20 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

38 posts views Thread by maadhuu | last post: by
41 posts views Thread by Alexei A. Frounze | last post: by
14 posts views Thread by Mike Labosh | last post: by
23 posts views Thread by bluejack | last post: by
10 posts views Thread by Richard Heathfield | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.