Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6061867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:59:10+00:00 2026-05-23T08:59:10+00:00

I am using vector<char> to send and receive data via socket. In this vector

  • 0

I am using vector<char> to send and receive data via socket. In this vector I stored data of different types. Unsigned Integer and Doubles.
To decode the data from vector I am using copy function.

vector<char> myVector = ... smth;
double value = 0.0;
copy(myVector.begin(), myVector.begin() + sizeof(value), &value);

It works with Integer without problem. But…

My problem is, that the compile gives out an error "free(): invalid pointer: 0x00000000006d0e30". I checked, the problem is with the double value, not with the vector. I looked the address of double value it was (0x6d0e38). Why the program tries to access the pointer backwards?
I would be glad, if you can say me, what I am doing wrong. And is it the good way to decode message?

Thank you a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:59:11+00:00Added an answer on May 23, 2026 at 8:59 am

    It works with Integer without problem. But…

    It most certainly will not work for integers. At least not for integers where sizeof(int) > 1! Because it will not write to just one integer, but spread the bytes in myVector over sizeof(T) integers, thus overwriting random memory. (see nightcracker’s answer)

    Please just use memcpy for this kind of copying:

    vector<char> myVector = ... smth;
    double value = 0.0;
    assert(myVector.size() == sizeof(double));
    memcpy(&value, &myVector[0], std::min(myVector.size(), sizeof(double)));
    // as an alternative to the assert + std::min() above, you could also throw
    // an exception if myVector.size() == sizeof(double) does not hold.
    // (that's what I'd do if the size should always match exactly)
    

    memcpy is made exactly for that kind of thing (copying raw memory), and I see no reason to use anything else here. Using std::copy does not make it better C++, especially when you’re not doing it correctly. std::copy is for copying objects, not raw memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a client that sends me data using this function: void CServerRetrieve::Send(char *buf,
So far, I have this code sample: ... int nbytes =0; vector<unsigned char> buffer;
Why this code gives error: #include<iostream> #include<vector> using namespace std; int main() { char
I'm reading from a boost::asio::ip::udp::socket like this: using boost::asio::ip::udp; // ... char recv_buf[128]; udp::endpoint
My TcpClient class accepts vector<char> in its SendData method like this: void CTcpClient::SendData(const vector<char>&
I have a vector like this: std::vector<std::vector<char> > _lines; I would like to iterate
I'm working with activemq-cpp and I am attempting to send binary data using BytesMessage.
Is iterating through the vector using an iterator and copying to a list the
I want to clear a element from a vector using the erase method. But
Having a vector containing pointers to objects then using the clear function doesn't call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.