memcpy, not understanding something
-
Windows 7, Visual Studio 2008, MFC, C++ A memset is not doing what I expect. Here is the relevant code. It is a received TCP/IP payload packet and the goal is to copy some of the data to a new location. m_carry_over_bytes has the expected value of 1200. All the code surrounding the memcpy is just to look at the before and after.
m_prev_header_packet_size = mp_search_pointer->iads_structure.header.packet_size;
m_prev_header_sequence_number = mp_search_pointer->iads_structure.header.sequence_number;
m_prev_header_packets_sent = mp_search_pointer->iads_structure.header.packets_sent;
m_prev_header_packets_lost = mp_search_pointer->iads_structure.header.loss_or_overflow;
m_prev_header_dummy_0_5555 = mp_search_pointer->iads_structure.header.dummy[0];
m_prev_header_dummy_1_6666 = mp_search_pointer->iads_structure.header.dummy[1];
m_prev_header_dummy_2_7777 = mp_search_pointer->iads_structure.header.dummy[2];
m_prev_header_dummy_3_ffff = mp_search_pointer->iads_structure.header.dummy[3];memcpy( &m_carry_over_buffer,
&mp_search_pointer,
m_carry_over_bytes );m_curr_header_packet_size = m_carry_over_buffer.iads_structure.header.packet_size;
m_curr_header_sequence_number = m_carry_over_buffer.iads_structure.header.sequence_number;
m_curr_header_packets_sent = m_carry_over_buffer.iads_structure.header.packets_sent;
m_curr_header_packets_lost = m_carry_over_buffer.iads_structure.header.loss_or_overflow;
m_curr_header_dummy_0_5555 = m_carry_over_buffer.iads_structure.header.dummy[0];
m_curr_header_dummy_1_6666 = m_carry_over_buffer.iads_structure.header.dummy[1];
m_curr_header_dummy_2_7777 = m_carry_over_buffer.iads_structure.header.dummy[2];
m_curr_header_dummy_3_ffff = m_carry_over_buffer.iads_structure.header.dummy[3];Looking at the m_prev* variables I find the expected values: size is 1228, sequence_number is 62316, ... dummy 2 is 0x7777, etc After the memcpy, the m_curr* values are expected to have the same values ad the m_prev* values. Instead they have all Fs, the value it was initialized to in the constructor. Both the source and destination work back to this structure:
//
union RECEIVE_TYPE
{
char char_array [ C_TCP_MAX_RECEIVE_SIZE ];
C_IADS_TD_FIXED_PARAMETER_PACKET iads_structure;
} m_receive_buffer;I expected this memcpy to be simple. Where am I not understanding it?
-
Windows 7, Visual Studio 2008, MFC, C++ A memset is not doing what I expect. Here is the relevant code. It is a received TCP/IP payload packet and the goal is to copy some of the data to a new location. m_carry_over_bytes has the expected value of 1200. All the code surrounding the memcpy is just to look at the before and after.
m_prev_header_packet_size = mp_search_pointer->iads_structure.header.packet_size;
m_prev_header_sequence_number = mp_search_pointer->iads_structure.header.sequence_number;
m_prev_header_packets_sent = mp_search_pointer->iads_structure.header.packets_sent;
m_prev_header_packets_lost = mp_search_pointer->iads_structure.header.loss_or_overflow;
m_prev_header_dummy_0_5555 = mp_search_pointer->iads_structure.header.dummy[0];
m_prev_header_dummy_1_6666 = mp_search_pointer->iads_structure.header.dummy[1];
m_prev_header_dummy_2_7777 = mp_search_pointer->iads_structure.header.dummy[2];
m_prev_header_dummy_3_ffff = mp_search_pointer->iads_structure.header.dummy[3];memcpy( &m_carry_over_buffer,
&mp_search_pointer,
m_carry_over_bytes );m_curr_header_packet_size = m_carry_over_buffer.iads_structure.header.packet_size;
m_curr_header_sequence_number = m_carry_over_buffer.iads_structure.header.sequence_number;
m_curr_header_packets_sent = m_carry_over_buffer.iads_structure.header.packets_sent;
m_curr_header_packets_lost = m_carry_over_buffer.iads_structure.header.loss_or_overflow;
m_curr_header_dummy_0_5555 = m_carry_over_buffer.iads_structure.header.dummy[0];
m_curr_header_dummy_1_6666 = m_carry_over_buffer.iads_structure.header.dummy[1];
m_curr_header_dummy_2_7777 = m_carry_over_buffer.iads_structure.header.dummy[2];
m_curr_header_dummy_3_ffff = m_carry_over_buffer.iads_structure.header.dummy[3];Looking at the m_prev* variables I find the expected values: size is 1228, sequence_number is 62316, ... dummy 2 is 0x7777, etc After the memcpy, the m_curr* values are expected to have the same values ad the m_prev* values. Instead they have all Fs, the value it was initialized to in the constructor. Both the source and destination work back to this structure:
//
union RECEIVE_TYPE
{
char char_array [ C_TCP_MAX_RECEIVE_SIZE ];
C_IADS_TD_FIXED_PARAMETER_PACKET iads_structure;
} m_receive_buffer;I expected this memcpy to be simple. Where am I not understanding it?
-
You haven't shown all of the structures involved, but perhaps the memcpy() should be:
memcpy( &m_carry_over_buffer,
mp_search_pointer,
m_carry_over_bytes );-- Harvey
H.Brydon, You got it right. I was forgetting that mp_search_pointer was a pointer to start with. Still, I don't understand why it did not copy something into m_carry_over_buffer Just for completeness they are declared as:
RECEIVE_TYPE *mp_search_pointer;
RECEIVE_TYPE m_carry_over_buffer;Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
Windows 7, Visual Studio 2008, MFC, C++ A memset is not doing what I expect. Here is the relevant code. It is a received TCP/IP payload packet and the goal is to copy some of the data to a new location. m_carry_over_bytes has the expected value of 1200. All the code surrounding the memcpy is just to look at the before and after.
m_prev_header_packet_size = mp_search_pointer->iads_structure.header.packet_size;
m_prev_header_sequence_number = mp_search_pointer->iads_structure.header.sequence_number;
m_prev_header_packets_sent = mp_search_pointer->iads_structure.header.packets_sent;
m_prev_header_packets_lost = mp_search_pointer->iads_structure.header.loss_or_overflow;
m_prev_header_dummy_0_5555 = mp_search_pointer->iads_structure.header.dummy[0];
m_prev_header_dummy_1_6666 = mp_search_pointer->iads_structure.header.dummy[1];
m_prev_header_dummy_2_7777 = mp_search_pointer->iads_structure.header.dummy[2];
m_prev_header_dummy_3_ffff = mp_search_pointer->iads_structure.header.dummy[3];memcpy( &m_carry_over_buffer,
&mp_search_pointer,
m_carry_over_bytes );m_curr_header_packet_size = m_carry_over_buffer.iads_structure.header.packet_size;
m_curr_header_sequence_number = m_carry_over_buffer.iads_structure.header.sequence_number;
m_curr_header_packets_sent = m_carry_over_buffer.iads_structure.header.packets_sent;
m_curr_header_packets_lost = m_carry_over_buffer.iads_structure.header.loss_or_overflow;
m_curr_header_dummy_0_5555 = m_carry_over_buffer.iads_structure.header.dummy[0];
m_curr_header_dummy_1_6666 = m_carry_over_buffer.iads_structure.header.dummy[1];
m_curr_header_dummy_2_7777 = m_carry_over_buffer.iads_structure.header.dummy[2];
m_curr_header_dummy_3_ffff = m_carry_over_buffer.iads_structure.header.dummy[3];Looking at the m_prev* variables I find the expected values: size is 1228, sequence_number is 62316, ... dummy 2 is 0x7777, etc After the memcpy, the m_curr* values are expected to have the same values ad the m_prev* values. Instead they have all Fs, the value it was initialized to in the constructor. Both the source and destination work back to this structure:
//
union RECEIVE_TYPE
{
char char_array [ C_TCP_MAX_RECEIVE_SIZE ];
C_IADS_TD_FIXED_PARAMETER_PACKET iads_structure;
} m_receive_buffer;I expected this memcpy to be simple. Where am I not understanding it?
-
You are specifying the source address as
&mp_search_pointer
which is the address of the pointer, rather than the pointer itself. I suspect that is not what you intended.Use the best guess
Richard MacCutchan wrote:
You are specifying the source address as
&mp_search_pointer
which is the address of the pointer, rather than the pointer itself. I suspect that is not what you intended.Exactly right. The address of the pointer gives you a block of memory just 4-bytes long, containing a memory address. The pointer itself gives you the allocated memory you're looking for.