473,606 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about functions

take the function:

char f(){
return(-1);
}

f is the function pointer, i know how it is used to call a function.

But, what exactly does the pointer point to?
Where is the return value stored?
Is it possible to get the code segment address, data segment address or the
size of the whole function in memory?

Thanks!
Nov 14 '05 #1
11 1329
"Martin Johansen" <ma******@is.on line.no> writes:
take the function:

char f(){
return(-1);
}

f is the function pointer, i know how it is used to call a function.

But, what exactly does the pointer point to?
It points to the function, in some unspecified way, i.e. the C standard
does not specify how it points to the function.
Where is the return value stored?
That depends on the implementation. The C standard does not specify how
the return value is returned.
Is it possible to get the code segment address, data segment address
or the size of the whole function in memory?


No, not in standard C. A code segment and/or data segment need not even
exist.

If you are interested in how these things are implemented on your
particular system, please ask in group dedicated to that system.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #2
In <qX************ *****@news4.e.n sc.no> "Martin Johansen" <ma******@is.on line.no> writes:
take the function:

char f(){
return(-1);
}

f is the function pointer, i know how it is used to call a function.

But, what exactly does the pointer point to?
It provides the necessary information for calling that function. When
this information doesn't fit into a pointer, it points to a place
containig this information. For example, on the IA64 architecture, two
64-bit addresses are needed for this purpose and a function pointer
actually points to a function descriptor that contains these addresses.
So, it takes a double indirection to dereference a function pointer on
that platform.
Where is the return value stored?
That's entirely irrelevant to function pointers. Return values are
typically stored in registers, these days.
Is it possible to get the code segment address, data segment address or the
size of the whole function in memory?


On common implementations , function pointers store the function's entry
address in the text segment. The function itself "knows" the addresses
of the objects it has to manipulate. However, there are exceptions from
this simple and common model, AS/400 being even more exotic than IA64:
function pointers are 768-bit (or so) entities on this platform.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #3
Martin Johansen wrote:
take the function:

char f(){
return(-1);
}

f is the function pointer, i know how it is used to call a function.
As others have stated, f is not the function pointer. "f" reprents the
location of a function. The compiler may not use any pointers to the
function at all. Depends on the implementation. It is conceivable that
the address of the function is pushed on the stack, then "popped" into
the program counter (or ip -- instruction pointer).


But, what exactly does the pointer point to?
A function pointer is:
char (*Pointer_To_f) ();
The above line declares a pointer to any kind of function that
has the same signature as your 'f' function.

One can take the location of the 'f' function and place it into
a pointer:
typedef char (*Pointer_To_f) ();
Pointer_To_f p_f;
p_f = f;
Where is the return value stored? The return value does not have to be stored.
A compiler could optimize this function and just assign the
calling variable to "-1".

Some implementations give you the freedom to place constant
data wherever you want to. Sometimes the constant data is
placed in the same region that the executable is, sometimes
not. Depends on the compiler and the implementation.

Is it possible to get the code segment address, data segment address or the
size of the whole function in memory? I've already asked this group if the size of a function
could be determined using only portable C, but they
said no. Other platform dependent tricks must be used
to determine the size of a function.

As far as the starting location, yes it can be obtained
using C. However, the C language has no concepts of code
segments or data segments. That is an implementation issue.

Thanks!

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #4
Thomas Matthews <Th************ *************** *@sbcglobal.net > writes:
Martin Johansen wrote:
take the function:
char f(){
return(-1);
}
f is the function pointer, i know how it is used to call a function.


As others have stated, f is not the function pointer.


`f' is a function designator, but except when it is the operand of the
`sizeof' or unary & operator, it is converted to a function pointer.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #5
Martin Johansen wrote:
take the function:

char f(){
return(-1);
}

f is the function pointer, i know how it is used to call a function.
'f' is _not_ a pointer. When used in an expression, 'f' is an lvalue of
function type. That's quite different from a pointer.
But, what exactly does the pointer point to?
Not applicable. There's no pointer here.
Where is the return value stored?
You mean between the moment of 'return' and the moment it gets into the
variable you provided when calling the function? It is stored
"somewhere" . Every implementation might do it differently.
Is it possible to get the code segment address, data segment address or the
size of the whole function in memory?


In general case - no.

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #6
Andrey Tarasevich <an************ **@hotmail.com> writes:
Martin Johansen wrote:
take the function:

char f(){
return(-1);
}

f is the function pointer, i know how it is used to call a function.


'f' is _not_ a pointer. When used in an expression, 'f' is an lvalue of
function type. That's quite different from a pointer.


When used in an expression, `f' *is* a pointer, unless it is the operand
of a `sizeof' or unary & operator. See 6.3.2.1#4.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #7
On Wed, 12 May 2004 23:52:35 +0200, Martin Dickopp
<ex************ ****@zero-based.org> wrote in comp.lang.c:
Andrey Tarasevich <an************ **@hotmail.com> writes:
Martin Johansen wrote:
take the function:

char f(){
return(-1);
}

f is the function pointer, i know how it is used to call a function.


'f' is _not_ a pointer. When used in an expression, 'f' is an lvalue of
function type. That's quite different from a pointer.


When used in an expression, `f' *is* a pointer, unless it is the operand
of a `sizeof' or unary & operator. See 6.3.2.1#4.

Martin


Why is it that you got this right in your answer to Thomas a few hours
earlier, but wrong here?

'f' is a function designator and has function type. First, last, and
always. It is never a pointer, any more than the name of an array is
a pointer. No matter what expression it is used in.

In most expressions, in fact all but the two you mentioned above, the
implementation evaluates the function designator to extract its
address, and extraction of an address always results in an rvalue of
pointer type.

But 'f' never becomes a pointer, just as in:

int func(int *);

int array [10];

func(array);

....'array' never becomes a pointer. A pointer rvalue is created by
extracting the address of a[0], and that rvalue is passed to 'func'.
'array' itself never becomes a pointer, in fact cannot become anything
other than what it is, i.e., a non-modifiable lvalue with the type
pointer to array of 10 ints.

Likewise, passing 'f' to a function that accepts a pointer to function
creates an rvalue of type pointer to function by extracting the
address of 'f'. 'f' itself is unchanged, and cannot ever be anything
other than a function designator having function type, as is clearly
quoted in the first sentence of the paragraph you cite from the
standard.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8
Jack Klein <ja*******@spam cop.net> writes:
On Wed, 12 May 2004 23:52:35 +0200, Martin Dickopp
<ex************ ****@zero-based.org> wrote in comp.lang.c:
Andrey Tarasevich <an************ **@hotmail.com> writes:
> Martin Johansen wrote:
>> take the function:
>>
>> char f(){
>> return(-1);
>> }
>>
>> f is the function pointer, i know how it is used to call a function.
>
> 'f' is _not_ a pointer. When used in an expression, 'f' is an lvalue of
> function type. That's quite different from a pointer.


When used in an expression, `f' *is* a pointer, unless it is the operand
of a `sizeof' or unary & operator. See 6.3.2.1#4.


Why is it that you got this right in your answer to Thomas a few hours
earlier, but wrong here?


Temporary brain hiccup. I should have written: When used in an
expression, `f' has pointer type, unless [...].

(Incidentally, this wouldn't have happend if I had adhered to my own
advice to never use words which can be an adjective with "object" and
"type" as nouns.)

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #9
Da*****@cern.ch (Dan Pop) writes:

|> In <qX************ *****@news4.e.n sc.no> "Martin Johansen"
|> <ma******@is.on line.no> writes:

|> >take the function:

|> >char f(){
|> > return(-1);
|> >}

|> >f is the function pointer, i know how it is used to call a function.

|> >But, what exactly does the pointer point to?

|> It provides the necessary information for calling that function.
|> When this information doesn't fit into a pointer, it points to a
|> place containig this information.

Since the implementation decides just how big a function pointer is, it
can always make it big enough for whatever information is necessary.

|> For example, on the IA64 architecture, two 64-bit addresses are
|> needed for this purpose and a function pointer actually points to a
|> function descriptor that contains these addresses. So, it takes a
|> double indirection to dereference a function pointer on that
|> platform.

A much simpler solution would simply be to make a function pointer twice
as big.

I suspect here that the reason has nothing to do with C, but with the
broken interface which Posix imposes on dlsym (and maybe other
functions).

|> >Where is the return value stored?

|> That's entirely irrelevant to function pointers. Return values are
|> typically stored in registers, these days.

More often, it depends on the type being returned. Different types can
be, and usually are, returned in different ways.

|> >Is it possible to get the code segment address, data segment
|> >address or the size of the whole function in memory?

|> On common implementations , function pointers store the function's
|> entry address in the text segment.

On a lot of implementations , segments are just an artifice of the
compiler/loader anyway. At the hardware level, either there is no such
thing as a segment, or the OS and compiler work together to make it seem
that way (even when it means artificially limiting the capacities of the
processor, as is the case with IA-32 architectures). Actual addressing
is linear, so code segment address and data segment address have no real
meaning.

|> The function itself "knows" the addresses of the objects it has to
|> manipulate. However, there are exceptions from this simple and
|> common model, AS/400 being even more exotic than IA64: function
|> pointers are 768-bit (or so) entities on this platform.

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #10

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

Similar topics

220
18961
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
39
3143
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am sure there are many discussions on the "problems" mentioned here. But I had this thoughts without looking into any forums or anything... it is kind of feedback.
28
3277
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are a number of aspects to this simplification, but for me the unification of methods and functions is the biggest benefit. All methods look like functions (which students already understand). Prototypes (classes) look like modules. This will...
35
4522
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I go to great lengths to restructure my code just to look concise and make it more manageable. When I say this, I'm also referring to the way I write my functions. It seems to me sometimes that I shouldn't have many void functions accepting...
51
4259
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class, a method (event) needs to be able to invoke methods of the second class. With static classes its possible but this is not desirable. There's obviouly some visibility problem I am not familiar with. It is not a parent-child relationship since...
4
1810
by: Tony Johansson | last post by:
Hello experts! I'm reading a book about C++ and there is something about inline that the book says that is unclear for me. The book says the following "Because inline functions are expanded at compile time, definitions of these functions, unlike other definitions, cannot be separately compiled and must be placed in header files. This creates a problem if the compiler does not actually inline a
81
7270
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
14
1650
by: Alan Silver | last post by:
Hello, I have spent ages trawling through Google, looking for information about global functions in ASP.NET and I'm still not clear about the best way to go about this (or not). I am writing a site that will be for members only. They will have to log in to gain access to any of the pages. I am holding the user information in an XML file (there will probably never be a large number of user, so this is efficient enough).
4
2033
by: Christoph Scholtes | last post by:
Hi, I have some questions about header files: Say I have a file functions.c which contains a couple of functions. I have declared some structs in this file too. The structs are defined in main.c. Now I create a header file which represents the interface of functions.c to my main program file main.c. I put in the header file: all function prototypes with keyword extern and the declarations of the structs, which are defined in the main...
22
2135
by: Neil Gould | last post by:
Or... when is a script not a script? I have several modules for managing different aspects of our club's website, most of which are multi-page. Does setting such things as server.ScriptTimeout or response.buffer values at the start of the first page persist throughout the session, or are they "reset" with each function or sub, etc.? TIA.
0
8016
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8440
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...
0
8306
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...
0
5466
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3937
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
3980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2448
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
1
1557
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1300
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.