|
Home > Archive > Unix Programming > May 2006 > non-constant initilization in c
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
non-constant initilization in c
|
|
| goldfita@signalsguru.net 2006-05-10, 7:15 pm |
| I'm using gcc, but a standard c solution would be even better. I'm
writing an extension for ruby, and I can't figure out if there's a good
way to initialize a "constant" that's not really contant. So what I
want is basically
//global
int foo = somefunction(konst);
Normally you would just do the assignment in main. The trouble is,
being an extension module, I don't have access to main. I could make a
separate foo for each object and assign the variable at object
creation, but that's ugly, and I would have to pass foo around instead
of making it global.
I also thought about looking at somefunction and hand compiling it, but
then someone might go and do something silly, like change the
implementation.
Is there a way of dealing with this?
| |
| Barry Margolin 2006-05-11, 1:20 am |
| In article <1147298886.222332.212730@g10g2000cwb.googlegroups.com>,
goldfita@signalsguru.net wrote:
> I'm using gcc, but a standard c solution would be even better. I'm
> writing an extension for ruby, and I can't figure out if there's a good
> way to initialize a "constant" that's not really contant. So what I
> want is basically
>
> //global
> int foo = somefunction(konst);
>
> Normally you would just do the assignment in main. The trouble is,
> being an extension module, I don't have access to main. I could make a
> separate foo for each object and assign the variable at object
> creation, but that's ugly, and I would have to pass foo around instead
> of making it global.
>
> I also thought about looking at somefunction and hand compiling it, but
> then someone might go and do something silly, like change the
> implementation.
>
> Is there a way of dealing with this?
I think you can do this in C++, but I don't think it's valid C.
Does Ruby automatically run an initialization function when it loads an
extension module? This would be the place to put the assignment.
If extension modules are shared objects, I think there's a standard way
to make them run an initialization function, but I don't know it offhand.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| goldfita@signalsguru.net 2006-05-11, 1:20 am |
|
Thanks, it's not critical or anything. I don't know enough about Ruby
unfortunately, but it is a shared object.
| |
| Paul Pluzhnikov 2006-05-11, 1:20 am |
| Barry Margolin <barmar@alum.mit.edu> writes:
> In article <1147298886.222332.212730@g10g2000cwb.googlegroups.com>,
> goldfita@signalsguru.net wrote:
>
Gcc-specific solution:
static void init() __attribute__((constructor))
{
foo = somefunction(konst);
}
[vbcol=seagreen]
> If extension modules are shared objects, I think there's a standard way
> to make them run an initialization function, but I don't know it offhand.
The ELF-"standard" solution is to build the DSO with a DT_INIT
dynamic entry with the initialization function as the d_ptr.
But (AFAIK) there is no "standard" C interface for the above,
so compiler-specific hacks are required.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Maxim Yegorushkin 2006-05-11, 1:20 am |
|
goldfita@signalsguru.net wrote:
> I'm using gcc, but a standard c solution would be even better. I'm
> writing an extension for ruby, and I can't figure out if there's a good
> way to initialize a "constant" that's not really contant. So what I
> want is basically
>
> //global
> int foo = somefunction(konst);
>
> Normally you would just do the assignment in main. The trouble is,
> being an extension module, I don't have access to main. I could make a
> separate foo for each object and assign the variable at object
> creation, but that's ugly, and I would have to pass foo around instead
> of making it global.
You can do it this way:
inline int* get_foo()
{
static int foo = somefunction(konst);
return &foo;
};
#define foo (*get_foo())
|
|
|
|
|