473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1973


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: cylin | last post by:
Dear all, I met a char pointer problem. please help,thanks. How to change the the value which is pointed by a char pointer by a function? for example,...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
38
by: maadhuu | last post by:
does it make sense to find the size of a function ??? something like sizeof(main) ??? thanking you ranjan.
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
14
by: Mike Labosh | last post by:
How do you define whether the return value of a function is ByRef or ByVal? I have a utility class that cleans imported records by doing *really heavy* string manipulation in lots of different...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
6
by: ais523 | last post by:
I'm trying to write a function that does some realloc-style modification of a pointer. The function itself works, but I'm having some problems with the prototype. This is a simple example of the...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.