2012-04-05 2 views
0

나는 명령을 직렬로 보내고 알 수없는 크기의 응답을받습니다. 사용하기사용자에게 반환 할 함수에 메모리 할당

(ioctl(fd_, FIONREAD, &bytes_in_buffer); 

내가 읽는 데 할당해야하는 메모리 양을 결정합니다.

sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - 
__builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) 
(old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 
* (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && 
((unsigned long)old_end & pagemask) == 0)' failed. 

사람이 어떤 조언을 제공 할 수 :

//This code calls the function below 
unsigned char CheckRefresh[] = {254, 124, 0}; 
unsigned char * response; 
unsigned int size; 
relay_board->SendCustomCommand(CheckRefresh, 3, &response, size); 

ErrorCode SendCustomCommand(unsigned char * command, unsigned int command_size, unsigned char **response, unsigned int &response_size) 
{ 
    //Send the command 
    write(fd_, command, command_size); 

    // ... Omitting Polling Code to Get correct number of bytes ... 
    (ioctl(fd_, FIONREAD, &bytes_in_buffer); 

    //Now getting the response 
    response_size = (unsigned int)bytes_in_buffer; 
    (*response) = new unsigned char(response_size); 
    if(read(fd_, *response, response_size) < 0) 
    { 
    std::cout << "[ProXRSerial] SendCustomCommand: Read failed... -- Errno: " << errno << std::endl; 
    return Failed; 
    }; 
    return Success; 
} 

나는이 다음과 같이

unsigned char * command = new unsigned char(3); 

에서 나의 다음 함수 호출 나누기로 내 스택을 손상이라고 생각? 나는 손실에 빠졌습니다. 이중 포인터를 건네 주면 사용자를위한 메모리를 할당 할 수 있다고 생각합니다 ...

감사합니다.

답변

7

라인

(*response) = new unsigned char(response_size); 

(*response) = new unsigned char[response_size]; 

버전이 부호 문자를 할당하고 값 response_size로 초기화 읽어야합니다.

+0

* facepalm * 고맙습니다. 재미있는 일은 지금까지 나의 모든 다른 코드들이 똑같은 일을하고 있지만, 그 경계에서 접근하고 쓰는 방법이다. 오하이오. – Constantin

+0

'응답 = 새로운 서명되지 않은 문자 [responze_size]; ' – jrok

+0

동일하게 간다'서명되지 않은 문자 * 명령 = 새로운 부호없는 문자 (3);', 아니? – jpm