2012-06-07 5 views
0

아래 코드는 tmpbuffer의 포인터를 저장하고 있습니다. tmpbuffer 자체를 저장하고 fwBuffer과 같은 배열에 포인터를 저장하지 않고 malloc/free을 사용하지 않으려면 어떻게해야합니까?이 코드에서 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; 

답변

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; 
} 
+0

감사합니다. – user1440367

1
short int *fwBuffer[1000000]; 

이 유형의 short int1000000 포인터 배열이다.
유효한 개체,이 경우에는 short int의 개체에 속하는 유효한 메모리를 가리 키지 않으면 포인터 자체가 유용하지 않습니다.

코드는 short int에 충분한 메모리를 할당 한 다음 해당 포인터를 배열에 배치하므로 배열이 유용합니다. 1000000 항목이 필요하기 때문에이 작업을 수행하는 올바른 방법이며 스택에 할당하면 스택 공간이 부족할 수 있습니다.

관련 문제