문제

Do I have to take care of memory when I deal with the enum ? This is where I declared my enum type. It is in another .h file This is where I attempt to declare variable
After that Do I have o do something like that

// This is where I declared my enum type. It is in another .h file

    typedef enum CardTypes
    {
        HEART = 1,
        DIAMOND =2,
        CLUB =3,
        SPADE = 4

    } CardType;

    // This is where I attempt to declare variable  

    CardType cardType=SPADE;

    //or

    CardType cardType=malloc(size(CardType));

    // After that Do I have o do something like that

    [cardType release]

    //or

     free(&card)

     Any help will be appreciated , thanks
도움이 되었습니까?

해결책

This is just a basic C type - and it's handled just like an int in this regard.

This is automatic:

CardType cardType=SPADE;

But when you find you must use malloc, then you would need to free it.

Typically (e.g. parameter, ivar, local variable), you would declare the enum by value, but (like int) you may occasionally need to use malloc+free.

다른 팁

CardType type = SPADE - Allocates memory on stack (no memory management needed)

CardType *type = malloc(sizeof(CardType)) - allocates memory on heap (mention a pointer *), because of explicit alloction you are responsible of freeing memory using free(type)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top