runtime type identification

This is a discussion on runtime type identification within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to modify some code I made to be more convenient to test. The program has several initial ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    knoxville, TN, US
    Posts
    2

    runtime type identification

    Hello,

    I'm trying to modify some code I made to be more convenient to test. The program has several initial conditions which are defined at the beginning, such as vector length, memory offset, and type. Each time I compile the code, I modify the initial conditions to do a specific test, so I want to make the code require as few manual changes as possible. There is also a variable for initial value of the variable, but depending on the type, this must be different. So far I haven't found anything that would work.

    Code:
    #define N_INIT          0
    #define NTIMES          1000
    #define OFFSET_INIT     0
    #define OFFSET_STEP     0
    #define THREAD_INIT     128
    #define THREAD_STEP     0
    #define ITER            700
    
            /*      settings for test       */
    static int N = N_INIT;          // initial vector length
    float _type;                    // used for type of array element
    static typeof(_type) elem_init = 2.f;   // if _type is float    **********************
    //static typeof(_type) elem_init = 2.f; // if _type is double
    //static typeof(_type) elem_init = 'a';  // if _type is char
    
    static int OFFSET = OFFSET_INIT;
    static int THREADS = THREAD_INIT;
    static int OPERATION = OPERATION_SET;

    On the line with the string of asterisks, I would like to test the type of _type at runtime so I can choose to initialize it as 2.f if it is a float or double and 'a' if it is a char.
    Essentially, I would like to be able to do something like:

    if( typeof(_type) == (float) ) elem_init = 2.f;
    elseif( typeof(_type) == (char) ) elem_init = 'a';

    Is anything like this possible in C?

    thanks,
    nads

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    No built-in way, no.
    You'd have to create code to simulate it. Like some field in a struct that describes what kind of type you're working with.
    Additionally, you could easily create some code in C++ to do this type of stuff.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Perhaps conditional compilation, where the type of _type can be coerced at compile time by a compiler directive, as in
    Code:
    #if TYPE == f
        /* set it to float */
        ...
    #endif

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You could do something like:
    Code:
    #include <stdio.h>
    
    #define TYPE_INT 1
    #define TYPE_FLOAT 2
    
    struct element
    {
        int type;
        union
        {   
            int i;
            float f;
            /* Add any other types you want here */
        } value;
    };
    
    void print_element(struct element* e)
    {
        switch(e->type)
        {   
            case TYPE_INT:
                printf("%d\n", e->value.i);
                break;
            case TYPE_FLOAT:
                printf("%f\n", e->value.f);
                break;
        }   
    }
    
    int main(void)
    {
        struct element e1, e2; 
        e1.type = TYPE_INT;
        e1.value.i = 10; 
    
        e2.type = TYPE_FLOAT;
        e2.value.f = 4.5f;
    
        print_element(&e1);
        print_element(&e2);
        return 0;
    }

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Bithub, you beat me to the punch, my first thought was to make use of a union as well.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    knoxville, TN, US
    Posts
    2
    itCbitC: thanks, that should work.

    Elysia, bithub: thanks, but I'd like to avoid structs, unions, and C++ to keep everything as lean as possible. The purpose of changing data types is to test the difference in speed of operations of different type;

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Well, as long as the provided solutions are to your liking, then away you go
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  3. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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