Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. memcpy, not understanding something

memcpy, not understanding something

Scheduled Pinned Locked Moved ATL / WTL / STL
c++csharpvisual-studioquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bkelly13
    wrote on last edited by
    #1

    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?

    H L 2 Replies Last reply
    0
    • B bkelly13

      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?

      H Offline
      H Offline
      H Brydon
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • H H Brydon

        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

        B Offline
        B Offline
        bkelly13
        wrote on last edited by
        #3

        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/

        1 Reply Last reply
        0
        • B bkelly13

          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?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • L Lost User

            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

            D Offline
            D Offline
            Dale Burr
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups