run time type checking 
Author Message
 run time type checking

Am writing a generic function which will return pointers to void of
the form.
test(void **_void_pntr)
{      
        if (blah blah)  
                *_void_ptr=&_float;
        else
                *_void_ptr=&_char;

Quote:
}

main()
{
        float * _float1
        char * _char1;

        test(&_float1);
        printf("%f", *(float*)_float1);
        test(&_char1);
        printf("%c", *(char*)_char);

Quote:
}

My worry is the lack of type checking. Ibelieve it is possible to do
some run-time type checking via the use of macros.
My dodgey memory seems to recall a 'typeof' call but my VC++ (No
access to a unix box at the moment curse it all) doesn't seem to know
about it and seems to infer thst it's a VC++ extension.
Please reply if you have any suggestions ( deadline looming). I'm also
cut off from email so you'll have to post.
Regards,
Vim


Fri, 12 Nov 1999 03:00:00 GMT  
 run time type checking

Quote:

>Am writing a generic function which will return pointers to void of
>the form.
>test(void **_void_pntr)
>{  
>    if (blah blah)  
>            *_void_ptr=&_float;
>    else
>            *_void_ptr=&_char;
>}
>main()
>{
>    float * _float1
>    char * _char1;
>    test(&_float1);
>    printf("%f", *(float*)_float1);
>    test(&_char1);
>    printf("%c", *(char*)_char);
>}
>My worry is the lack of type checking. Ibelieve it is possible to do
>some run-time type checking via the use of macros.

Type checking should not be your only worry. Have a look at the
FAQ called "Can I use a void ** pointer to pass a generic pointer
to a function by reference?". In a world where all pointers are
created equal (and have equal representation), this may not be
a problem.

Quote:
>My dodgey memory seems to recall a 'typeof' call but my VC++ (No
>access to a unix box at the moment curse it all) doesn't seem to know
>about it and seems to infer thst it's a VC++ extension.

What would typeof return, anyway? The inference that typeof is
a VC++ extension from the fact that VC++ does not know about
typeof is somewhat bewildering.

Kurt

--
| Kurt Watzka                             Phone : +49-89-2180-6254



Fri, 12 Nov 1999 03:00:00 GMT  
 run time type checking




Quote:
>Am writing a generic function which will return pointers to void of
>the form.
>test(void **_void_pntr)
>{  
>    if (blah blah)  
>            *_void_ptr=&_float;
>    else
>            *_void_ptr=&_char;

You should avoid using identifiers that begin with underscores. They are
reserved.

Quote:
>}
>main()
>{
>    float * _float1
>    char * _char1;

>    test(&_float1);

You have a problem here. The function expects a  (void **), and you are
giving it a (float **). These two pointers are not assignment compatible,
so your compiler should diagnose a constraint violation here.

Note that putting a cast here won't correct the problem, because _float1
is not a pointer to void. What you are trying to do is reinterpret its
contents as though it were a pointer to void, resulting in undefined behavior.

Assuming that you have a function test which produces a void * that points
to storage of suitable alignment and quantity to hold a float value, you
can do this:

        void *returned_pointer;
        float *converted_pointer;

        test(&returned_pointer);            /* get the void * out   */
        converted_pointer = returned_pointer;   /* convert to float *   */

Quote:
>    printf("%f", *(float*)_float1);

Since _float1 is already a pointer to float, casting it to pointer to float
is redundant.

Quote:
>    test(&_char1);
>    printf("%c", *(char*)_char);
>}

>My worry is the lack of type checking. Ibelieve it is possible to do

My worry is your compiler if it produced no diagnostics for the above program!

Quote:
>some run-time type checking via the use of macros.
>My dodgey memory seems to recall a 'typeof' call but my VC++ (No
>access to a unix box at the moment curse it all) doesn't seem to know
>about it and seems to infer thst it's a VC++ extension.

There is no typeof operator in C. It appears as an extension in some
compilers. But what it commonly does is construct a type expression whose
type is the same as the declared type of the object to which typeof is applied
to. It is not a run time type identification mechanism.

You will probably need to pass the type as an explicit extra argument to the
test function. Also, the use of the function will be greatly simplified
if you return the void * value instead. For example:

    #include <assert.h>
    #include <stdio.h>

    enum obj_type { integer, floating, character };

    void *test(enum obj_type type)
    {
        static int i = 1;
        static float f = 2.0;
        static char c = 'x';

        switch (type) {
        case integer:
            return &i;
        case floating:
            return &f;
        case character:
            return &c;
        default:
            assert(0);  /* type has invalid value */
        }
    }

    int main(void)
    {
        float *fl = test(floating);
        printf("%f\n", *fl);
        return 0;
    }



Fri, 12 Nov 1999 03:00:00 GMT  
 run time type checking



Quote:
>Am writing a generic function which will return pointers to void of
>the form.
>test(void **_void_pntr)

It is a good idea always to include return types for function. Since this
doesn't appear to return a value make that:

void test(void **_void_pntr)

Quote:
>{      
>        if (blah blah)  
>                *_void_ptr=&_float;
>        else
>                *_void_ptr=&_char;
>}

What are _float and _char here? Identifiers that begin with an underscore are
reserved at file scope. Since you haven't defined them within a function
there is no way these can be valid. It is generally best to avoid identifiers
with leading underscores.

Assuming that these represent objects of type float and char note that the
assignments here cause pointers to these to be converted to void * and
it is those void * pointers that are being written.

Quote:
>main()

Better written as:

int main(void)

Quote:
>{
>        float * _float1
>        char * _char1;

>        test(&_float1);

Here you are trying to pass a pointer to a pointer to float to test where
it requires a pointer to pointer to void. This is invalid in C. To correct
this define _float1 with type void *.

Quote:
>        printf("%f", *(float*)_float1);

With that change the cast here now makes sense (it was redundant before).

Quote:
>        test(&_char1);
>        printf("%c", *(char*)_char);

Similarly here. It might be better to write:

void *test(void)

{      
        if (blah blah)  
                return &_float;
        else
                return &_char;

Quote:
}

which allows you to assign the returned void * value directly to the
appropriate type of pointer in the caller.

Quote:
>}

>My worry is the lack of type checking.

What type checking do you want?

Quote:
>Ibelieve it is possible to do
>some run-time type checking via the use of macros.

C doesn't use any runtime type information, it has a static type system
which is resolved at compile time. Macros are a source code construct.

Quote:
>My dodgey memory seems to recall a 'typeof' call but my VC++ (No
>access to a unix box at the moment curse it all) doesn't seem to know
>about it and seems to infer thst it's a VC++ extension.

Probably, there is no typeof operator in the C language. Some compilers
do define it as an extension but from what I've seen it is still a source
level operation.

Quote:
>Please reply if you have any suggestions ( deadline looming). I'm also
>cut off from email so you'll have to post.

What is it that you are trying to guard against? Certainly void * can
create a bit of a hole in type checking but generally you just have to be
aware of it and be careful. In your example you have a test on (blah blah)
which determines which pointer is used. There is no portable way to
validate that against the type of arguments passed to the function (since
by the time the function sees them they are all void ** anyway).

--
-----------------------------------------


-----------------------------------------



Sat, 13 Nov 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Run-time type checking

2. Determining type at run-time or compile-time

3. turning off run time checks

4. Run-Time Check Failure #3

5. Run-Time Check Failure #2

6. Re : Strange run-time check failure

7. Strange run-time check failure

8. Run-Time Check Failure in atlimage.h

9. Run-Time Check Failure #3

10. Checking for Programming Errors at Run Time

11. Run-time checking in C

12. Run-time Checks for C

 

 
Powered by phpBB® Forum Software