0

下面的代码是存储一个指针tmpbuffer。我将如何将tmpbuffer自身而不是指针存储在数组中fwBuffer而不是使用malloc/ free

short int *fwBuffer[1000000];

size =  sizeof(short int)*length*inchannels;
short int *tmpbuffer = (short int*)malloc(size);

int count = 0;
for (count = 0; count < length*inchannels; count++)
{
    tmpbuffer[count] = (short int) (inbuffer[count]);
}

fwBuffer[saveBufferCount] = tmpbuffer;
4

2 回答 2

1
short int *fwBuffer[1000000];

1000000是一个类型的指针数组short int
一个指针本身是没有用的,除非它指向一些有效的内存,它属于一个有效的对象,在这种情况下是一个类型的对象short int

代码为 a 分配了足够的内存short int,然后将该指针放入数组中,从而使数组变得有用。这是执行此操作的正确方法,因为您需要1000000项目,如果您在堆栈上分配它们,您可能会用完堆栈空间。

于 2012-06-07T03:14:16.207 回答
0

你想做这样的事情吗?这会将所有存储的缓冲区聚合到一个缓冲区中。请注意,我确实有一个单独的缓冲区来存储索引;要么你可能不需要这个,要么理论上你可以将它打包到 fwBuffer 数组中的一个位置。

// Max number of data chunks
const unsigned maxBuffers = 1024;
// All the data stored here.
short int fwBuffer[1000000];
// how many data chunks we have
unsigned saveBufferCount = 0;
// Index to find each data chunk
// bufferIndex[saveBufferCount] points to where the next buffer will be placed (i.e. _after_ all the stored data).
short int* bufferIndex[maxBuffers] = {fwBuffer};

void storeBuffer(unsigned length, unsigned inchannels, short int* inbuffer)
{    
        short int *bufferIterator = bufferIndex[saveBufferCount];
        // Could do a memcpy here.
        int count = 0;
        for (count = 0; count < length*inchannels; count++)
        {
            *bufferIterator++ = inbuffer[count];
        }       
        ++saveBufferCount;
        bufferIndex[saveBufferCount] = bufferIterator;
}
于 2012-06-07T03:45:16.357 回答