473,698 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function pointer to itself

Hello

Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_poin ter = (void **)get_gp_value ;
return function_pointe r[1];
}

Thanks

Feb 17 '06 #1
12 1996


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

Can someone explain what the following code is doing?

void *get_gp_value()
{
void **function_poin ter = (void **)get_gp_value ;
return function_pointe r[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_poin ter = (void **)get_gp_value ;
return function_pointe r[1];
}


Invoking undefined behavior.

HTH, HAND.

--
Keith Thompson (The_Other_Keit h) 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_poin ter = (void **)get_gp_value ;
return function_pointe r[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_poin ter = (void **)get_gp_value ;
return function_pointe r[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************ ***********@tun ews.univie.ac.a t>,
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_sta rs[3] = { &a, &b, &c };

void **x = three_void_star s;

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_poin ter = (void **)get_gp_value ;
return function_pointe r[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_poin ter = (void **)get_gp_value ;
>> return function_pointe r[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_poin ter = (void **)get_gp_value ;
return function_pointe r[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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3250
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, ----------------------------------------------------- #include <iostream> using namespace std;
6
2538
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 called CUtils with a static method called print2std()
4
3622
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 useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
38
2365
by: maadhuu | last post by:
does it make sense to find the size of a function ??? something like sizeof(main) ??? thanking you ranjan.
41
10033
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 integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed...
14
2559
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 methods, and it operates on DataTables in excess of 100,000 rows of anywhere between 4 and 30 columns. I'd like to get the functions to return ByRef for greater speed and memory efficiency, but I can't see how to do that. --
23
7809
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 called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
6
1839
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 sort of thing I want to do (not the complete code, that works but is technically UB). #include <stdlib.h> void indirectmalloc(void** ptr, size_t s) {
17
3247
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: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
10
3600
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 cast if necessary but preferably to one of the /right/ function pointer
0
8597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8884
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8855
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4611
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3034
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2319
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.