casting?

This is a discussion on casting? within the C Programming forums, part of the General Programming Boards category; Btw, I want to explicitely 'hide' my structure. Let say I have a structure: Code: typedef struct { void *base; ...

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    casting?

    Btw, I want to explicitely 'hide' my structure.

    Let say I have a structure:
    Code:
    typedef struct
    {
        void *base;
        unsigned short initialSize;
        unsigned short increment;
        unsigned int allocated;
        unsigned int elementSize;
    
    } _MEMORY_POOL, *_PMEMORY_POOL;
    and a typedef:
    Code:
    typedef void *MEMORY_POOL;
    Code:
    void *_pcheck(MEMORY_POOL p, unsigned int offset)
    {
        if(offset >= ((_PMEMORY_POOL)p)->allocated)
            if((((_PMEMORY_POOL)p)->base = realloc(((_PMEMORY_POOL)p)->base, (((_PMEMORY_POOL)p)->allocated = ++((_PMEMORY_POOL)p)->increment * ((_PMEMORY_POOL)p)->initialSize) * ((_PMEMORY_POOL)p)->elementSize)) == NULL)
                abort();
    
        return ((_PMEMORY_POOL)p)->base;
    }
    Is it a good idea to do something like this?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    I would still suggest you typedef MEMORY_POOL to just void and use MEMORY_POOL* to increase the visibility and fact that it is a pointer.
    Pointer typedefs can cause headaches.
    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
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Thanks Elysia.

    Btw, when we do a lot of casting, doesn't that affect performance?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Depends on what you do, where and when. And it also depends on the hardware.
    I wouldn't say it's bad. It's essential to languages such as C.
    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.

  5. #5
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    where and when
    Any clue, please?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I guess you mean casting things to your structure and back? I would be surprised if that made any difference at all; the only thing I can think of that would cause problems is mis-alignment, but that shouldn't ever actually happen. (In other words, I would expect p->base, where p is a _PMEMORY_POOL, and ((_PMEMORY_POOL)p)->base, where p is a void *, to generate the exact same assembly: load p, add the offset for base, and fetch from that memory location.)

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Especially if you try to change sizes of things. That means extra (maybe) instructions or cycles to convert the data.
    I can't really say, though. I'm not an expert on the hardware topic.
    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.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Casting pointers only tells the compiler to interpret data differently. Casts don't actually take a performance hit on their own.
    It's accessing the data in the specified format that could be more or less efficient, but no more so than if it were already of that type.
    Last edited by iMalc; 07-20-2008 at 12:25 AM. Reason: Correction
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Thank you for your attention.

    So, I made a conclusion:
    Code:
    pstructure --> pvoid --> pstructure
    doesn't affect performance at all.

    But...
    Code:
    pstructureA --> pvoid --> pstructureB
    will make a mis-alignment.

    ...

    Out of the box:
    Btw, I think I will migrate to C++...
    I getting problem with data types in C.
    When I tried to implement stack in C, should I make str_stack()?, int_stack()?, char_stack()?, void_stack()?, arrrrrgghhhh... *confuse* I'm going crazy because of void* XP

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by iMalc View Post
    C-style casts, reinterpret_cast, and static_cast only tell the compiler to interpret data differently. They don't actually take a performance hit on their own.
    It's accessing the data in the specified format that could be more or less efficient, but no more so than if it were already of that type.
    Not true for primitive conversions. For example static or C casting an chat to a int will take performance hit, because it has to sign extend the value.

    But this isn't C++, so it's better not to bring C++ terms in. Suffice to say that casting pointers in C will never take a performance hit.

    As an aside: it is not allowed to cast function pointers to non function pointers. Trying to do this may result in a performance hit, assuming you compiler allows you.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by audinue View Post
    Out of the box:
    Btw, I think I will migrate to C++...
    I getting problem with data types in C.
    When I tried to implement stack in C, should I make str_stack()?, int_stack()?, char_stack()?, void_stack()?, arrrrrgghhhh... *confuse* I'm going crazy because of void* XP
    Depends on the complexity and purpose of the stack. Is it merely to work with pointers, then void_stack might do, otherwise you're going to have problems and probably have to duplicate some code or create a devious work-around.
    Suffice to say, C++ can handle this with ease and with more speed than C probably (if you choose to use methods with function pointers, or such).
    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.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    //C99 :
    typedef struct
    {
        void *base;
        unsigned short initialSize;
        unsigned short increment;
        unsigned int allocated;
        unsigned int elementSize;
    
    } MEMORY_POOL_INTERNAL;
    struct MEMORY_POOL;
    void *_pcheck(MEMORY_POOL *p, unsigned int offset)
    {
        MEMORY_POOL_INTERNAL *p_i = (MEMORY_POOL_INTERNAL *) p;
        if(offset >= p_i->allocated)
            if((p_i->base = realloc(p_i->base, (p_i->allocated = ++p_i->increment * p_i->initialSize) * p_i->elementSize)) == NULL)
                abort();
    
        return p_i->base;
    }
    *ahem*
    Last edited by robwhit; 07-19-2008 at 10:57 PM.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Corrected the above for what I was thinking.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Code:
    #define p ((_PMEMORY_POOL)p)
    /*
       ... p-> ...
    */
    #undef p
    :P

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    I hope you were sarcastic, because that is a very horrible hack
    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.

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

Similar Threads

  1. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 12:09 PM
  2. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-18-2007, 12:06 AM
  3. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 01:17 AM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 03:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 06:01 AM

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