I would like to copy 4 bytes from unsigned int to unsigned char array. Once executed the following function get_result
goes to segmentation fault :
int exec_cmd(unsigned int * apu32Var)
{
int ret = -1;
char cmd[100] = { 0 };
char resp[100] = { 0 };
sprintf(cmd, "%s %s", "/home/send_frames.sh", "read");
ret = exec_cmd_ret_result(cmd, resp);
if( apu32Var != NULL )
{
*apu32Var = (((unsigned int)resp[0]) <<24)+(((unsigned int)resp[1]) <<16)+(((unsigned int)resp[2]) <<8)+(unsigned int)resp[3];
}
return ret;
}
int get_result(unsigned char * buffer, unsigned short * size)
{
unsigned int u32Var = 0;
exec_cmd(&u32Var);
memcpy(buffer, &u32Var, sizeof(unsigned int));
*size += sizeof(unsigned int);
return 0;
}
int main(int argc, char **argv)
{
unsigned char *buf;
unsigned short *size;
get_result(buf+4, size);
return 0;
}
However, regarding to memcpy() man page it seems memcpy() are well managed. What is going wrong ?
Test_result
is called. And what does your debugger tell you? – Jabberwockybuf
being used uninitialized inmain()
? – Jens