473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

const type qualifier and external linkage (term.?)

Hi group,

this is a 'best practice' type question (I want discussion of the issue
- whys and why nots - similar to "casting the return value of malloc()",
to cite an analogous case).

Let's say I have a function written in assembler/(some non-C language)
which takes one pointer-to-const-char arg, the declaration being:

extern unsigned int str_len(const char *s);

Is the 'const' type qualifier 1) advisable and 2) legal? The C compiler
has no way of enforcing what a non-C function does with the pointer, and
as I understand it, the type qualifier is a guarantee of sorts - the
compiler guarantees said l-value will not be modified using the passed
pointer.

If I'm not mistaken, this makes the presence of the qualifier
inadvisable. Correct?

Response appreciated. Apologies for possible OT or incorrect terminology.

-ben
--
Posted via a free Usenet account from http://www.teranews.com

Dec 30 '07 #1
4 2204
In article <47***********************@free.teranews.com>
Ben Petering <bj*@dfmagicp.orgwrote:
>this is a 'best practice' type question (I want discussion of the issue
- whys and why nots - similar to "casting the return value of malloc()",
to cite an analogous case).

Let's say I have a function written in assembler/(some non-C language)
which takes one pointer-to-const-char arg, the declaration being:

extern unsigned int str_len(const char *s);

Is the 'const' type qualifier 1) advisable and 2) legal?
It is definitely "legal" (a poorly-specified term, but no matter how
*you* are using it, I think it comes out the same ... of course it is
possible that none of the interpretations I am guessing at are the one
you intended). As for "advisable", that depends.
>The C compiler has no way of enforcing what a non-C function does with
the pointer,
Indeed -- but it has no way of enforcing what a C function does either:

void icky(const int *p) {
*(int *)p = 42;
}

is valid C code, with no diagnostic required. I would call this
"inadvisable" though. :-)
>and as I understand it, the type qualifier is a guarantee of sorts - the
compiler guarantees said l-value will not be modified using the passed
pointer.
No, there is no such guarantee (see icky() above). It does tend
to promise a human reader that the function will not modify *s,
though. The main thing that it (this const) does, in a C compiler,
is make calls that pass const-qualified pointers semantically
correct, so that no diagnostic is required.

I think this is far clearer when shown by example. Suppose we are
writing some C code and are working on some intermediate or top level
function:

void something(const char *str) {
unsigned int val;

... do some work ...
val = str_len(str); /* note this line */
... do more work ...
}

For whatever reason (good or bad) our function, something() in this
case, takes a "const char *" value. It then calls the assembly
str_len(), passing the pointer value "str" on. If you change the
declaration for str_len() to take plain (non-const-qualified)
"char *", the call at the "noted" line now violates a constraint,
and a diagnostic is required.

If something() is an intermediate-level function, and we want to
call it as follows, then having something() take "const char *" is
"good" because one of the following calls provides a pointer to
"const char":

const char const_buf[] = "hello";
char modifiable_buf[] = "world";
...
something(const_buf);
something(modifiable_buf);

It is OK to "add" a const qualifier (but only a "top level" one;
see the comp.lang.c FAQ for details; C gets this wrong while C++
gets it right) in a call, but it is not OK to remove one. This is
(presumably) why something() takes a "const char *", and if
something() needs to call str_len(), and str_len() will not modify
the chars involved, including the qualifier in the declaration for
str_len() is probably a good idea.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 30 '07 #2
Chris Torek wrote:
In article <47***********************@free.teranews.com>
Ben Petering <bj*@dfmagicp.orgwrote:
>The C compiler has no way of enforcing what a non-C function does with
the pointer,

Indeed -- but it has no way of enforcing what a C function does either:

void icky(const int *p) {
*(int *)p = 42;
}

is valid C code, with no diagnostic required. I would call this
"inadvisable" though. :-)
Even
int x = (int*)p;
would result in undefined behavior if p pointed to an object declared
const. I am aware of one implementation in which the results of
int x = (int*)p; and
int x = *p;
can be different for parameter const int *p.

--
Thad
Dec 30 '07 #3
On Sun, 30 Dec 2007 10:39:26 -0700, Thad Smith wrote:
Even
int x = (int*)p;
I'll assume you meant *(int*)p.
would result in undefined behavior if p pointed to an object declared
const. I am aware of one implementation in which the results of
int x = (int*)p; and
int x = *p;
can be different for parameter const int *p.
Accessing read-only memory using non-const pointers is certainly allowed
in C and in fact quite often done, since string literals have type (non-
const) char[]. It's also valid for objects defined explicitly as const;
there's simply no prohibition against doing so. The behaviour is
undefined if you try to write to read-only data (whether string literal
or const). Could you give some details about your implementation?
Dec 30 '07 #4
Harald van Dijk wrote:
On Sun, 30 Dec 2007 10:39:26 -0700, Thad Smith wrote:
>Even
int x = (int*)p;

I'll assume you meant *(int*)p.
Yes. Thanks for catching this.
>would result in undefined behavior if p pointed to an object declared
const. I am aware of one implementation in which the results of
int x = (int*)p; and
int x = *p;
can be different for parameter const int *p.

Accessing read-only memory using non-const pointers is certainly allowed
in C and in fact quite often done, since string literals have type (non-
const) char[]. It's also valid for objects defined explicitly as const;
there's simply no prohibition against doing so.
Oops, I misread the Standard. 6.7.3 p5 switched from discussing const
modifier to volatile modifier and left me with the wrong impression.
The behaviour is
undefined if you try to write to read-only data (whether string literal
or const). Could you give some details about your implementation?
A different addressing mechanism is used for read-mostly (flash) memory and
read-write memory (RAM). const variables are placed in flash memory.
Dereferencing a non-const pointer uses only RAM access while dereferencing
a const-qualified pointer does run-time testing to access either RAM or
flash. There is no language mechanism to write const variables. I believe
there is a compile-time option to place string literals in read-mostly memory.

--
Thad
Dec 31 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: William Payne | last post by:
Hello, in my application (which spans approximately 20 source files and a 2-3 thousand lines) I have a few global varialbes, variables that never change their values. One of them is declared like...
9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
4
by: Steven T. Hatton | last post by:
#include <iostream> namespace ns{ const char name = "This is a Class Name";//won't compile //char name = "This is a Class Name"; // compiles template <typename T, char* Name_CA=name> struct...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
5
by: MoslyChang | last post by:
Hi, All When I look at effective c++,item2 and item3. I have some basic questions , Does anyone be familar with this topic? it suggests const is perfer to #define, then I think how to replace...
5
by: gw7rib | last post by:
I was having linking errors when I put: const LPCTSTR Main_window_name = _TEXT("Thingy_main_window"); in one file and extern const LPCTSTR Main_window_name; in another. I've since...
4
by: newbarker | last post by:
Hello, Very basic question here. If I have the following constants declared in a.cpp and in b.cpp, would there be any possibility of duplicate symbols?: a.cpp -------- #include <string>
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.