Вопрос

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