473,386 Members | 1,801 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,386 software developers and data experts.

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 1318
"Martin Johansen" <ma******@is.online.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.nsc.no> "Martin Johansen" <ma******@is.online.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.learn.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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8
Jack Klein <ja*******@spamcop.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.nsc.no> "Martin Johansen"
|> <ma******@is.online.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 objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #10
Martin Dickopp wrote:
>
> '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 [...].
...


Sorry, but that's still incorrect. The words "is" and "has" do not have
that meaning in formal C lingo. I can only suggest that you take a
closer look at 6.3.2.1/4 yourself. It states clearly that _the_ _result_
_of_ _the_ _implicit_ _conversion_ applied to expression 'f' has
"pointer to function" type, not 'f' itself. Once again, the implicit
conversion is applied to the result of expression 'f'. But 'f' itself
has function type, not pointer type.

Best regards,
Andrey Tarasevich

Nov 14 '05 #11
In <86************@lns-vlq-37-82-254-162-226.adsl.proxad.net> James Kanze <ka***@gabi-soft.fr> writes:
Da*****@cern.ch (Dan Pop) writes:

|> In <qX*****************@news4.e.nsc.no> "Martin Johansen"
|> <ma******@is.online.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.
Unless it has excellent reasons for not doing so.
|> 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.
Also a much slower solution, for programs dealing with a lot of such
pointers (as in large arrays of pointers to functions).
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).
The ultimate reason is that this is what the ABI requires.
|> >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.
And on some others, they aren't.
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.


Trivially false on *any* Harvard architecture, the high end PDP-11s
included. All the world's not a von Neumann architecture...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12

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

Similar topics

220
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...
39
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...
28
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...
35
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...
51
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,...
4
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...
81
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...
14
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...
4
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...
22
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.