2012-04-05 7 views
0

私はシリアルでコマンドを送信し、未知のサイズの応答を受け取ります。使用するユーザーに返す関数にメモリを割り当てる

(ioctl(fd_, FIONREAD, &bytes_in_buffer); 

私は自分の読書のために割り当てる必要があるメモリを決定します。

//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); 

で私の次の関数呼び出しの区切りとして、私のスタックを破壊されて信じている:

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. 

誰でもいくつかのアドバイスを提供してもらえますか?私は迷っています。そのようなダブルポインタを渡すことで、ユーザーにメモリを割り当てることができると思いました...

ありがとうございます。

答えて

7

ライン

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

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

をお読みくださいあなたのバージョンが符号なし文字を割り当て、その値response_sizeで初期化します。

+0

* facepalm *ありがとうございました。面白いことはこれまでのところ私の他のすべてのコードが同じことをしていて、まだその境界からアクセスして書き出しています...ああ、ヒープです。 – Constantin

+0

'応答=新しい署名されていないchar [responsze_size];' – jrok

+0

と同じ行く 'unsigned char * command =新しいunsigned char(3);'、いいえ? – jpm

関連する問題