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

  • Home
  • SEARCH
  • 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 8087375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:45:20+00:00 2026-06-05T18:45:20+00:00

Suppose I have the following simple struct: struct Vector3 { double x; double y;

  • 0

Suppose I have the following simple struct:

struct Vector3
{
    double x;
    double y;
    double z;
};

and I create a list of vertices:

std::vector<Vector3> verticesList;

In addition to this I need to use a third-party library. The library has a function with the following signature:

typedef double[3] Real3;
external void createMesh(const Real3* vertices, const size_t verticesCount);

What is the best way to convert verticesList into something which could be passed into createMesh() as the vertices parameter?

At the moment I use the following approach:

static const size_t MAX_VERTICES = 1024;

if (verticesList.size() > MAX_VERTICES)
    throw std::exception("Number of vertices is too big");

Real3 rawVertices[MAX_VERTICES];
for (size_t vertexInd = 0; vertexInd < verticesList.size(); ++vertexInd)
{
    const Vector3& vertex = verticesList[vertexInd];

    rawVertices[vertexInd][0] = vertex.x;
    rawVertices[vertexInd][1] = vertex.y;
    rawVertices[vertexInd][2] = vertex.z;
}

createMesh(rawVertices, verticesList.size());

But surely it is not the best way to solve the issue.

  • 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-06-05T18:45:21+00:00Added an answer on June 5, 2026 at 6:45 pm

    That is one proper way of doing it. There are also some other ways…

    The type Vector3 is layout compatible with the type Real3, the implication of this is that you can force casting a pointer to one type to a pointer of the other:

    createMesh( reinterpret_cast<Real3*>(&verticesList[0]), vertices.size() );
    

    Other alternative, as Rook mentions, to remove the loop is using memcpy, since the types are POD:

    Real3 rawVertices[MAX_VERTICES];
    std::memcpy( rawVertices, &verticesList[0], 
                 vertices.size()*sizeof verticesList[0] );
    

    This is more concise, and probably more efficient, but it still is copying the whole container.


    I believe that the standard does guarantee this behavior (at least C++11), two standard layout and standard compatible types have the same memory layout (duh?), and §9.2p19 states:

    A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa.

    This guarantee technically means something slightly different than what I claimed before: you can reinterpret_cast<double*>(&verticesList[0]) points to verticesList[0].x. But it also implies that the conversion from double* to Real3 pointer through reinterpret cast will also be fine.

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

Sidebar

Related Questions

Suppose I have some simple struct like this: public struct WeightedInt { public int
Suppose I have the following (trivially simple) base class: public class Simple { public
Lets suppose that I have the following simple query var q = from p
Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ...
Suppose I have a list of things (numbers, to keep things simple here) and
Very simple question: suppose I have the following js/jquery code doSomething(); $.get(url, callback); doSomethingElse();
Let's suppose I have the following table (let's call it my_table ): CREATE TABLE
Suppose we have a simple database containing following data: name apple pear banana grape
Suppose you have a simple block of code like this: app.get('/', function(req, res){ res.send('Hello
Suppose I have the following struct: struct sampleData { int x; int y; };

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.