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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:21:13+00:00 2026-05-19T22:21:13+00:00

OK first and foremost, performance is most important here so I doubt a map

  • 0

OK first and foremost, performance is most important here so I doubt a map would work. I have a list of structs (about 16 of them) like

struct A { ... };
struct B { ... }; 
...

each are different and each are of different sizes.

I’m wondering what elegant way we might be able to do something along the lines of:

char BufferA[sizeof(struct A)];
char BufferB[sizeof(struct B)];

then write some method or mapping to return BufferA if you are working with struct A. Speed is definitely the most important, I imagine using templates would help but I’m not sure it the whole thing can be templatized.

Update*** Sorry for not being clear, the buffers are all pre-allocated. I just need a very fast way to get the proper Buffer given a struct type.

Update 2*** Sorry for not specifying, alignment is an important trait here and I do in fact byte-align each struct with #pragma pack(push, 1)

  • 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-19T22:21:14+00:00Added an answer on May 19, 2026 at 10:21 pm
    template<typename X>
    struct Buffer
    {
        static char *ptr()
        {
            // Note if no alignment is needed for your use then
            // just a simple "static char buf[sizeof(X)]; return buf;"
            // would be sufficient instead of the following.
            union Aligner {
                X x;
                char buf[sizeof(X)];
            };
    
            static Aligner a;
    
            return a.buf;
        }
    };
    
    struct B
    {
        int x, y, z;
    };
    
    void foo()
    {
        Buffer<B>::ptr()[2] = 12;
    }
    

    With g++ -O2 the code above generates just a fixed memory write operation in foo.

    .globl _Z3foov
        .type   _Z3foov, @function
    _Z3foov:
    .LFB1:
        .cfi_startproc
        .cfi_personality 0x0,__gxx_personality_v0
        pushl   %ebp
        .cfi_def_cfa_offset 8
        movl    %esp, %ebp
        .cfi_offset 5, -8
        .cfi_def_cfa_register 5
    
        movb    $12, _ZZN6BufferI1BE3ptrEvE1a+2   <=== this is the assignment
    
        popl    %ebp
        ret
        .cfi_endproc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First and foremost, apologies for any cross-posting. Hope I'm not repeating an issue here,
THE EXAMPLE First and foremost, here's my code and problem: http://www.nathanstpierre.com/MBX/showoff.html THE ISSUE So
First and foremost, I apologize for any vagueness in this question. At this point,
First and foremost let me state that I know that accessing server side controls
First and foremost, I do not know RegEx but am trying to piece something
First and foremost. I'm totally new to programming in Excel. I'm trying to make
First and foremost thank you for checking out my problem, and for any help
I am first and foremost a perl coder, but like many, also code in
i recently switched to mac. first and foremost i installed xampp. then for django-python-mysql
I am working on my first big Actionscript 3 website and trying to decide

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.