Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
macro and static inline
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
sinbad  
View profile  
 More options Sep 28 2012, 12:51 am
Newsgroups: comp.lang.c
From: sinbad <sinbad.sin...@gmail.com>
Date: Thu, 27 Sep 2012 21:51:48 -0700 (PDT)
Local: Fri, Sep 28 2012 12:51 am
Subject: macro and static inline
when would one use macros and when static inline,
unless the static inline function is referenced
by it's address, it is 'almost' like a macro with
better readability, so why not use static inline
as macros.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Tsiombikas  
View profile  
 More options Sep 28 2012, 1:28 am
Newsgroups: comp.lang.c
From: John Tsiombikas <nucl...@member.fsf.org>
Date: 28 Sep 2012 05:28:45 GMT
Local: Fri, Sep 28 2012 1:28 am
Subject: Re: macro and static inline
On 2012-09-28, sinbad <sinbad.sin...@gmail.com> wrote:

> when would one use macros and when static inline,
> unless the static inline function is referenced
> by it's address, it is 'almost' like a macro with
> better readability, so why not use static inline
> as macros.

I use macros when:
- it's something so simple that the verbosity of the function
  definition syntax would add too much visual overhead

- I want a bit of repetitive code to be "pasted" a few times inside a
  function, which needs to use a bunch of local variables, in which case
  making it a function would recurire that I pass a large amount of
  arguments.

- I want to generate code of some sort, say multiple versions of a
  polygon rasterizer routine and call the right one depending on the
  current state vector, instead of branching a lot in that routine which
  would ruin performance on some systems when it's an integral part of
  some inner drawing loop.

- I want my code to compile with C89 compilers.

There might by other reasons out there, these are the ones I could pull
from the top of my head right now.

--
John Tsiombikas
http://nuclear.mutantstargoat.com/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kaz Kylheku  
View profile  
 More options Sep 28 2012, 1:29 am
Newsgroups: comp.lang.c
From: Kaz Kylheku <k...@kylheku.com>
Date: Fri, 28 Sep 2012 05:29:09 +0000 (UTC)
Local: Fri, Sep 28 2012 1:29 am
Subject: Re: macro and static inline
On 2012-09-28, sinbad <sinbad.sin...@gmail.com> wrote:

> when would one use macros and when static inline,
> unless the static inline function is referenced
> by it's address, it is 'almost' like a macro with
> better readability, so why not use static inline
> as macros.

Macros can do syntactic things that functions cannot, like introduce
declarations into the calling environment, control evaluation or
provide new syntax like for_each_element (x, list) { ... }.

(Try writing an inline function do do for_each_element.)

If you *can* write something as an inline function, then do it.  
If you cannot, then you have no choice but to try it with a macro.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johann Klammer  
View profile  
 More options Sep 28 2012, 1:30 am
Newsgroups: comp.lang.c
From: Johann Klammer <klamm...@NOSPAM.a1.net>
Date: Fri, 28 Sep 2012 07:26:47 +0200
Local: Fri, Sep 28 2012 1:26 am
Subject: Re: macro and static inline
sinbad wrote:
> when would one use macros and when static inline,
> unless the static inline function is referenced
> by it's address, it is 'almost' like a macro with
> better readability, so why not use static inline
> as macros.

It is for micromanagement fanatics. The compiler is free to ignore the
inline, and it usually does if you compile for space optimisation and
call the function from two or more different places.

If you want your compiler to decide, use static inline.
If you want it _always_ inlined, use macros.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kaz Kylheku  
View profile  
 More options Sep 28 2012, 1:39 am
Newsgroups: comp.lang.c
From: Kaz Kylheku <k...@kylheku.com>
Date: Fri, 28 Sep 2012 05:39:57 +0000 (UTC)
Local: Fri, Sep 28 2012 1:39 am
Subject: Re: macro and static inline
On 2012-09-28, Johann Klammer <klamm...@NOSPAM.a1.net> wrote:

> sinbad wrote:
>> when would one use macros and when static inline,
>> unless the static inline function is referenced
>> by it's address, it is 'almost' like a macro with
>> better readability, so why not use static inline
>> as macros.
> It is for micromanagement fanatics. The compiler is free to ignore the
> inline, and it usually does if you compile for space optimisation and
> call the function from two or more different places.

> If you want your compiler to decide, use static inline.
> If you want it _always_ inlined, use macros.

Even if you use static without inline, the compiler can inline anyway.
(For that reason, compilers should really treat inline seriously and
"do it", otherwise what is it for?)

In one recent project, I detect what kind of support there is for inline
using a shell script that compiles code samples. It prepares a suitable
"#define INLINE ..." line in a "config.h" header.

It will detect whether the static keyword is required or not by doing
linkage tests (is there a clash if the same inline function goes into
two translation units).

With gcc in C90 mode  (-ansi) it turns out to be:

  #define INLINE static __inline__

If I configure to build build the program using g++, then it's just "inline".

The shell code looks like this (but isn't the entire logic because it relies on
some rules in a makefile compile and which link a single program out of
conftest1.c and conftest2.c).

printf "Checking how to declare inline functions ... "

if [ -z "$inline" ] ; then
  for inline in \
    "inline" "static inline" "extern inline" \
    "__inline__" "static __inline__" "extern __inline__" \
    "static"
  do
    cat > conftest1.c <<!
$inline int func(void)
{
  return 0;

}

int main(void)
{
  return func();
}

!
    cat > conftest2.c <<!
$inline int func(void)
{
  return 0;
}

!
    rm -f conftest2
    if ! $make conftest2 > conftest.err 2>&1 ; then
        continue
    fi
    break
  done
fi

printf '"%s"\n' "$inline"
printf "#define INLINE $inline\n" >> config.h


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kuyper  
View profile  
 More options Sep 28 2012, 7:21 am
Newsgroups: comp.lang.c
From: James Kuyper <jameskuy...@verizon.net>
Date: Fri, 28 Sep 2012 07:21:33 -0400
Local: Fri, Sep 28 2012 7:21 am
Subject: Re: macro and static inline
On 09/28/2012 12:51 AM, sinbad wrote:

> when would one use macros and when static inline,
> unless the static inline function is referenced
> by it's address, it is 'almost' like a macro with
> better readability, so why not use static inline
> as macros.

I prefer declaring functions (static, inline, or otherwise) whenever
what I'm doing can be done using functions. I prefer function-like
macros only for the things that they can do, which can't be done by
actual functions. The # and ## operators are the prime examples. Also,
the expansion of a  macro that occurs inside the body of a function is
interpreted in the scope of that function, something that would not be
true of a call to a static inline function.

Macros also provide a weak form of genericity - consider, for example,
the max() macro. It's not as convenient, powerful, or type-safe as C++
templates, but in many cases it's better than what can be done in C
using an actual function. C2011 added a _Generic() feature which reduces
the need for using function-like macros for this purpose, but I don't
think it eliminates that need. I could be mistaken - I haven't had time
to really study _Generic().
--
James Kuyper


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Brown  
View profile  
 More options Sep 28 2012, 11:05 am
Newsgroups: comp.lang.c
From: David Brown <da...@westcontrol.removethisbit.com>
Date: Fri, 28 Sep 2012 17:02:31 +0200
Local: Fri, Sep 28 2012 11:02 am
Subject: Re: macro and static inline
On 28/09/2012 07:39, Kaz Kylheku wrote:

In my experience, compilers /do/ treat "inline" seriously.  They don't
treat it as a command (there are circumstances where it is impossible to
inline the function), but they usually treat it as a very heavy hint. So
unless you have forced the compiler to out-line the function (by using
recursion, taking its address, forgetting to enable optimisations,
etc.), then "inline" will normally do exactly that.

Compilers often have additional compiler-specific extensions (such as
gcc's "always_inline" and "flatten" attributes) to give even more
control over inlining.

As others have said, "static" functions may be inlined anyway - this is
particularly true for use-once functions when optimising, or very small
functions when optimising hard.  In fact, the compiler can - if it wants
- inline non-static functions as well.  It will have to generate
out-line versions too, but these can perhaps be garbage collected at
link time if they are not needed.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Wessel  
View profile  
 More options Sep 28 2012, 1:02 pm
Newsgroups: comp.lang.c
From: Robert Wessel <robertwess...@yahoo.com>
Date: Fri, 28 Sep 2012 12:02:39 -0500
Local: Fri, Sep 28 2012 1:02 pm
Subject: Re: macro and static inline
On Fri, 28 Sep 2012 07:26:47 +0200, Johann Klammer

<klamm...@NOSPAM.a1.net> wrote:
>sinbad wrote:
>> when would one use macros and when static inline,
>> unless the static inline function is referenced
>> by it's address, it is 'almost' like a macro with
>> better readability, so why not use static inline
>> as macros.
>It is for micromanagement fanatics. The compiler is free to ignore the
>inline, and it usually does if you compile for space optimisation and
>call the function from two or more different places.

>If you want your compiler to decide, use static inline.
>If you want it _always_ inlined, use macros.

While it's mostly theoretical these days, I've seen compilers in the
past (mainframe, Cobol*, in the era of very expensive memory), that
could actually find common code sequences, and "outline" them (IOW
turn them into a subroutine).  The point was to generate smaller code.

While I don't know of any compilers that will do it today, at least
theoretically, a compiler optimizing for size could take several macro
invocations meeting the right conditions, and generate a common
subroutine for the code they emit.

So not "always".

*Cobol would have particularly encouraged that optimization because
the lack of proper subroutines (at the time, it's a bit better now),
encouraged considerable replication of code.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Collins  
View profile  
 More options Sep 28 2012, 6:04 pm
Newsgroups: comp.lang.c
From: Ian Collins <ian-n...@hotmail.com>
Date: Sat, 29 Sep 2012 10:04:43 +1200
Local: Fri, Sep 28 2012 6:04 pm
Subject: Re: macro and static inline
On 09/29/12 05:02 AM, Robert Wessel wrote:

I haven't checked their current offerings, but Green Hills embedded
compiler had a space optimisation mode that did just that.  I'm sure
other embedded compilers offer similar options.  Memory doesn't have to
be expensive to be a limited resource.

--
Ian Collins


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ark  
View profile  
 More options Oct 4 2012, 2:30 am
Newsgroups: comp.lang.c
From: Ark <akha...@macroexpressions.com>
Date: Thu, 04 Oct 2012 02:30:24 -0400
Local: Thurs, Oct 4 2012 2:30 am
Subject: Re: macro and static inline

On 9/28/2012 12:51 AM, sinbad wrote:

> when would one use macros and when static inline,
> unless the static inline function is referenced
> by it's address, it is 'almost' like a macro with
> better readability, so why not use static inline
> as macros.

Amazingly, no-one mentioned static initialization.
Those of us who live with const data really and truly can't replace
initializer macros with functions, inline or not.
- Ark

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »