2

处理枚举时是否必须注意内存?这是我声明我的枚举类型的地方。它在另一个 .h 文件中这是我尝试声明变量的地方
在那之后我是否要做类似的事情

// 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
4

2 回答 2

2

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.

于 2012-05-27T09:20:34.967 回答
1

CardType type = SPADE - 在堆栈上分配内存(不需要内存管理)

CardType *type = malloc(sizeof(CardType)) - 在堆上分配内存(提到指针 *),因为显式分配,您负责使用 free(type) 释放内存

于 2012-05-27T09:35:56.757 回答