释放使用malloc分配的内存的最佳方法 [英] Best way to release memory allocated using malloc

查看:210
本文介绍了释放使用malloc分配的内存的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有将整数转换为字节数组的功能(对于iPhone).为了增加动态性,我已经使用malloc分配了数组.但是我认为这会泄漏内存.什么是管理此内存的最佳方法,

I have function to convert an integer into byte array (for iPhone). To add dynamicity I have allocate the array using malloc. But I think this will leak memory. What's best way to manage this memory,

+ (unsigned char *) intToByteArray:(int)num{
    unsigned char * arr = (unsigned char *) 
                          malloc(sizeof(num) * sizeof(unsigned char));
    for (int i = sizeof(num) - 1 ; i >= 0; i --) {
        arr[i] = num & 0xFF;
        num = num >> 8;
    }
    return arr;
}

打电话时

int x = 500;
unsigned char * bytes = [Util intToByteArray:x];

我想避免调用free(bytes),因为调用函数不知道或不明确知道,内存已分配但未释放.

I want to avoid the call free(bytes) since, the calling function do not know or explicitly knows, the memory is allocated and not freed.

推荐答案

几件事:

  1. 根据定义,char类型(以及signed charunsigned char)的大小都为1,因此sizeof(unsigned char)是不必要的.

  1. The char type (and signed char and unsigned char) all have a size of 1 by definition, so sizeof(unsigned char) is unnecessary.

看起来您只想获取int对象的字节表示,如果是这种情况,则不必为其分配更多空间,只需获取int的地址即可并将其转换为指向unsigned char *的指针.如果字节顺序错误,则可以使用NSSwapInt函数交换int中字节的顺序,然后获取地址并转换为unsigned char *.例如:

It looks like you just want to get the byte representation of an int object, if this is the case, it is not necessary to allocate more space for it, simply take the address of the int and cast it to a pointer to unsigned char *. If the byte order is wrong you can use the NSSwapInt function to swap the order of the bytes in the int and then take the address and cast to unsigned char *. For example:

int someInt = 0x12345678;
unsigned char *bytes = (unsigned char *) &someInt;

此强制转换是合法的,并且从bytes读取是合法的,直到读取sizeof(int)个字节为止.这是在访问对象表示形式".

This cast is legal and reading from bytes is legal up until sizeof(int) bytes are read. This is accessing the “object representation”.

如果您坚持使用malloc,则只需在完成后将缓冲区传递给free,如下所示:

If you insist on using malloc, then you simply need to pass the buffer to free when you are done, as in:

free(bytes);

  • 您的方法名称暗含对返回缓冲区的正确所有权.如果您的方法返回调用者负责释放的内容,则通常使用newcopy或有时使用create命名该方法.更合适的名称是copyBytesFromInt:或类似的名称.否则,您可以让该方法接受预分配的缓冲区并调用方法getBytes:fromInt:,例如:

  • The name of your method does not imply the correct ownership of the returned buffer. If your method returns something that the caller is responsible for freeing, it is conventional to name the method using new, copy, or sometimes create. A more suitable name would be copyBytesFromInt: or something similar. Otherwise you could have the method accept a pre-allocated buffer and call the method getBytes:fromInt:, for example:

    + (void) getBytes:(unsigned char *) bytes fromInt:(int) num
    {
        for (int i = sizeof(num) - 1 ; i >= 0; i --) {
            bytes[i] = num & 0xFF;
            num = num >> 8;
        }
    }
    

  • 您可以将bytes包装到NSData实例中:

  • You could wrap your bytes into a NSData instance:

    NSData *data = [NSData dataWithBytesNoCopy:bytes length:sizeof(num) freeWhenDone:YES];
    

    确保您的方法遵循通常的对象所有权规则.

    Make sure your method follows the usual object ownership rules.

    这篇关于释放使用malloc分配的内存的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆