By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,696 Members | 1,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,696 IT Pros & Developers. It's quick & easy.

information hiding vs. inlining

P: n/a
Do compilers inline functions even if the programmer could not do
it manually because the needed information is hidden at the language level?

I am talking about stuff like incomplete types and static variables e.g.

...
get_attribute(object);
...
Where object is an incomplete type in this unit and get_attribute basically
just does object->attribute.

Or

get_color(x);

which should translate into Colors[x] but Colors[] is an array declared
static in another unit i.e. not accessible here at language level.

Jul 5 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
copx wrote:
Do compilers inline functions even if the programmer could not do
it manually because the needed information is hidden at the language
level?

I am talking about stuff like incomplete types and static variables
e.g.

..
get_attribute(object);
..
Where object is an incomplete type in this unit and get_attribute
basically just does object->attribute.

Or

get_color(x);

which should translate into Colors[x] but Colors[] is an array
declared static in another unit i.e. not accessible here at language
level.
The compiler could inline the functions if it had access to their source
code. It cannot do so if they are in the form of libraries. Usually
code to manage opaque data is already compiled and only public
declarations are available in source form, so inlining may not be
possible.

Jul 5 '08 #2

P: n/a


santosh wrote:
copx wrote:
Do compilers inline functions even if the programmer could not do
it manually because the needed information is hidden at the language
level?

The compiler could inline the functions if it had access to their source
code. It cannot do so if they are in the form of libraries. Usually
code to manage opaque data is already compiled and only public
declarations are available in source form, so inlining may not be
possible.
There are some compiler/linkers that make automated
inline choices at link time that only need library objects.
Most of the compilers that do full code optimization at
link time have the ability to also inline library or application
function objects without sources.

Walter..

Jul 5 '08 #3

P: n/a
santosh <sa*********@gmail.comwrites:
copx wrote:
>Do compilers inline functions even if the programmer could not do
it manually because the needed information is hidden at the language
level?

I am talking about stuff like incomplete types and static variables
e.g.

..
get_attribute(object);
..
Where object is an incomplete type in this unit and get_attribute
basically just does object->attribute.

Or

get_color(x);

which should translate into Colors[x] but Colors[] is an array
declared static in another unit i.e. not accessible here at language
level.

The compiler could inline the functions if it had access to their source
code. It cannot do so if they are in the form of libraries. Usually
code to manage opaque data is already compiled and only public
declarations are available in source form, so inlining may not be
possible.
Strictly speaking, the *compiler* can't do this kind of inlining even
if it has access to the source code. It can't know that you won't
change the source code and recompile it before linking.

For example, in a.c:

get_color(x);

and in b.c:

int get_color(int x)
{
static int Colors = { ... };
return Colors[x];
}

I can compile a.c, then completly change the implementation of
get_color() and recompile b.c, then link. If the compiler has inlined
the call in a.c, I get an inconsistent program.

This kind of fancy cross-unit inlining has to be done at the linking
phase. This might re-invoke the compiler in some cases; if so, it
does so at a time when you no longer have an opportunity to modify
anything.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 5 '08 #4

P: n/a

"copx" <co**@gazeta.plschrieb im Newsbeitrag news:g4**********@inews.gazeta.pl...
Do compilers inline functions even if the programmer could not do
it manually because the needed information is hidden at the language level?
[snip]

Thanks everyone!
Jul 5 '08 #5

P: n/a
copx wrote:
Do compilers inline functions even if the programmer could not do
it manually because the needed information is hidden at the language level?

I am talking about stuff like incomplete types and static variables e.g.

..
get_attribute(object);
..
Where object is an incomplete type in this unit and get_attribute basically
just does object->attribute.

Or

get_color(x);

which should translate into Colors[x] but Colors[] is an array declared
static in another unit i.e. not accessible here at language level.
Some implementations are capable of doing that; it is allowed under the
"as if" rule.

However, being able to inline (or do any other sort of optimization)
across translation units (i.e. object files) is still in its infancy and
not available with most implementations. If you want your accessor
functions to be inlined with common systems today, you will need to
provide a complete type and static inline accessor functions in your
header file. Examine, for instance, your system's <stdio.hfor ideas
on how to do this.

Note that using that strategy may cause significant versioning problems
for you if you use <OT>dynamic libraries</OTand aren't extremely careful.

S
Jul 6 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.