Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Exposing globals as read-only

Reply
Thread Tools

Exposing globals as read-only

 
 
Ark Khasin
Guest
Posts: n/a
 
      03-10-2008
Consider a problem of exposing some
T foo;
for read-only access by other modules, in order to prevent inadvertent
(as opposed to malicious) writes to it. I cannot afford and/or do not
want to incur the overhead of access functions. A solution could be:

foo.h:
extern T foo;
#define foo ((const T)foo) //hide the original foo

foo.c:
#include "foo.h"
#undef foo
T foo;

There can be variations on this theme avoiding the often deprecated
#undef, like
foo.h:
extern T foo;
#ifndef OWNER_foo
#define foo ((const T)foo) //hide the original foo
#endif

foo.c:
#define OWNER_foo
#include "foo.h"
T foo;

What are NG's opinions on whether
- it looks right aesthetically
- it is a good idea to "overload" the identifier
- this can be a recommended practice (pattern) for a company's coding
standard
- (probably OT) this can confuse popular or bundled code browsers,
debuggers etc

--
Thank you,
Ark
 
Reply With Quote
 
 
 
 
Falcon Kirtaran
Guest
Posts: n/a
 
      03-10-2008
Ark Khasin wrote:
> Consider a problem of exposing some
> T foo;
> for read-only access by other modules, in order to prevent inadvertent
> (as opposed to malicious) writes to it. I cannot afford and/or do not
> want to incur the overhead of access functions. A solution could be:
>
> foo.h:
> extern T foo;
> #define foo ((const T)foo) //hide the original foo
>
> foo.c:
> #include "foo.h"
> #undef foo
> T foo;
>
> There can be variations on this theme avoiding the often deprecated
> #undef, like
> foo.h:
> extern T foo;
> #ifndef OWNER_foo
> #define foo ((const T)foo) //hide the original foo
> #endif
>
> foo.c:
> #define OWNER_foo
> #include "foo.h"
> T foo;
>
> What are NG's opinions on whether
> - it looks right aesthetically
> - it is a good idea to "overload" the identifier
> - this can be a recommended practice (pattern) for a company's coding
> standard
> - (probably OT) this can confuse popular or bundled code browsers,
> debuggers etc
>
> --
> Thank you,
> Ark


I wonder if it would work to do something like:

extern const T foo;

--
--Falcon Kirtaran
 
Reply With Quote
 
 
 
 
Ark Khasin
Guest
Posts: n/a
 
      03-10-2008
Falcon Kirtaran wrote:
> Ark Khasin wrote:
>> Consider a problem of exposing some
>> T foo;
>> for read-only access by other modules, in order to prevent inadvertent
>> (as opposed to malicious) writes to it. I cannot afford and/or do not
>> want to incur the overhead of access functions. A solution could be:
>>
>> foo.h:
>> extern T foo;
>> #define foo ((const T)foo) //hide the original foo
>>
>> foo.c:
>> #include "foo.h"
>> #undef foo
>> T foo;
>>
>> There can be variations on this theme avoiding the often deprecated
>> #undef, like
>> foo.h:
>> extern T foo;
>> #ifndef OWNER_foo
>> #define foo ((const T)foo) //hide the original foo
>> #endif
>>
>> foo.c:
>> #define OWNER_foo
>> #include "foo.h"
>> T foo;
>>
>> What are NG's opinions on whether
>> - it looks right aesthetically
>> - it is a good idea to "overload" the identifier
>> - this can be a recommended practice (pattern) for a company's coding
>> standard
>> - (probably OT) this can confuse popular or bundled code browsers,
>> debuggers etc
>>

>
> I wonder if it would work to do something like:
>
> extern const T foo;
>

No: foo.c would have a conflict between the definition and the
declaration. Fooling the compiler by not including foo.h not only is
bad, but also causes UB (most likely, a linker's complaint)
--
Ark
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-10-2008
Ark Khasin wrote:
> Consider a problem of exposing some
> T foo;
> for read-only access by other modules, in order to prevent inadvertent
> (as opposed to malicious) writes to it. I cannot afford and/or do not
> want to incur the overhead of access functions. A solution could be:
>

Provide an inline function in a header.

--
Ian Collins.
 
Reply With Quote
 
Shri
Guest
Posts: n/a
 
      03-10-2008
Can this be done???

Put all the functions who want Write access to this global variable in
a different file, and make the variable "global static". so now the
variable is visible only to the functions that are defined in the new
file.

Now to give other modules read access, put a function in the new file
which when called will just return the value of the global variable (I
think this is not a big overhead).

Example:

New.c

static int Variable; // this is a global static variable and hence it
is visible only to the functions defined in this same file.

Func1()
{

.....
;;;;

}

//All functions which need write access

int GetVarValue()
{
return Variable;
}


Foo.c

int GetVarValue();

Func00()
{

....
;;;

}

//All functions that do not need write access to the global variable.
 
Reply With Quote
 
Ark Khasin
Guest
Posts: n/a
 
      03-10-2008
Ian Collins wrote:
> Ark Khasin wrote:
>> Consider a problem of exposing some
>> T foo;
>> for read-only access by other modules, in order to prevent inadvertent
>> (as opposed to malicious) writes to it. I cannot afford and/or do not
>> want to incur the overhead of access functions. A solution could be:
>>

> Provide an inline function in a header.
>

How does it hide the variable from inadvertent write access?
-- Ark
 
Reply With Quote
 
Ark Khasin
Guest
Posts: n/a
 
      03-10-2008
Shri wrote:
>
> Now to give other modules read access, put a function in the new file
> which when called will just return the value of the global variable (I
> think this is not a big overhead).
>

The whole point of the exercise is to avoid access functions.
-- Ark
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-10-2008
Ark Khasin wrote:
> Shri wrote:
>>
>> Now to give other modules read access, put a function in the new file
>> which when called will just return the value of the global variable (I
>> think this is not a big overhead).
>>

> The whole point of the exercise is to avoid access functions.


Why?

--
Ian Collins.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-10-2008
Ark Khasin wrote:
> Ian Collins wrote:
>> Ark Khasin wrote:
>>> Consider a problem of exposing some
>>> T foo;
>>> for read-only access by other modules, in order to prevent inadvertent
>>> (as opposed to malicious) writes to it. I cannot afford and/or do not
>>> want to incur the overhead of access functions. A solution could be:
>>>

>> Provide an inline function in a header.
>>

> How does it hide the variable from inadvertent write access?


You don't expose the variable, just the function that returns its value.

--
Ian Collins.
 
Reply With Quote
 
Ark Khasin
Guest
Posts: n/a
 
      03-10-2008
Ian Collins wrote:
> Ark Khasin wrote:
>> Ian Collins wrote:
>>> Ark Khasin wrote:
>>>> Consider a problem of exposing some
>>>> T foo;
>>>> for read-only access by other modules, in order to prevent inadvertent
>>>> (as opposed to malicious) writes to it. I cannot afford and/or do not
>>>> want to incur the overhead of access functions. A solution could be:
>>>>
>>> Provide an inline function in a header.
>>>

>> How does it hide the variable from inadvertent write access?

>
> You don't expose the variable, just the function that returns its value.
>

I may be thick but I don't know how an inline function would know what
the variable is unless it's declared... Could you please offer a snippet
of code?
-- Ark
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exposing Frameset & Frames in ASPx =?Utf-8?B?QVNQIFlhYm9o?= ASP .Net 1 09-02-2004 07:12 AM
Exposing event in user control Mark ASP .Net 1 07-02-2004 04:29 AM
Exposing properties of a user control to the web forms page Jason ASP .Net 1 02-16-2004 10:15 PM
exposing collection/array type properties wh ASP .Net 2 02-10-2004 08:56 AM
Exposing a string array on a control - how to get PB support Howard Dierking ASP .Net 1 08-14-2003 10:17 AM



Advertisments