473,320 Members | 2,146 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,320 software developers and data experts.

Read-only outside translation unit


I have a translation unit which defines a global object:

BEGIN "yellow.c"

int i = 7;

END

I want this object to be accessible from other translation units, so I give
it external linkage. However I want it to be read-only in other translation
units. I didn't think it was too far-fetch to simply declare it as follows
in a header file:

BEGIN "yellow.h"

extern int const i;

END

Is this definitely not supported by C89? I realise I could write a function
which would return the variable's value, but I thought a solution like
above would be neater.

--
Tomás Ó hÉilidhe
Dec 8 '07 #1
4 1865
"Tomás Ó hÉilidhe" <to*@lavabit.comwrites:
I have a translation unit which defines a global object:

BEGIN "yellow.c"

int i = 7;

END

I want this object to be accessible from other translation units, so I give
it external linkage. However I want it to be read-only in other translation
units. I didn't think it was too far-fetch to simply declare it as follows
in a header file:

BEGIN "yellow.h"

extern int const i;

END

Is this definitely not supported by C89? I realise I could write a function
which would return the variable's value, but I thought a solution like
above would be neater.
I would say you're better off using get/set - with the set not exported
to other modules. Don't export the variable using the header - just give
access via your "get" routine. Much cleaner and safer IMO - more
apparent to the calling code which is getting access to the read only
(from outside its home module) variable too.
Dec 8 '07 #2
Tomás Ó hÉilidhe wrote:
I have a translation unit which defines a global object:

BEGIN "yellow.c"

int i = 7;

END

I want this object to be accessible from other translation units, so I give
it external linkage. However I want it to be read-only in other translation
units. I didn't think it was too far-fetch to simply declare it as follows
in a header file:

BEGIN "yellow.h"

extern int const i;

END

Is this definitely not supported by C89? I realise I could write a function
which would return the variable's value, but I thought a solution like
above would be neater.
If it's not too inconvenient, you can use indirection to achieve the
desired result:
"yellow.h":

extern const int * const pi;
"yellow.c":

#include "yellow.h"
static int i=7;
const int * const pi = &i;

Note: you should always #include a header file in the translation unit
that defines one of the objects or functions declared in the header.
This isn't needed for correct compilation, so people often thing it's
not useful. However, it helps ensure that you will be warned if your
header's declaration is inconsistent with the definition.
Dec 8 '07 #3
James Kuyper <ja*********@verizon.netwrote in
news:Y2x6j.1026$bW.251@trnddc07:
If it's not too inconvenient, you can use indirection to achieve the
desired result:
"yellow.h":

extern const int * const pi;
"yellow.c":

#include "yellow.h"
static int i=7;
const int * const pi = &i;

Or maybe:

"yellow.h":

extern int const *const pi;
#define i (*pi)
( ...and yes I *do* realise there'll be trouble if there's any variables
called "i" within functions.)

I've decided to just have the object non-const throughout all the
translation units... I just have to be sure I don't do something stupid
like write "*=" instead of "*".
--
Tomás Ó hÉilidhe
Dec 8 '07 #4
Tomás Ó hÉilidhe wrote:
I have a translation unit which defines a global object:

BEGIN "yellow.c"

int i = 7;

END

I want this object to be accessible from other translation units, so I give
it external linkage. However I want it to be read-only in other translation
units. I didn't think it was too far-fetch to simply declare it as follows
in a header file:

BEGIN "yellow.h"

extern int const i;

END

Is this definitely not supported by C89? I realise I could write a function
which would return the variable's value, but I thought a solution like
above would be neater.
You'll probably get away with it, but I see two problems
(others may see more):

- You'll be unable to #include "yellow.h" in the compilation
of yellow.c, because the const and non-const declarations
conflict. This isn't fatal, but it means the compiler
never gets a chance to detect other mismatches between
the declarations in the header and the definitions in the
.c file -- change `int' to `long' in just one place, for
example, and the compiler won't be able to warn you.

- Nothing prevents an "outsider" from writing his own
`extern int i;' declaration or from setting up a pointer
with `int *pi = (int*)&i;', and mucking with the value
of `i' to his heart's content. This, too, is non-fatal
under the assumption of "cooperative" outsiders, but the
protection given by `const' is merely advisory if the
actual object has external linkage and isn't really `const'.

For greater cleanliness and greater protection, if you need
either, use an accessor function. If you don't like the syntax,
you can disguise it with a macro:

/* yellow.c */
static int i;
int geti(void) { return i; }

/* yellow.h */
int geti(void);
#define i ( geti() )

.... although indiscriminate use of this tactic can make garbage
out of otherwise clean code.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Dec 8 '07 #5

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

Similar topics

2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
6
by: Steve | last post by:
Hi, I'm trying to convert a file reading loop into one using streams. The BSD OS read API returns the number of bytes read, but istream::read returns itself. How can I find out the number of...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
2
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing...
2
by: Andrea Bauer | last post by:
Hallo, wie kann ich so eine Datei unter .Net schreiben C++ oder C#. Bitte mit Funktionsaufrufen. Vielen Dank. Grüße Andrea <Product> <ProgramNumber>2</ProgramNumber>
4
by: Ollie Cook | last post by:
Hi, I am having some difficulty with read(2) and interrupting signals. I expect I am misunderstanding how the two work together, so would appreciate some guidance. I am trying to 'time out' a...
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
4
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
5
by: Thomas Christensen | last post by:
This issue has been raised a couple of times I am sure. But I have yet to find a satisfying answer. I am reading from a subprocess and this subprocess sometimes hang, in which case a call to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.