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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:01:22+00:00 2026-05-23T08:01:22+00:00

EDIT: The main intent is to allow manipulating underlying data as part of an

  • 0

EDIT: The main intent is to allow manipulating underlying data as part of an encapsulated struct as opposed to direct data manipulation.

Which of the following approaches is recommended when it comes to wrapping some data inside a struct:

  1. Keep a pointer to the data within the struct:

    new s(buf), which stores buf in a local field (s->buf = buf)

  2. reinterpret_cast-ing a memory address to a struct:

    reinterpret_cast<s*>(buf)

  3. Use the new operator against the memory address where the data is located:

    new(buf) s;

Here is a sample program for these approaches:

#include <iostream>
using namespace std;

struct s {
    int* i;
    s(int* buf) : i(buf) {}
    int getValue() { return *i * 2; }
};

struct s2 {
    int i;
    int getValue() { return i * 2; }
};

int main() {
    int buf = 10;
    s a(&buf);
    cout << "value: " << a.getValue() << ", size: " << sizeof(a) << ", address: " << &a << ", buf-address: " << &buf << endl;

    s2* a2 = new(&buf) s2;
    cout << "value: " << a2->getValue() << ", size: " << sizeof(*a2) << ", address: " << a2 << ", buf-address: " << &buf << endl;

    s2* a3 = reinterpret_cast<s2*>(&buf);
    cout << "value: " << a3->getValue() << ", size: " << sizeof(*a3) << ", address: " << a3 << ", buf-address: " << &buf << endl;
}

And the output:

value: 20, size: 4, address: 0027F958, buf-address: 0027F964
value: 20, size: 4, address: 0027F964, buf-address: 0027F964
value: 20, size: 4, address: 0027F964, buf-address: 0027F964

Both size & time are important. Also, maintainability is important, e.g. someone might add by mistake a virtual function to s2 (which will mess up the data alignment).

Thanks!

  • 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-23T08:01:23+00:00Added an answer on May 23, 2026 at 8:01 am

    Placement new will still call the constructor, wiping out anything that’s in the buffer already if such a constructor exists (or is created unknowingly in the future) so I don’t think that’s a safe option. reinterpret_cast is undefined behavior even though it may appear to work for you. Storing a local pointer seems to be the best option although you’ve only given a very tiny inkling of what you’re trying to do.

    If you’re attempting serialization here, remember important issues like sizeof(int) and endianness.

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

Sidebar

Related Questions

Ok, i have 2 edit controls and a button in my main window; in
Backdrop: There are two forms, the main application form and a form to edit
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT What small things which are too easy to overlook do I need to
I'm trying to send some String Data from sub activity to main activity and
I start an activity from the main one using a Intent: Intent i =
I am having problems to work with an intent. Debugger crashes at last following
Hi I have an application which after login, loads a main activity which contains
What am I doing: My main intent is to enable user friendly text to
EDIT: I have figured out my main issue, but still have one concern. Everything

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.