From: Shriramana Sharma <samjnaa@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: efficiency in passing a value to a function
Date: Sun, 08 Apr 2007 20:15:42 +0530 [thread overview]
Message-ID: <46190016.2000402@gmail.com> (raw)
In-Reply-To: <6a00c8d50704041138l278f34d2t9c97ba551f7ad5c3@mail.gmail.com>
Steve Graegert wrote:
> I have seen many programs making use const reference parameters in
> order to inform the compiler that the parameter is read-only, and
> hence should be better optimized.
I thought the usage of const before & in function definitions was to
prevent the value from being modified. I never knew there is an
optimization aspect.
> Unfortunately, this intent is at odds with the C++ language
> definition. The const keyword says that the storage may not be
> modified through the given name. What it does not say is that the
> storage cannot be modified through some other name.
I observe that this is true for C too, not just C++. Anyway, how is it
exactly "at odds" with the language definition?
[BTW gcc is intelligent enough to give me a compile-time warning:
const.c:7: warning: initialization discards qualifiers from pointer
target type
when I write const int i = 2 ; int * iptr = & i ; and I don't even have
to give -Wall !]
> you can only initialize them, const is basically ineffective a
> improving run-time performance. It does, however, catch errors in the
> programming process.
const is also useful for preventing a function writing to a variable
passed to it as reference, which is what this thread was originally about.
Just for the record, are there any other uses for const?
Shriramana Sharma.
next prev parent reply other threads:[~2007-04-08 14:45 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-04 17:13 efficiency in passing a value to a function Shriramana Sharma
2007-04-04 18:27 ` Glynn Clements
2007-04-11 21:44 ` Adam Dyga
2007-04-04 18:38 ` Steve Graegert
2007-04-05 12:05 ` Glynn Clements
2007-04-05 13:02 ` Steve Graegert
2007-04-08 15:17 ` Shriramana Sharma
2007-04-13 17:23 ` Glynn Clements
2007-04-14 13:15 ` Adam Dyga
2007-04-08 14:45 ` Shriramana Sharma [this message]
2007-04-13 17:51 ` Glynn Clements
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=46190016.2000402@gmail.com \
--to=samjnaa@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).