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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:23:23+00:00 2026-05-24T01:23:23+00:00

I have a class that looks roughly like this: template<std::size_t dim> class Foo {

  • 0

I have a class that looks roughly like this:

template<std::size_t dim>
class Foo {
public:
    Foo(void const * const data);
private: 
    double vals[dim];
}

For the constructor, I know that void* data points to an array of float values (of dimension dim). Now I would like to initialize the vals-array (preferably) in the initialization list of that constructor.

To make matters worse, the floats pointed to do not necessarily have to be memory-aligned properly.

How could I do this efficiently?


Edit 1

With respect to the discussion taking place below maybe let me state my design priorities first. This might help you to focus on that problems that matter most for me.

  1. Cope with bad memory alignment
  2. Get as few operations as possible in terms of performance.

Honestly, if we need the constructors body to get a fast algorithm working, this is fine for me as well. The focus is on raw power.

  • 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-24T01:23:25+00:00Added an answer on May 24, 2026 at 1:23 am

    If you’re afraid that passed pointer is not aligned to float boundary (for example, file mapping etc), then you can write something like

    template<std::size_t dim>
    class Foo {
    public:
        Foo(void const * const data)
        {
            float temp[dim];
            memcpy( &temp, data, dim * sizeof( float ) );
            std::copy( temp, temp + dim, vals );
        }
    private: 
        double vals[dim];
    }
    

    Possibly an overzealous and not-so-portable solution:

    template<std::size_t dim>
    class Foo {
    public:
        Foo(void const * const data)
        {
            if( static_cast<long>( data ) % sizeof( float ) == 0 ) {
                const float *temp = data;
                std::copy( temp, temp + dim, vals );
            } else {
                float temp[dim];
                memcpy( &temp, data, dim * sizeof( float ) );
                std::copy( temp, temp + dim, vals );
            }
        }
    private: 
        double vals[dim];
    }
    

    Initializing in the initializer list won’t make your code faster, it’s just a convenience when it’s possible.

    If you’re strongly concerned about performance, I would wrap this if in a macro and only use if on the architectures, which require properly aligned access (x86 is not one, it’s just slower on x86).

    Edit

    Another solution proposed in the comments, thanks to
    Steve Jessop. It focuses on reducing the temporary variable size.

    double *dest = vals;
    float tmp;
    void const *first = data;
    void const *last = data + dim * sizeof(float);
    while( first != last ) {
        memcpy( &tmp, first, sizeof(float) );
        first += sizeof(float);
        *dest++ = tmp;
    }
    

    A bit of micro-benchmarking/disassebmlying is possibly needed.

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

Sidebar

Related Questions

I have a class that roughly looks like this: public class ViewModel { public
I have a class that looks like this public class SomeClass { public SomeChildClass[]
I have a class that looks like this: public class TextField : TextBox {
I have a c#-class that looks roughly like this: class ImageContainer { Image image;
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I have a class with a bunch of properties that look like this: public
I have a class with a static method that looks roughly like: class X
I have a ton of repeating code in my class that looks like the

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.