方法之间的第一个数据包传递指针(Obj-C) [英] Pass Pointer to First Packet Between Methods (Obj-C)

查看:103
本文介绍了方法之间的第一个数据包传递指针(Obj-C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里丢失了一些东西,但是我不确定如何解决它.此作品的第一个版本:

I'm missing something here, but I'm not sure how to fix it. The first version of this works:

- (void) sendBytes:(const UInt8*)bytes size:(UInt32)size
{
    Byte packetBuffer[size+100];
    MIDIPacketList *packetList = (MIDIPacketList*)packetBuffer;
    MIDIPacket     *packet     = MIDIPacketListInit(packetList);
    MIDIPacketListAdd(packetList, sizeof(packetBuffer), packet, 0, size, bytes);
    [self sendPacketList:packetList];
}

对于DRYness,我尝试从创建数据包列表中创建一种方法:

For DRYness, I try to make a method out of the packet list creation:

- (MIDIPacketList*) makePacketList:(const UInt8*)data size:(UInt32)size
{
    Byte packetBuffer[size+100];
    MIDIPacketList *packetList = (MIDIPacketList*)packetBuffer;
    MIDIPacket     *packet     = MIDIPacketListInit(packetList);
    MIDIPacketListAdd(packetList, sizeof(packetBuffer), packet, 0, size, data);
    return packetList;
}


- (void) sendBytes:(const UInt8*)bytes size:(UInt32)size
{
    MIDIPacketList *packetList = [self makePacketList:bytes size:size];
    [self sendPacketList:packetList];
}

现在,sendPacketList方法失败,并显示EXC_BAD_ACCESS.使用GDB,即使在sendPacketList ...

And now the sendPacketList method fails with an EXC_BAD_ACCESS. Using GDB, the packetList still looks good even within sendPacketList...

查看文档,似乎我要传递的内容只是指向列表中第一个数据包的指针.所以...我该怎么做?

Looking at the docs, it seems that the thing I'm passing around is just a pointer to the first packet in the list. So... how can I do this?

推荐答案

问题在于Byte packetBuffer[size+100]声明了本地数组,该数组在该方法退出后不能再访问.您有两个选择(我将其写为函数):

The trouble is that Byte packetBuffer[size+100] declares a local array, which must not be accessed after that method exits. You have two options (which I'll write as functions):

MIDIPacketList *makePacketList(const UInt8 *data, UInt32 size) {
    Byte *packetBuffer = malloc(size + 100);
    MIDIPacketList *packetList = (MIDIPacketList*)packetBuffer;
    MIDIPacket     *packet     = MIDIPacketListInit(packetList);
    MIDIPacketListAdd(packetList, sizeof(packetBuffer), packet, 0, size, data);
    return packetList;
}

如果这样做,则稍后必须free()缓冲区,这很痛苦.

If you do it this way, you'll have to free() the buffer later on, which is kind of a pain.

MIDIPacketList *makePacketList(Byte *packetBuffer, const UInt8 *data, UInt32 size) {
    MIDIPacketList *packetList = (MIDIPacketList*)packetBuffer;
    MIDIPacket     *packet     = MIDIPacketListInit(packetList);
    MIDIPacketListAdd(packetList, size + 100, packet, 0, size, data);
    return packetList;
}

在这种情况下,您必须在函数外部声明Byte packetBuffer[size + 100]并将其作为第一个参数传递,这也有些不方便.

In this case, you'll have to declare the Byte packetBuffer[size + 100] outside of the function and pass it in as the first argument, which is also somewhat inconvenient.

这篇关于方法之间的第一个数据包传递指针(Obj-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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