Runtime formation and execution at runtime

This is a discussion on Runtime formation and execution at runtime within the C Programming forums, part of the General Programming Boards category; Is there any way we can form and execute a statement at runtime in C?...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    PA, USA
    Posts
    11

    Runtime formation and execution at runtime

    Is there any way we can form and execute a statement at runtime in C?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, yes and no. You can't form a C expression and run it, but you could generate code at runtime - there is also no standard way of doing that, it changes both with processor architecture and OS architecture how you go about this.

    What are you actually trying to do? As in, do you have a particular problem you are trying to solve, and if so, can you explain what the problem is?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    PA, USA
    Posts
    11
    There are three integer variables x1, x2 and x3 having values 1,2 and 3 respectively. Another fourth array variable x contains either x1, x2 or x3. I want to print the value of the variable which is the value of x. e.g. if x contains x2, I want to display 2. (Of course this is a simplification of a bigger program).

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sounds like all you need is a pointer to the right variable [somehow you must know which variable you want printed somewhere, right?]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    PA, USA
    Posts
    11
    Yes.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So I take it that solved the problem?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    PA, USA
    Posts
    11
    Not really! I was thinking of this in a larger perspective. For this small scale problem, we can use if or swich statement or a program of the following kind. But I wanted to know whether there is any other technique through which we can form a statement and execute it at runtime.


    Code:
    #define GET_VAL(x) (strcmp(x, "x1") == 0) ? x1 : ( \
                                    (strcmp(x, "x2") == 0) ? x2 : ( \
                                        (strcmp(x, "x3") == 0) ? x3 : 0))
    main()
    {
        int x1 = 1;
        int x2 = 2;
        int x3 = 3;
        char x[20];
    
        printf("Enter value of x ");
        scanf("%s", x);
    
        printf("Value is %d\n", GET_VAL(x));
    }


    Thanks

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could use an array of ints instead of 3 different ints, then just take an index number instead of a variable name from scanf(). Or if the user really feels the need to put x before the number, just strip off the x from the input, convert the rest to an int and use it as the array index.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Location
    PA, USA
    Posts
    11
    That also will work. But my original point is different. I wanted to know whether there is any other technique through which we can form a statement and execute it at runtime.

    Thanks

    Soham

  10. #10
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Sounds like what you want to do is dynamically create/reference variable names.

    But I am pretty sure that is impossible.

    edit: or maybe you can with macros which I have little experience with

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Quote Originally Posted by Soham View Post
    That also will work. But my original point is different. I wanted to know whether there is any other technique through which we can form a statement and execute it at runtime.

    Thanks

    Soham
    Yes - you can write a calculator - standard learning task
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    So do you want the user to be able to enter something like this:
    Code:
    sprintf( str, "a = %d", i );
    and have your program compile and run that code, or do you just want simple arithmetic statements?
    If you want to write your own C shell, it'll probably be pretty damned hard & time consuming.

  13. #13
    Registered User
    Join Date
    Aug 2008
    Location
    PA, USA
    Posts
    11
    I want to execute it also. I think it is not that easy. I wanted something like Dynamic SQL from Oracle.

    ANyways, thanks.

  14. #14
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    You could generate temp .c file and compile/execute it... or maybe there's some "library" for compiling code...
    EDIT: or compile into shared library, and dlopen() it in current context...

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, are you writing something like a calculator/expression parser/spreadsheet application?

    If so, you probably should consider implementing a "variable storage".

    If you want a way to use/get variables out of your application for debug purposes or some such, you could add some code to store a name and variable, e.g.

    Code:
    #define ADD_VAR(t, x) add_var(#t, #x, &x,)
    #define REMOVE_VAR(x) remove_var(#x)
    
    void add_var(const char *type, const char *name, void *addr)
    {
       // Store type, name and addr in some sort of stack/list/array/etc. 
    }
    
    void remove_var(const char *name)
    {
        // remove the last added variable of that name. 
    }
    
    
    const char *get_fmt(const char *type)
    {
        const static struct {
           char *type;
           char *fmt;
        } table[] = 
       {
           { "int", "%d" },
           { "char *", "%s" },
           { NULL, NULL }
       };
       int i;
    
       for(i = 0; table[i].type; i++)
       {
          if(strcmp(type, table[i].type) == 0)
             return table[i].fmt;
       }
       return NULL;
    }
    
    void print_var(const char *name)
    {
       // Find name in list. 
       if (found)
       {
          const char *fmt = getfmt(found->type);
          char buffer[100];
          if (!fmt) printf("Unknown format %s\n", found->type);
          else
          {
            sprintf(buffer, "%s = %s\n", name, fmt);
            printf(buffer, *found->addr);
          }
       }
       else
       {
           printf("Could not find variable\n");
       }
    }
    
    
    int main()
    {
       int x = 1;
       int y = 7;
       char *str = "foo";
       char *name;
    
       ADD_VAR(int, x);
       ADD_VAR(int, y);
       ADD_VAR(char *, str);
    
       scanf("%s", name);
       print_var(name);
    
       REMOVE_VAR(str);
       REMOVE_VAR(y);
       REMOVE_VAR(x);
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21