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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:16:41+00:00 2026-06-13T19:16:41+00:00

Consider the following: #include <vector> using namespace std; struct Vec2 { float m_x; float

  • 0

Consider the following:

#include <vector>
using namespace std;

struct Vec2
{
  float m_x;
  float m_y;
};

vector<Vec2> myArray;

int main()
{
  myArray.resize(100);

  for (int i = 0; i < 100; ++i)
  {
    myArray[i].m_x = (float)(i);
    myArray[i].m_y = (float)(i);
  }

  float* raw;
  raw = reinterpret_cast<float*>(&(myArray[0]));
}

Is raw guaranteed to have 200 contiguous floats with the correct values? That is, does the standard guarantee this?

EDIT: If the above is guaranteed, and if Vec2 has some functions (non-virtual) and a constructor, is the guarantee still there?

NOTE: I realize this is dangerous, in my particular case I have no
choice as I am working with a 3rd party library.

  • 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-13T19:16:42+00:00Added an answer on June 13, 2026 at 7:16 pm

    I realize this is dangerous, in my particular case I have no choice as I am working with a 3rd party library.

    You may add compile time check of structure size:

    live demo

    struct Vec2
    {
        float a;
        float b;
    };
    
    int main()
    {
            int assert_s[ sizeof(Vec2) == 2*sizeof(float) ? 1 : -1 ];
    }
    

    It would increase your confidence of your approach (which is still unsafe due to reinterpret_cast, as mentioned).


    raw = reinterpret_cast(&(myArray[0]));

    ISO C++98 9.2/17:

    A pointer to a POD 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. [ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. —end note ]


    And finally, runtime check of corresponding addresses would make such solution rather safe. It can be done during unit-tests or even at every start of program (on small test array).

    Putting it all together:

    live demo

    #include <vector>
    #include <cassert>
    using namespace std;
    struct Vec2
    {
        float a;
        float b;
    };
    
    int main()
    {
        int assert_s[ sizeof(Vec2) == 2*sizeof(float) ? 1 : -1 ];
        typedef vector<Vec2> Vector;
        Vector v(32);
        float *first=static_cast<float*>(static_cast<void*>(&v[0]));
        for(Vector::size_type i,size=v.size();i!=size;++i)
        {
            assert((first+i*2) == (&(v[i].a)));
            assert((first+i*2+1) == (&(v[i].b)));
        }
        assert(false != false);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
Consider the following code: #include <iostream> using namespace std; int main() { int x,
Consider the following code: #include <vector> using namespace std; struct foo { void bar()
Consider the following code snippet: #include <vector> using namespace std; void sub(vector<int>& vec) {
Consider the following code: #include<iostream> #include<vector> using namespace std; class Foo { public: template<
Consider the following: #include <iostream> #include <cmath> int main() { using std::cout; using std::endl;
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
please consider following code #include <iostream> using namespace std; class Digit { private: int
Consider the following program: #include <iostream> #include <algorithm> using namespace std; template<class T> struct
Consider the following code: #include <iostream> #include <vector> #include <boost/numeric/ublas/vector.hpp> #include <boost/numeric/ublas/io.hpp> int main()

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.