Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Static array defined in one module, but used in another module

Reply
Thread Tools

Static array defined in one module, but used in another module

 
 
pozzugno@gmail.com
Guest
Posts: n/a
 
      10-24-2012
I have a module in which I define many const arrays of int of different
lengths (someone knows SNMP OIDs?).
In the same module I really use these arrays and the length is calculated by
a simple preprocessor macro (OID_LENGTH).

--- oid.c --
#define OID_LENGTH(oid) (sizeof(oid) / sizeof(oid[0]))

const int oidParam1[] = { 1, 1, 3 };
const int oidParam2[] = { 1, 2 };
const int oidParam1[] = { 2, 1 };
const int oidParam1[] = { 2, 2, 5, 4 };

int print_oid(int *oid, size_t size) {
while(size--) {
printf("%d", *oid++);
if (size) putchar('.');
}
return putchar('\n');
}

....
print_oid(oidParam1, OID_LENGTH(oidParam1));
....
---

Now the application has grown and I want to create a different module that
uses the same arrays defined in oid.c. Certainly I can write the definition
in a "oid.h" include as extern, but I can't specify the length.

--- mib.h ---
extern int oidParam1[];
extern int oidParam2[];
int print_oid(int *oid, size_t size);
---

For example, how can I print oidParam1 with print_oid() calling it from a
different module? I couldn't use OID_LENGTH macro (even if I move its
declaration to mib.h).

Any suggestions?
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      10-24-2012
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have a module in which I define many const arrays of int of different
> lengths (someone knows SNMP OIDs?).
> In the same module I really use these arrays and the length is calculated
> by
> a simple preprocessor macro (OID_LENGTH).
>
> --- oid.c --
> #define OID_LENGTH(oid) (sizeof(oid) / sizeof(oid[0]))
>
> const int oidParam1[] = { 1, 1, 3 };
> const int oidParam2[] = { 1, 2 };
> const int oidParam1[] = { 2, 1 };
> const int oidParam1[] = { 2, 2, 5, 4 };


> Now the application has grown and I want to create a different module that
> uses the same arrays defined in oid.c. Certainly I can write the
> definition
> in a "oid.h" include as extern, but I can't specify the length.


How about defining a variable like this in same module as the arrays:

int oidParam1length = OID_LENGTH(oidParam1);

Then declaring this as extern in the other module and using this variable to
obtain the length. (Maybe you ought to stick a const prefix on it as well.).


--
Bartc



 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-24-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> I have a module in which I define many const arrays of int of different
> lengths (someone knows SNMP OIDs?).
> In the same module I really use these arrays and the length is calculated by
> a simple preprocessor macro (OID_LENGTH).
>
> --- oid.c --
> #define OID_LENGTH(oid) (sizeof(oid) / sizeof(oid[0]))
>
> const int oidParam1[] = { 1, 1, 3 };
> const int oidParam2[] = { 1, 2 };
> const int oidParam1[] = { 2, 1 };
> const int oidParam1[] = { 2, 2, 5, 4 };


[duplicate names are just a typo, presumably?]

> int print_oid(int *oid, size_t size) {
> while(size--) {
> printf("%d", *oid++);
> if (size) putchar('.');
> }
> return putchar('\n');
> }
>
> ...
> print_oid(oidParam1, OID_LENGTH(oidParam1));
> ...
> ---
>
> Now the application has grown and I want to create a different module that
> uses the same arrays defined in oid.c. Certainly I can write the definition
> in a "oid.h" include as extern, but I can't specify the length.
>
> --- mib.h ---
> extern int oidParam1[];
> extern int oidParam2[];
> int print_oid(int *oid, size_t size);
> ---
>
> For example, how can I print oidParam1 with print_oid() calling it from a
> different module? I couldn't use OID_LENGTH macro (even if I move its
> declaration to mib.h).
>
> Any suggestions?


A couple of things come to mind. First, can you switch from a
size-based interface to one that uses a sentinel? If there is junk int
that can never appear in an OID (-1?) putting that at the end would mean
you could avoid havint to have a length at all.

Alternatively, you could number (rather than name) the arrays and
provide access to them and their lengths via other arrays (or via access
functions).

This can be achieved without data duplication by using some
pre-processor magic. Write a file oid.h like this:

OID(oidParam1, 1, 1, 3 )
OID(oidParam2, 1, 2 )
OID(oidParam3, 2, 1 )
OID(oidParam4, 2, 2, 5, 4 )

and you can generate the tables like this:

#define OID(name, ...) static const int name[] = { __VA_ARGS__ };
#include "oid.h"

#undef OID
const int *const oid[] = {
#define OID(name, ...) name,
#include "oid.h"
};

#undef OID
const size_t oid_size[] = {
#define OID(name, ...) sizeof name/sizeof *name,
#include "oid.h"
};

Finally, you can make the index constants directly from the same source
file. This would go in a public header:

#define OID(name, ...) name,
enum {
#include "sz.h"
};

Many alternatives to this scheme are possible. You could obviously
define a struct that would contain the pointer and length as a single
object, for example.

--
Ben.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-24-2012
On 10/24/2012 06:45 AM, (E-Mail Removed) wrote:
> I have a module in which I define many const arrays of int of different
> lengths (someone knows SNMP OIDs?).
> In the same module I really use these arrays and the length is calculated by
> a simple preprocessor macro (OID_LENGTH).
>
> --- oid.c --
> #define OID_LENGTH(oid) (sizeof(oid) / sizeof(oid[0]))


There's nothing specific to OID about that macro, so it should be given
a more generic name, such as ARRAY_LENGTH.

> const int oidParam1[] = { 1, 1, 3 };
> const int oidParam2[] = { 1, 2 };
> const int oidParam1[] = { 2, 1 };
> const int oidParam1[] = { 2, 2, 5, 4 };
>
> int print_oid(int *oid, size_t size) {
> while(size--) {
> printf("%d", *oid++);
> if (size) putchar('.');
> }
> return putchar('\n');
> }
>
> ...
> print_oid(oidParam1, OID_LENGTH(oidParam1));
> ...
> ---
>
> Now the application has grown and I want to create a different module that
> uses the same arrays defined in oid.c. Certainly I can write the definition
> in a "oid.h" include as extern, but I can't specify the length.
>
> --- mib.h ---
> extern int oidParam1[];
> extern int oidParam2[];


You should have included 'const' in that declaration.

> int print_oid(int *oid, size_t size);
> ---
>
> For example, how can I print oidParam1 with print_oid() calling it from a
> different module? I couldn't use OID_LENGTH macro (even if I move its
> declaration to mib.h).


mib.h:
extern const int oidParam1[];
extern const size_t oidParam1_length;

oid.c:
#include "mib.h"
const int oidParam1[] = { 1, 1, 3 };
const size_t oidParam1_length = OID_LENGTH(oidParam1);
--
James Kuyper
 
Reply With Quote
 
pozz
Guest
Posts: n/a
 
      10-24-2012
On Wednesday, October 24, 2012 1:28:36 PM UTC+2, Ben Bacarisse wrote:
> (E-Mail Removed) writes:
>
> > I have a module in which I define many const arrays of int of different
> > lengths (someone knows SNMP OIDs?).
> > In the same module I really use these arrays and the length is calculated
> > by a simple preprocessor macro (OID_LENGTH).
> >
> > --- oid.c --

>
> > #define OID_LENGTH(oid) (sizeof(oid) / sizeof(oid[0]))
> >
> > const int oidParam1[] = { 1, 1, 3 };
> > const int oidParam2[] = { 1, 2 };
> > const int oidParam1[] = { 2, 1 };
> > const int oidParam1[] = { 2, 2, 5, 4 };

>
> [duplicate names are just a typo, presumably?]


Oh, yes, I'm sorry.

> > int print_oid(int *oid, size_t size) {
> > while(size--) {
> > printf("%d", *oid++);
> > if (size) putchar('.');
> > }
> > return putchar('\n');
> > }
> >
> > ...
> > print_oid(oidParam1, OID_LENGTH(oidParam1));
> > ...
> > ---
> >
> > Now the application has grown and I want to create a different module that
> > uses the same arrays defined in oid.c. Certainly I can write the definition
> > in a "oid.h" include as extern, but I can't specify the length.
> >
> > --- mib.h ---
> > extern int oidParam1[];
> > extern int oidParam2[];
> > int print_oid(int *oid, size_t size);
> > ---
> >
> > For example, how can I print oidParam1 with print_oid() calling it from a
> > different module? I couldn't use OID_LENGTH macro (even if I move its
> > declaration to mib.h).
> >
> > Any suggestions?

>
> A couple of things come to mind. First, can you switch from a
> size-based interface to one that uses a sentinel? If there is junk int
> that can never appear in an OID (-1?) putting that at the end would mean
> you could avoid havint to have a length at all.


Like a null-terminating string? This could be a good idea, but I have two
problems with this approach.

On some small machines, I don't use int, but unsigned char to have a small
memory requirements and a good range (0-255). If I'd use *signed* char, I
could use a too small range (0-127), just to have the negative sentinel.
Maybe I can use 255 as a sentinel, so leaving 0-254 values available.

Anyway I have already written many functions (an entire library) that accept
two parameters: the pointer to the array and its length. If I use the sentinel,
I should change them (or write some wrappers).


> Alternatively, you could number (rather than name) the arrays and
> provide access to them and their lengths via other arrays (or via access
> functions).
>
> This can be achieved without data duplication by using some
> pre-processor magic. Write a file oid.h like this:
>
> OID(oidParam1, 1, 1, 3 )
> OID(oidParam2, 1, 2 )
> OID(oidParam3, 2, 1 )
> OID(oidParam4, 2, 2, 5, 4 )
>
> and you can generate the tables like this:
>
> #define OID(name, ...) static const int name[] = { __VA_ARGS__ };
> #include "oid.h"
>
> #undef OID
> const int *const oid[] = {
> #define OID(name, ...) name,
> #include "oid.h"
> };
>
> #undef OID
> const size_t oid_size[] = {
> #define OID(name, ...) sizeof name/sizeof *name,
> #include "oid.h"
> };
>
> Finally, you can make the index constants directly from the same source
> file. This would go in a public header:
>
> #define OID(name, ...) name,
> enum {
> #include "sz.h"
> };
>
> Many alternatives to this scheme are possible. You could obviously
> define a struct that would contain the pointer and length as a single
> object, for example.


Yes, this is good. I like it. I already have used a similar approach for
other purposes.

Thank you for your answer.
 
Reply With Quote
 
Charles Richmond
Guest
Posts: n/a
 
      10-24-2012
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have a module in which I define many const arrays of int of different
> lengths (someone knows SNMP OIDs?).
> In the same module I really use these arrays and the length is calculated
> by
> a simple preprocessor macro (OID_LENGTH).
>
> --- oid.c --
> #define OID_LENGTH(oid) (sizeof(oid) / sizeof(oid[0]))
>
> const int oidParam1[] = { 1, 1, 3 };
> const int oidParam2[] = { 1, 2 };
> const int oidParam1[] = { 2, 1 };
> const int oidParam1[] = { 2, 2, 5, 4 };
>
> int print_oid(int *oid, size_t size) {
> while(size--) {
> printf("%d", *oid++);
> if (size) putchar('.');
> }
> return putchar('\n');
> }
>
> ...
> print_oid(oidParam1, OID_LENGTH(oidParam1));
> ...
> ---
>
> Now the application has grown and I want to create a different module that
> uses the same arrays defined in oid.c. Certainly I can write the
> definition
> in a "oid.h" include as extern, but I can't specify the length.
>
> --- mib.h ---
> extern int oidParam1[];
> extern int oidParam2[];
> int print_oid(int *oid, size_t size);
> ---
>
> For example, how can I print oidParam1 with print_oid() calling it from a
> different module? I couldn't use OID_LENGTH macro (even if I move its
> declaration to mib.h).
>
> Any suggestions?


Suggestion: Create an "oid.h" file that contains the definitions for the
*static* arrays. Then include the file in *both* modules. You end up with
*two* copies of the constant arrays... but you only have to change the
arrays in *one* place (the "oid.h" file) if there is need for them to
change.

--

numerist at aquaporin4 dot com

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-24-2012
On 10/24/2012 6:45 AM, (E-Mail Removed) wrote:
> I have a module in which I define many const arrays of int of different
> lengths (someone knows SNMP OIDs?).
> In the same module I really use these arrays and the length is calculated by
> a simple preprocessor macro (OID_LENGTH).
>
> --- oid.c --
> #define OID_LENGTH(oid) (sizeof(oid) / sizeof(oid[0]))
>
> const int oidParam1[] = { 1, 1, 3 };
> const int oidParam2[] = { 1, 2 };
> [...]
> Any suggestions?


Have you considered packaging both the length and a pointer
in a single struct?

// In a header somewhere:
struct oid {
size_t size;
const int *data;
};
extern const struct oid oidParam1;
extern const struct oid oidParam2;

// In your defining module:
static const int oid1[] = { 1, 1, 3 };
const struct oid oidParam1 = { OID_LENGTH(oid1), oid1 };
static const int oid2[] = { 1, 2 };
const struct oid oidParam2 = { OID_LENGTH(oid2), oid2 };

This could be lipsticked a bit with additional macro magic,
if desired.

--
Eric Sosman
(E-Mail Removed)d
 
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
Defined but not used? But I am using it! Travis C++ 6 06-28-2007 08:23 PM
How can I reference a static array defined in another .cpp file silverburgh.meryl@gmail.com C++ 6 02-18-2007 11:52 PM
how can I access an array defined in another module vabby Perl Misc 6 01-02-2007 11:43 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
how to use static function defined in one file in another file is that impposiible in 'c ' rashmi C Programming 5 04-29-2005 03:26 PM



Advertisments