0

Well this might be a very weird question but my curiosity has striken pretty hard on this. So here it goes...

NOTE: Lets take the language C into consideration here.

As programmers we usually define a user-defined datatype(say struct) in the source code with the appropriate name.
Suppose I have a program in which I have a structure defined as:

struct Animal {
  char *name;
  int lifeSpan;
};

And also I have started the execution of this program.
Now, my question here is;
What if I want to define a new structure called "Plant" just like "Animal" mentioned above in my program, without writing its definition in the source code itself(which is obviously impossible currently) but rather from a user input string(or a file input) during runtime.
Lets say my program takes input string from a text file named file1.txt whose content is:

struct Plant {
  char *name;
  int lifeSpan;
};

What I want now is to have a new structure named "Plant" in my program which is already in execution. The program should read the file content and create a structure as written in the file and attach it to itself on-the-go.

I have checked out a solution for C++ in the discussion Declaring a data type dynamically in C++ but it doesnt seem to have a very convincing solution.

The solution I am looking for is at the compiler-linker-loader level rather than from the language itself.
I would be very pleased and thankful if anyone is looking forward to sharing their ideas on this.

i_rigia
  • 86
  • 8
  • 4
    An extreme example would seem to be something like a python interpreter written in C. You could probably dream up some type of c interpreter but that sounds a lot like a compiler. What are you try to do? – user1593858 Jul 10 '18 at 23:09
  • [possibly useful](https://stackoverflow.com/questions/8696653/dynamically-load-a-function-from-a-dll) – jaggedSpire Jul 10 '18 at 23:09
  • 1
    Any purely-C++ solution would be quite complicated. It's easier - but not easy - to embed another, scripting language into your program, Lua or Python or whatever. Google "Lua C interface" or "Python C interface". – Victor Sergienko Jul 10 '18 at 23:09
  • 1
    How would your program refer to and use the struct it read in from file? Without knowing what it contains how can you write code that uses it? – Galik Jul 10 '18 at 23:10
  • 1
    A database comes close to providing a dynamic data type. Interpreters could probably apply JIT compilation to achieve something close. C/C++ projects can embed interpreters. Dynamically loaded objects can be used to extend an executables functionality at runtime, but the object has to be compiled before being loaded. – jxh Jul 10 '18 at 23:10
  • No, this is fantasy. – Paul Sanders Jul 10 '18 at 23:12
  • 2
    Is it possible in any language - sure. Is it possible directly in C or C++, no. –  Jul 10 '18 at 23:12
  • To echo some of the other comments -- it's strictly speaking possible but best case, the means to that end are self-defeating. – 0xdd Jul 10 '18 at 23:25
  • 1
    Thanks for all your suggestions guys. By the way, regarding python/Lua/etc interpreter in C, the interpreter is the executing program and the source code(python or any) is the input file passed to it. But to my best knowledge, I guess that if we define a datatype in python(say) then that datatype's metadata is just a data structure(something like- struct PyObject{}) already defined in the python interpreter's code. Here we are not making any changes in the metadata of the interpreter program itself. I guess I would somewhat agree with @Myst's answer below. – i_rigia Jul 11 '18 at 00:38
  • @i_rigia: yes, that's right. Unless you do run-time code generation (i.e. JIT-compile some new machine code that has the new data types hard coded into it) the code that's actually running is only what you originally compiled. Everything it does is just manipulating data. – Peter Cordes Jul 11 '18 at 06:42
  • Please explain what C syntax would allow your program to refer to a type name that has not been defined nor declared. If you agree that the new type will only be defined for the new code that is also loaded with it, there will be possible solutions in C. – jxh Jul 14 '18 at 01:35

1 Answers1

1

What you're asking about is basically "can we implement C as a scripting language?", since this is the only way code can be executed after compilation.

I'm aware that people have been writing (mostly in the comments) that it's possible in other languages but isn't possible in C, since C is a compiled language (hence data types should be defined during compile time).

However, to the best of my knowledge it's actually possible (and might not be as hard as one would imagine).

There are many possible approaches (machine code emulation (VM), JIT compilation, etc').

One approach will use a C compiler to compile the C script as an external dynamic library (.dll on windows, .so on linux, etc') and than "load" the compiled library and execute the code (this is pretty much the JIT compilation approach, for lazy people).


EDIT:

As mentioned in the comments, by using this approach, the new type is loaded as part of an external library.

The original code won't know about this new type, only the new code (or library) will be "aware" of this new type and able to properly use it.

On the other hand, I'm not sure why you're insisting on the need to use static types and a compiler-linker-loader level solution.

The language itself (the C language) can manage this task dynamically (during execution time).

Consider Ruby MRI, for example. The Ruby language supports dynamic types that can be defined during runtime...

...However, this is implemented in C and it's possible to use the code from within C to define new modules and classes. These aren't static types that can be tested during compilation (type creation and identification is performed during runtime).

This is a perfect example showing that C (as a language) can dynamically define "types".

However, this is also a poor example because Ruby's approach is slow. A custom approved can be far faster since it would avoid the huge overhead related to functionality you might not need (such as inheritance).

Myst
  • 18,516
  • 2
  • 45
  • 67
  • Thanks for ur suggestion. But one small query here... is it possible to link and load a .dll or .so when the program is in execution. I hope it is. – i_rigia Jul 11 '18 at 00:42
  • 5
    @i_rigia: It's possible to load a shared object (DLL) at runtime, but the program loading it must know about the types it will encounter to be able to use the loaded shared object. You'd have to write some system to describe the new, previously unknown type, and provide access functions to the data, and so on. You'd not be able to assign values or compare values or do anything with the values. You couldn't even dereference the pointers meaningfully — you'd have to coerce them to some other type and then play with fire. So, in practice, the answer is "No". – Jonathan Leffler Jul 11 '18 at 00:52
  • 1
    @i_rigia, as mentioned by Jonathan Leffler, you'd be able to load the `dll` / `so`, but the original code (the program, not the loaded library) will still have no knowledge of the new type. My answer assumes the new type is either used by the new code or "opaque". – Myst Jul 11 '18 at 02:25
  • Hmmm...So I guess its not possible as desired. – i_rigia Jul 11 '18 at 09:38
  • But what about a concept like linux modules? There we can write a whole new C code which may include new custom datatypes and still link and load it over the in-execution kernel program and still be able to execute the module on-the-go. How that works? Or maybe am I missing a point here? – i_rigia Jul 11 '18 at 09:45
  • @i_rigia - I'm not sure I understand the question in the comment. When using kernel modules, the core kernel that loaded the module has no knowledge of any new types. The loaded module uses the new types it declares (either internally or allowing user access using pre-defined headers) - not the original kernel. – Myst Jul 11 '18 at 13:47
  • Oh ok. I get u. So the kernel has no knowledge of the types defined in our module ryt? – i_rigia Jul 11 '18 at 16:24
  • 1
    @i_rigia - yap, that's right. The pre-compiled code (the kernel) doesn't have pre-knowledge of what types the future code (the module) might define... But still, they "communicate" with each other using pre-designed callbacks, functions and/or types as needed. – Myst Jul 11 '18 at 19:01
  • So I guess we have to come up with an OS design that has this weird feature. I mean more enhanced feature than kernel modules or DLLs. And somehow I feel that the solution for this problem is quite possible. hehe!!! – i_rigia Jul 11 '18 at 20:40
  • @i_rigia lol .... I'm curious, why insist of static types instead of using a dynamic type scheme? I mean, it's quite easy to implements a dynamic type scheme in C. Even a flexible dynamic type system that can be translated to JSON isn't that hard (I coded one for facil.io, so I know) and the price in performance doesn't have to be that high (depending on the required features)... so, why not? – Myst Jul 11 '18 at 20:58
  • @Myst Yeah! But I guess to have a dynamically typed language the only solution currently available is to have an interpreter. If Im not wrong! – i_rigia Jul 13 '18 at 08:55
  • @i_rigia , I updated my answer with the comments. Note that you don't need a dynamically typed language. It's possible to author dynamic types with C. As for an interpreter, than by definition the answer is "yes". Whenever you read external code and parse it, you are in fact implementing an interpreter. You don't have to use an existing one. You can author your own. For example, you could convert JSON data to dynamic type definitions using your own interpreter. – Myst Jul 13 '18 at 14:58