* Need for const in function argument list
@ 2006-03-26 10:44 Shriramana Sharma
2006-03-26 10:57 ` Steve Graegert
0 siblings, 1 reply; 6+ messages in thread
From: Shriramana Sharma @ 2006-03-26 10:44 UTC (permalink / raw)
To: Linux C Programming List
In the Qt constructor for the DateTime class, I see:
QDateTime::QDateTime(const QDate &date)
what is the need for the keyword const here? If it is to indicate that the
date variable does not change, then in very many such places should the const
keyword be used. But it is not.
As a related question, in the syntax for printf, we have printf(const char
*format) -- why should we have const here?
--
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-03-26 W12-7 UTC+0530
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Need for const in function argument list
2006-03-26 10:44 Need for const in function argument list Shriramana Sharma
@ 2006-03-26 10:57 ` Steve Graegert
2006-03-26 13:38 ` Shriramana Sharma
0 siblings, 1 reply; 6+ messages in thread
From: Steve Graegert @ 2006-03-26 10:57 UTC (permalink / raw)
To: linux-c-programming
On 3/26/06, Shriramana Sharma <samjnaa@gmail.com> wrote:
> In the Qt constructor for the DateTime class, I see:
>
> QDateTime::QDateTime(const QDate &date)
>
> what is the need for the keyword const here? If it is to indicate that the
> date variable does not change, then in very many such places should the const
> keyword be used. But it is not.
You've answered your own question. If there is no const in places
where it should be there can be three reasons: (a) either the
developer have forgotten to mark the parameters const or (b) the code
has been written before ANSI C where the const keyword was not known.
Most sophisticated libraries provide properly designed functions, so I
am sure, (c) they have had their reasons not to "const" in some
places.
> As a related question, in the syntax for printf, we have printf(const char
> *format) -- why should we have const here?
Because the format (the string to display) is provided as a constant
expression and it is not being modified:
printf("%s\n", "abc");
printf("%s\n", mystring);
In both cases the format argument is a constant string.
\Steve
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Need for const in function argument list
2006-03-26 10:57 ` Steve Graegert
@ 2006-03-26 13:38 ` Shriramana Sharma
2006-03-26 15:38 ` Steve Graegert
2006-03-26 16:27 ` Glynn Clements
0 siblings, 2 replies; 6+ messages in thread
From: Shriramana Sharma @ 2006-03-26 13:38 UTC (permalink / raw)
To: Linux C Programming List
Sunday, 26 March 2006 16:27 samaye, Steve Graegert alekhiit:
> Because the format (the string to display) is provided as a constant
> expression and it is not being modified:
> printf("%s\n", "abc");
> printf("%s\n", mystring);
> In both cases the format argument is a constant string.
Why, the following works as well:
#include "stdio.h"
void main(void)
{
char s[10] = "\n%s\n\n";
printf(s, "hello");
}
Here s is not a const char *. It is a variable char *. That actually compiled
and executed. So probably it's only because the printf *function* does not
change the value (and *should* not change the value during parsing, I
presume).
--
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-03-26 W12-7 UTC+0530
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Need for const in function argument list
2006-03-26 13:38 ` Shriramana Sharma
@ 2006-03-26 15:38 ` Steve Graegert
2006-03-26 16:27 ` Glynn Clements
1 sibling, 0 replies; 6+ messages in thread
From: Steve Graegert @ 2006-03-26 15:38 UTC (permalink / raw)
To: linux-c-programming
On 3/26/06, Shriramana Sharma <samjnaa@gmail.com> wrote:
> Sunday, 26 March 2006 16:27 samaye, Steve Graegert alekhiit:
>
> > Because the format (the string to display) is provided as a constant
> > expression and it is not being modified:
> > printf("%s\n", "abc");
> > printf("%s\n", mystring);
> > In both cases the format argument is a constant string.
>
> Why, the following works as well:
>
> #include "stdio.h"
> void main(void)
> {
> char s[10] = "\n%s\n\n";
> printf(s, "hello");
> }
>
> Here s is not a const char *. It is a variable char *. That actually
> compiled and executed.
Yes, it compiles and why should it not do so? It's a __string constant__, since it cannot be changed in any way. It's effectively the same as the statement
char *s = "\n%s\n\n"
The situation is different with malloc(2). It allows for dynamic allocation of memory, thus turning string constants into dynamic data structures:
char * s;
s = (char *) malloc(100);
Every string inside double quotes (" ") is a character constant.
\Steve
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Need for const in function argument list
2006-03-26 13:38 ` Shriramana Sharma
2006-03-26 15:38 ` Steve Graegert
@ 2006-03-26 16:27 ` Glynn Clements
2006-03-27 8:25 ` Yorgos Pagles
1 sibling, 1 reply; 6+ messages in thread
From: Glynn Clements @ 2006-03-26 16:27 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
Shriramana Sharma wrote:
> > Because the format (the string to display) is provided as a constant
> > expression and it is not being modified:
> > printf("%s\n", "abc");
> > printf("%s\n", mystring);
> > In both cases the format argument is a constant string.
>
> Why, the following works as well:
>
> #include "stdio.h"
> void main(void)
> {
> char s[10] = "\n%s\n\n";
> printf(s, "hello");
> }
>
> Here s is not a const char *. It is a variable char *. That actually compiled
> and executed. So probably it's only because the printf *function* does not
> change the value (and *should* not change the value during parsing, I
> presume).
If a pointer parameter is declared as const, you can pass either const
or non-const pointers. If it isn't declared as const, then you can
only pass non-const pointers, not const pointers.
When applied to function parameters, const indicates that the function
will not modify the data to which it points. The absence of const
indicates that the function may modify the data, and thus you cannot
pass a pointer to immutable data.
When applied to variables (lvalues), const indicates that the data
should not be modified by anything. Attempts to modify it (either
directly or by passing a pointer as a non-const parameter) will
generate a warning, and the data itself may be stored in a read-only
memory segment.
Pointer types should always be declared as const unless you actually
need to modify the data.
Similarly, if you aren't going to modify an array, it should be
declared as const (or static const for a local variable), e.g.:
static const char s[] = "\n%s\n\n";
Doing so will cause the compiler to place the array in the rodata
section. The rodata section is mapped read-only, and one copy is
shared by all processes using the executable (or shared library),
rather than needing one copy for each process.
For local variables, using "static const" eliminates the need to copy
the string into the array at run-time. For a function which is called
frequently, this will improve performance.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Need for const in function argument list
2006-03-26 16:27 ` Glynn Clements
@ 2006-03-27 8:25 ` Yorgos Pagles
0 siblings, 0 replies; 6+ messages in thread
From: Yorgos Pagles @ 2006-03-27 8:25 UTC (permalink / raw)
To: Linux C Programming List
Glynn Clements wrote:
>
> If a pointer parameter is declared as const, you can pass either const
> or non-const pointers. If it isn't declared as const, then you can
> only pass non-const pointers, not const pointers.
>
For that alone please use const when it is needed. Some moron I had to
work with once wrote a wrapper for some printing functions and in the
most significant of his functions he had
void PrintText(char*){/...}
and there was no way for me to write:
PrintText("Hello World");
in my code, I had to use the ugly:
PrintText(const_cast<char*>("Hello World"));
because of someone else's ignorance.
--
Make it work. Make it fast. Make it right. Pick any two...
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2006-03-27 8:25 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-26 10:44 Need for const in function argument list Shriramana Sharma
2006-03-26 10:57 ` Steve Graegert
2006-03-26 13:38 ` Shriramana Sharma
2006-03-26 15:38 ` Steve Graegert
2006-03-26 16:27 ` Glynn Clements
2006-03-27 8:25 ` Yorgos Pagles
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).