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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:08:50+00:00 2026-05-15T18:08:50+00:00

struct Vector { float x, y, z; }; func(Vector *vectors) {…} usage: load float

  • 0
struct Vector 
{
   float x, y, z;
};

func(Vector *vectors) {...}

usage:
load float *coords = load(file);
func(coords);

I have a question about the alignment of structures in C++. I will pass a set of points to the function func(). Is is OK to do it in the way shown above, or is this relying on platform-dependent behavior? (it works at least with my current compiler) Can somebody recommend a good article on the topic?

Or, is it better to directly create a set of points while loading the data from the file?

Thanks

  • 1 1 Answer
  • 1 View
  • 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-15T18:08:51+00:00Added an answer on May 15, 2026 at 6:08 pm

    Structure alignment is implementation-dependent. However, most compilers give you a way of specifying that a structure should be “packed” (that is, arranged in memory with no padding bytes between fields). For example:

    struct Vector {
        float x;
        float y;
        float z;
    } __attribute__((__packed__));
    

    The above code will cause the gcc compiler to pack the structure in memory, making it easier to dump to a file and read back in later. The exact way to do this may be different for your compiler (details should be in your compiler’s manual).

    I always list members of packed structures on separate lines in order to be clear about the order in which they should appear. For most compilers this should be equivalent to float x, y, z; but I’m not certain if that is implementation-dependent behavior or not. To be safe, I would use one declaration per line.

    If you are reading the data from a file, you need to validate the data before passing it to func. No amount of data alignment enforcement will make up for a lack of input validation.

    Edit:

    After further reading your code, I understand more what you are trying to do. You have a structure that contains three float values, and you are accessing it with a float* as if it were an array of floats. This is very bad practice. You don’t know what kind of padding that your compiler might be using at the beginning or end of your structure. Even with a packed structure, it’s not safe to treat the structure like an array. If an array is what you want, then use an array. The safest way is to read the data out of the file, store it into a new object of type struct Vector, and pass that to func. If func is defined to take a struct Vector* as an argument and your compiler is allowing you to pass a float* without griping, then this is indeed implementation-dependent behavior that you should not rely on.

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

Sidebar

Related Questions

I have got file with vector of : typedef struct { float fX; ///<
What I have: Vectors of different custom structs(one custom struct per vector) I used
Let's I have struct Vector { float i,j,k; } I want to zero all
I have two vector classes: typedef struct D3DXVECTOR3 { FLOAT x; FLOAT y; FLOAT
I have a struct that looks like this: typedef struct _my_struct { float first_vector[SOME_NUM][OTHER_NUM];
I have the following code: #include <iostream> #include <vector> using namespace std; struct A{};
struct Vector { float i,j,k; } std::vector pt[size]; ... = ... + pt[temp]; temp
This is my code. struct Vector { float x, y, z, w; }; typedef
I'm making a vector/matrix library. (GCC, ARM NEON, iPhone) typedef struct{ float v[4]; }
I have structure like this: struct Rz3DContourNode { float x; float y; float z;

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.