Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inline functions have addresses?

Reply
Thread Tools

Inline functions have addresses?

 
 
Ravi
Guest
Posts: n/a
 
      07-23-2010
In section 7.1.1 of the book The C++ Programming language the author
mentions -

"inline function still has a unique variable and so do the static
variables of an inline function"

I am confused. If I have inline function then it can't have address.
Does this happen in C also?
 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      07-23-2010
On Jul 23, 9:01*am, Ravi <(E-Mail Removed)> wrote:
> In section 7.1.1 of the book The C++ Programming language the author
> mentions -
>
> "inline function still has a unique variable and so do the static
> variables of an inline function"
>
> I am confused. If I have inline function then it can't have address.
> Does this happen in C also?


If you take the address of an inlined function, the compiler will
outline it somewhere and give you the address of that outlined
function.

C inline functions are subtly different, and I'll let someone else
tackle that part of your question.

-Howard
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      07-23-2010
On 23 Jul., 15:01, Ravi wrote:
>
> I am confused. If I have inline function then it can't have address.


Sure it can. Think of inline as an additional property of a function
and forget about what a compiler might or might not do w.r.t. actual
inlining. An "inline function" is just like a normal function and it
has an address as well (at least you can ask for it). But there are
two big differences in terms of semantics (I can think of):

- The one definition rule allows multiple definitions of the same
inline function (at most one definition per translation unit)
as long as the definitions are equivalent. This is not allowed
for non-inline functions.

- Any use of an inline function *requires* a definition of the
function in the translation unit where it is used. This is not
necessary for non-inline functions.

For a compiler that handles translation units independently (like most
compilers do), the benifits of these rules are:

- Inlining of an inline function is fairly easy because the
translation unit includes the function's definition.

- The compiler doesn't need to create a distinct symbol for the
inline function unless its address is taken or the function
is used but not actually inlined.


> Does this happen in C also?


In C the inline keyword is just a decoration. As far as I can tell, it
doesn't have any effect on the semantics of your program (unlike in C+
+). To avoid multiple definition errors C programmers often use inline
in combination with static which makes the function to have internal
linkage.


Cheers!
SG
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      07-23-2010
Ravi wrote:
> In section 7.1.1 of the book The C++ Programming language the author
> mentions -
>
> "inline function still has a unique variable and so do the static
> variables of an inline function"
>
> I am confused. If I have inline function then it can't have address.
> Does this happen in C also?


You are indeed confused.

Inline functions are... well, _functions_. And as all functions, they
have addresses. Where did you get that strange idea that an inline
function "can't have address"?

The fact that the body of inline function can be _inlined_ at some (or
all) points from where this function is called has absolutely no effect
on the function itself. In other words, actual _inlining_ is something
that affects specific _calls_ to the function, but has no effect on the
functions itself. In particular, there's nothing to prevent an inline
function from having an address.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Marek Borowski
Guest
Posts: n/a
 
      07-23-2010
On 23-07-2010 20:00, Andrey Tarasevich wrote:
> Ravi wrote:
>> In section 7.1.1 of the book The C++ Programming language the author
>> mentions -
>>
>> "inline function still has a unique variable and so do the static
>> variables of an inline function"
>>
>> I am confused. If I have inline function then it can't have address.
>> Does this happen in C also?

>
> You are indeed confused.
>
> Inline functions are... well, _functions_. And as all functions, they
> have addresses. Where did you get that strange idea that an inline
> function "can't have address"?
>

Not strange. What is reason for keeping not used code inside binary ?
(if all is inlined)

Regards

Marek

 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      07-23-2010
Marek Borowski wrote:
> On 23-07-2010 20:00, Andrey Tarasevich wrote:
>> Ravi wrote:
>>> In section 7.1.1 of the book The C++ Programming language the
>>> author mentions -
>>>
>>> "inline function still has a unique variable and so do the static
>>> variables of an inline function"
>>>
>>> I am confused. If I have inline function then it can't have
>>> address. Does this happen in C also?

>>
>> You are indeed confused.
>>
>> Inline functions are... well, _functions_. And as all functions,
>> they have addresses. Where did you get that strange idea that an
>> inline function "can't have address"?
>>

> Not strange. What is reason for keeping not used code inside binary
> ? (if all is inlined)
>


If you take its address, it must have one.

Otherwise the unused code can be removed.


Bo Persson


 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      07-23-2010
Marek Borowski wrote:
> On 23-07-2010 20:00, Andrey Tarasevich wrote:
>> Ravi wrote:
>>> In section 7.1.1 of the book The C++ Programming language the author
>>> mentions -
>>>
>>> "inline function still has a unique variable and so do the static
>>> variables of an inline function"
>>>
>>> I am confused. If I have inline function then it can't have address.
>>> Does this happen in C also?

>> You are indeed confused.
>>
>> Inline functions are... well, _functions_. And as all functions, they
>> have addresses. Where did you get that strange idea that an inline
>> function "can't have address"?
>>

> Not strange. What is reason for keeping not used code inside binary ?
> (if all is inlined)


Firstly, nobody said that it has to be kept in the final binary, if it
is really not used.

Secondly, one reason to keep it (at least at some intermediate stages of
the compilation) is that inline functions in C++ (just like any other
functions) have external linkage by default. Some implementations might
decide to generate and keep the unused body for a while, until the
compilation is pretty mush done, and it is finally known whether the
body is actually used.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-27-2010
SG <(E-Mail Removed)> wrote:
> In C the inline keyword is just a decoration. As far as I can tell, it
> doesn't have any effect on the semantics of your program (unlike in C+
> +). To avoid multiple definition errors C programmers often use inline
> in combination with static which makes the function to have internal
> linkage.


Does that mean it's not possible in C to do this (and have it work in
the same way)?

inline void foo()
{
static int counter = 0;
++counter;
...
}
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-27-2010
On 07/27/10 07:19 PM, Juha Nieminen wrote:
> SG<(E-Mail Removed)> wrote:
>> In C the inline keyword is just a decoration. As far as I can tell, it
>> doesn't have any effect on the semantics of your program (unlike in C+
>> +). To avoid multiple definition errors C programmers often use inline
>> in combination with static which makes the function to have internal
>> linkage.

>
> Does that mean it's not possible in C to do this (and have it work in
> the same way)?
>
> inline void foo()
> {
> static int counter = 0;
> ++counter;
> ...
> }


Considering the "as if" rule, it must.

--
Ian Collins
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-27-2010
Ian Collins <(E-Mail Removed)> wrote:
> On 07/27/10 07:19 PM, Juha Nieminen wrote:
>> SG<(E-Mail Removed)> wrote:
>>> In C the inline keyword is just a decoration. As far as I can tell, it
>>> doesn't have any effect on the semantics of your program (unlike in C+
>>> +). To avoid multiple definition errors C programmers often use inline
>>> in combination with static which makes the function to have internal
>>> linkage.

>>
>> Does that mean it's not possible in C to do this (and have it work in
>> the same way)?
>>
>> inline void foo()
>> {
>> static int counter = 0;
>> ++counter;
>> ...
>> }

>
> Considering the "as if" rule, it must.


It must what?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple functions (one version being inline and other beingnon-inline) Rahul C++ 3 02-28-2008 03:28 PM
converting inline functions to C functions jamihuq C Programming 7 05-17-2006 08:46 AM
Tool which expands implicitly inline inline functions tthunder@gmx.de C++ 3 06-16-2005 12:54 AM
Can we have inline functions in c? Prafull Soni C Programming 15 03-07-2005 10:46 PM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM



Advertisments