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 7497363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:04:08+00:00 2026-05-29T19:04:08+00:00

When I create a matrix using Eigen, like this: Eigen::MatrixXd M(3,3); M<< 1.0, 4.0,

  • 0

When I create a matrix using Eigen, like this:

  Eigen::MatrixXd M(3,3);
  M<< 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 ;
  std::cout<<M<<std::endl;

produces

1 4 7
2 5 8
3 6 9

I can walk with a pointer through the data, printing each element:

  double * d = M.data();
  for(int i = 0;i<M.size();i++)
  {
    std::cout<<*d<<" ";
    d++;
  }
  std::cout<<std::endl;

produces

1 2 3 4 5 6 7 8 9

I can also use std::copy to copy it to an array of the same type on the stack, then print the elements of that array:

  double data_copy[9];
  std::copy(M.data(),M.data()+M.size(),data_copy);
  for(int i = 0;i<M.size();i++)
  {
    std::cout<<data_copy[i]<<" ";
  }
  std::cout<<std::endl;

produces

1 2 3 4 5 6 7 8 9

However, I can’t seem to use memcpy to do the equivalent copy. This only manages to copy the first element:

  double data_memcopy[9];
  memcpy(data_memcopy,M.data(),M.size());
  for(int i = 0;i<M.size();i++)
  {
    std::cout<<data_memcopy[i]<<" ";
  }
  std::cout<<std::endl;

produces

1 0 0 0 0 0 0 0 0

Any good reason why memcpy is not doing what I expect it should? (Or am I using it improperly?)

  • 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-29T19:04:10+00:00Added an answer on May 29, 2026 at 7:04 pm

    You need to multiply by the size of a double:

    memcpy(data_memcopy,M.data(),M.size() * sizeof(double));
    

    Otherwise, you are only copying M.size() bytes, and each double is more than one byte on your machine.

    So you were probably only writing to the first and second doubles (they probably are 8 bytes on your system, since you copied the first one successfully, and the second double you only probably copied one byte which was likely zeros).

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

Sidebar

Related Questions

I want to create images like this from a double precision matrix using MATLAB.
Using JavaScript, how do I create an HTML table that can "accept" numeric matrix
I am trying to create a matrix to look like this: [1 x x^2
I know that using the Insert menu, you can create a matrix with vertical
I am trying to create a sortable image matrix, 5x5 using scriptaculous javascript library
How can I create a const boost matrix? The following did not work: const
I am trying to create a Matrix class using c++. So far, I have
I want to create a matrix representation for a graph algorithm using the least
I was wondering how to create a matrix and compute its inverse using SymPy
I'm using R to create a heatmap from a matrix using heatmap.2 - and

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.