Re: Automatically generate variables
- From: jacob navia <jacob@xxxxxxxxxxxxxxxx>
- Date: Wed, 14 Feb 2007 00:25:45 +0100
Mark McIntyre wrote:
On 13 Feb 2007 08:35:59 -0800, in comp.lang.c , "Nate"
<nverbeek@xxxxxxxxxxxxx> wrote:
Hello,
I am looking for a method to automatically declare variables in C.
I'm not sure if there is a good way to do this,
There isn't - you can't define object names at runtime in C.
Wrong
#include <stdio.h>
#include <windows.h>
int main(void)
{
FILE *f = fopen("dynamic.c","w");
void (*fn)(void);
void *ptr;
// 1 Define some structure for instance
fprintf(f,"struct dynamicobject { char *name;int len; };\n");
// 2 Define an object of that type
fprintf(f,"struct dynamicobject var = {\"NoName\",6};\n");
// 3 Define an exported function in a shared object that returns the
// address of the created object
fprintf(f,"void * __declspec(dllexport) \n");
fprintf("GetDynamicObject(void)\n\treturn &var;}\n");
// Done Close the file
fclose(f);
// Compile it. The compiler can change of course :-)
system("lcc dynamic.c");
// Link it into a shared object
system("lcclnk -dll dynamic.obj");
// Open the shared object (dlopen under Unix)
void *h = LoadLibrary("variables.dll");
// Get the address of the created function in the shared object
fn = (void (*fn)(int))GetProcAddress("GetDynamicObject");
// Call the function we just compile
ptr = fn();
// And now, ladies and gentlemen
// Here I have a pointer to a dynamically created object
}
Most of my customers buy lcc-win32 as a "Just in time compiler", that
allows them to do this much more efficiently than what is shown here
A JIT compiler is specialized in generating code dynamically. For instance, if you are modelling molecule interaction you can develop
a special language that is task oriented, compile it to C, then
just JIT compile it into a shared object that you run on the fly.
Of course what you generate is an object, not NAMES...
In this sense the OP is completely wrong of course.
.
- Follow-Ups:
- Re: Automatically generate variables
- From: Mark McIntyre
- Re: Automatically generate variables
- From: Flash Gordon
- Re: Automatically generate variables
- References:
- Automatically generate variables
- From: Nate
- Re: Automatically generate variables
- From: Mark McIntyre
- Automatically generate variables
- Prev by Date: Re: Automatically generate variables
- Next by Date: Re: Automatically generate variables
- Previous by thread: Re: Automatically generate variables
- Next by thread: Re: Automatically generate variables
- Index(es):
Relevant Pages
|