Getting some namespace features in C with structs

J

James Harris

One thing I like about some other (non-C) languages is their
management of names so I got to wondering: Would it be a good or a bad
idea to use C's structs to provide some of the features of namespaces?

Unless the idea is a total non-starter are there *any* ways in which
it could usefully be used? The more I think about it the more options
come to mind but maybe I am not familiar enough with C to see why I
have never come across C programs which work that way.

James
 
I

Ian Collins

James said:
One thing I like about some other (non-C) languages is their
management of names so I got to wondering: Would it be a good or a bad
idea to use C's structs to provide some of the features of namespaces?

Unless the idea is a total non-starter are there *any* ways in which
it could usefully be used? The more I think about it the more options
come to mind but maybe I am not familiar enough with C to see why I
have never come across C programs which work that way.

C lacks scoping syntax, so you can only group items with an instance of
a struct wither than the struct type. For example in C++ you can write

struct X
{
static int n;
};

and you can refer to "X::n" without having to have an instance of X.
Thus C++ can (and did once upon a time) use structs as simple
namespaces. The main limitation was you had to put everything within
one struct declaration, hence one header.
 
M

Malcolm McLean

One thing I like about some other (non-C) languages is their
management of names so I got to wondering: Would it be a good or a bad
idea to use C's structs to provide some of the features of namespaces?


Unless the idea is a total non-starter are there *any* ways in which
it could usefully be used? The more I think about it the more options
come to mind but maybe I am not familiar enough with C to see why I
have never come across C programs which work that way.
One trick to is declare a struct "globals". Then you know where your global
variable are. Also, you can convert any function from using globals to
being a parameterised function by adding a GLOBALS * and mechanically going
through converting all the dots to arrows.

If you merge two programs then, as you say, you effectively have namespaces.
 
P

Paul N

One thing I like about some other (non-C) languages is their
management of names so I got to wondering: Would it be a good or a bad
idea to use C's structs to provide some of the features of namespaces?

Unless the idea is a total non-starter are there *any* ways in which
it could usefully be used? The more I think about it the more options
come to mind but maybe I am not familiar enough with C to see why I
have never come across C programs which work that way.

Wouldn't it be easier to use #defines? For instance,

#define name mynamespace_name

?
 
K

Keith Thompson

James Harris said:
One thing I like about some other (non-C) languages is their
management of names so I got to wondering: Would it be a good or a bad
idea to use C's structs to provide some of the features of namespaces?

Unless the idea is a total non-starter are there *any* ways in which
it could usefully be used? The more I think about it the more options
come to mind but maybe I am not familiar enough with C to see why I
have never come across C programs which work that way.

You can group object definitions within a struct; if there's only one
instance of the struct, it can act like a namespace:

struct {
int this;
char *that;
double the_other_thing;
} pseudo_namespace;

But you can't initialize them ("int this = 42;"), and you can't put
other declarations within a struct (typedefs, enum types, other structs,
functions, etc.).

You can have pointer-to-function members, but you have to initialize
them explicitly to point to the functions you want.
 
J

James Harris

Wouldn't it be easier to use #defines? For instance,

#define name mynamespace_name

?

That seems traditional but deals only with named constants and not
other names.

How would you avoid the potential for collision of the name prefix?
Modules published by two independent people could use the same prefix
AFAICS.

James
 
J

James Harris

You can group object definitions within a struct; if there's only one
instance of the struct, it can act like a namespace:

    struct {
        int this;
        char *that;
        double the_other_thing;
    } pseudo_namespace;

But you can't initialize them ("int this = 42;"), and you can't put
other declarations within a struct (typedefs, enum types, other structs,
functions, etc.).

For manifest constants couldn't they be initialised by assigning as in

... pseudo_namespace = {...};

Other types could be 'manually' initialised as in

pseudo_namespace.the_other_thing = 4.25;

Of course, I would choose shorter names!

Once initialised I would hope that the compiler would be smart enough
in its constant propagation to recognise that where the code later
refers to the initialised value that it is a constant.
You can have pointer-to-function members, but you have to initialize
them explicitly to point to the functions you want.

That sounds OK. Does that mean that functions could subsequently be
called with such as the following?

namespace.init();
namespace.process(namespace.CONSTANT);

James
 
J

James Harris

You can group object definitions within a struct; if there's only one
instance of the struct, it can act like a namespace:

Just picking up on the "one instance" part of your comment I wonder if
multiple instances could be used by passing a pointer to the requisite
instance to a function.

For example, say there are two units which are functionally identical
but exist at different addresses how about

#import foreign struct-defining header
dev0 = {...}; /* Set up addresses etc for this device */
dev1 = {...}; /* Set up addresses etc for this device */
x = dev.poll(&dev0);
y = dev.poll($dev1);

Then in the entirely separate device management module

#import own struct-defining header
int poll(struct dev *d)
{
return port_read(d->STATUS_REG);
}

The example cannot use d.STATUS_REG but d->STATUS_REG is as good.

Anyone know of a more reasonable way to do the same?

James
 
J

Jorgen Grahn

One thing I like about some other (non-C) languages is their
management of names so I got to wondering: Would it be a good or a bad
idea to use C's structs to provide some of the features of namespaces?


Unless the idea is a total non-starter are there *any* ways in which
it could usefully be used? The more I think about it the more options
come to mind but maybe I am not familiar enough with C to see why I
have never come across C programs which work that way.
One trick to is declare a struct "globals". Then you know where your global
variable are. [...]
If you merge two programs then, as you say, you effectively have namespaces.

I do that rather often. In general, I find structs to organize data
like that is underused in much of the source code I come across. I
may combine a whole bunch of global variables or arrays and next
structs to get some, well, structure:

static int rxBytes[30];
static int rxDatagrams[30];
static int txBytes[30];
static int txDatagrams[30];

/* turns into */

static struct {
struct {
struct {
int bytes;
int datagrams;
} rx;
struct {
int bytes;
int datagrams;
} tx;
} statistics[30];
} global;

I.e. global.statistics[5].rx.bytes = foo instead of rxBytes[5] = foo.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top