dynamic type casting 
Author Message
 dynamic type casting

Hi.  I'm trying to figure out a way to dynamically cast pointers.  What
I'm doing now looks like this:

        int crt_node(LIST_TYP typ, void **p)
        {
                if (typ == DTL_LIST)         // for this particular type
                {                            // malloc with an approriate cast and size
                        if ((*p = (DTL *) malloc(sizeof(DTL))) == NULL)
                        {
                                return(ERROR);
                        }
                }
                .
                .  similar for several different types
                .
                return(OK);
        }

What I'd really like to see is something along the lines of

        if ((*p = (TYP_CAST(LIST_TYP) *) malloc(sizeof(TYP_CAST(LIST_TYP)))) == NULL)
        {
                return(ERROR);
        }
        return(OK);

Where TYP_CAST() is some sort of a macro expansion.  Any ideas?
Thanks in advance.

                                                                                                                                                                 Charles



Sun, 27 Apr 1997 03:40:39 GMT  
 dynamic type casting

Quote:

>Hi.  I'm trying to figure out a way to dynamically cast pointers.  What
>I'm doing now looks like this:
>    if ((*p = (DTL *) malloc(sizeof(DTL))) == NULL)
>What I'd really like to see is something along the lines of
>    if ((*p = (TYP_CAST(LIST_TYP) *) malloc(sizeof(TYP_CAST(LIST_TYP)))) == NULL)
>Where TYP_CAST() is some sort of a macro expansion.  Any ideas?
>Thanks in advance.

One idea: Forget about the type cast :-) . You can assign a void
pointer to any pointer type in C. malloc() returns a void pointer.
So?

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




Sun, 27 Apr 1997 17:40:53 GMT  
 dynamic type casting

Quote:
> Hi.  I'm trying to figure out a way to dynamically cast pointers.  What
> I'm doing now looks like this:

    Could you not replace the function crt_node itself with a macro,
    like...

        #define CRT_NODE(typ)   malloc(sizeof(typ))

    Or just call malloc() directly instead of disguising it.

Quote:
> What I'd really like to see is something along the lines of

>    if ((*p = (TYP_CAST(LIST_TYP) *) malloc(sizeof(TYP_CAST(LIST_TYP)))) == NULL)

    I don't believe you can do the first bit, but it's probably not
    necessary anyway because malloc() is supposed to return a void *
    which does not require casting.

    Suppose you define your type values so that they can be used as an
    index into an array of type information. Then you can do something
    like...

        int     typesize[MAXTYPE] = { 2, 4, 2, ... };

        void    *crt_node(type) /*==================================*/
        int     type;
        {
            return( malloc(typesize[type]) );
        }

    The pointer value returned by malloc() can be used instead of your
    OK and ERROR definitions. Note that this is pretty silly code, and
    could easily be replaced with direct calls to malloc() unless you
    need the crt_node structure to fit into a function table someone
    else wrote.

Quote:
>                                                                                                                                                             Charles

--

***             Count Templar, ELITE, Cobra Mk III (FM-287)             ***


Mon, 28 Apr 1997 04:18:20 GMT  
 dynamic type casting

Quote:
>     The pointer value returned by malloc() can be used instead of your
>     OK and ERROR definitions.

It can, yes, but this is seriously dodgy advice.  You should never use a
return value for two purposes like this.  The fact that malloc() does is
a design defect of malloc().

--
Neil Gall                              Hewlett-Packard Ltd

Phone: +44 (0)131 331 7112   / / /_/   South Queensferry
Fax  : +44 (0)131 331 7488      /      Scotland   EH30 9TG



Fri, 02 May 1997 18:46:19 GMT  
 dynamic type casting

Quote:

> >     The pointer value returned by malloc() can be used instead of your
> >     OK and ERROR definitions.
> It can, yes, but this is seriously dodgy advice.  You should never use a
> return value for two purposes like this.  The fact that malloc() does is

    Is it really two purposes? The function is supposed to get a
    pointer. If the pointer is valid after the function is called,
    then it succeeded. If it is NULL, then it failed. I guess it's a
    matter of style, but checking the pointer directly is usually
    faster, I think, and because it involves less "things" and less
    indirection it may appear clearer to some people.

    There are some functions I've seen which return a 0 high word in
    the pointer value to indicate the value is not actually a pointer.
    I think that may be overdoing it, but fopen() and malloc() and so
    on seem to be pretty straightforward.

Quote:
> Neil Gall                              Hewlett-Packard Ltd

--

***             Count Templar, ELITE, Cobra Mk III (FM-287)             ***


Sat, 03 May 1997 17:26:52 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Dynamic Type Casting at Runtime

2. Cast to a dynamic type?

3. dynamic casting (type known at runtime)

4. Storing a type for dynamic casts...

5. Type Casting between Base and Derived Type (Serialization of objects with Type members)

6. type casting to / from pointers to incomplete data types

7. type casting to / from pointers to incomplete data types

8. cast cstring type to integer type

9. Casting to/from STL types to MFC types

10. Why can't overload type cast to base type (object)

11. Strong typing....(static/dynamic typing)

12. Dynamic Casting

 

 
Powered by phpBB® Forum Software