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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:28:25+00:00 2026-05-18T21:28:25+00:00

I’m working on a message-passing runtime system that has existing message allocation code that

  • 0

I’m working on a message-passing runtime system that has existing message allocation code that looks like this when a messages contain variable-length data:

struct MsgBase {
  void* operator new(size_t obj_size, int arr1_size, int arr2_size);
};
struct Msg : MsgBase {
  double *arr1;
  double *arr2;
};
struct MsgBase {
  void* operator new(size_t obj_size, int arr1_size, int arr2_size) {
    size_t offsets[2];
    offsets[0] = obj_size;
    offsets[1] = obj_size + sizeof(double)*arr1_size;
    Msg *m = (Msg *)malloc(offsets[1] + sizeof(double)*arr2_size);
    m->arr1 = (char*)m + sizeof(double)*arr1_size;
    m->arr1 = (char*)m->arr1 + sizeof(double)*arr2_size;
    return m;
  }
};

For hardware-interface reasons, the message has to be allocated as one big buffer, and copying into such a buffer after the fact would kill performance1.

And (lots of) client code that does

Msg *msg = new (12, 17) Msg;
msg->arr1[6] = 543.43;

The problem we’ve just encountered is that g++ 4.4 (unlike earlier versions) is zeroing out sizeof(Msg) bytes starting at msg before the pointer is returned to us, so those offset pointers into the buffer are not preserved. Thus, the second line of code results in a segfault.

The zeroing doesn’t happen if we declare a constructor Msg::Msg(), but my intuition is that the compiler would be within its rights to zero out the allocation before calling the constructor anyway.

  • So, is my intuition correct, that code like this has no real assurance of working, even if we explicitly declare the constructor?

  • Assuming that the code above won’t continue to work, do I have any hope to preserve the client interface? How would the replacement for this look?


Notes: the MsgBase class and attendant operator new() are generated from a client-provided interface definition that says something like

message Msg {
  double arr1[];
  double arr2[];
};

While the client code is responsible for defining Msg itself and ensuring that it inherits from MsgBase. So, we can change anything about MsgBase, but pretty much nothing about Msg without forcing that on existing application code.

  1. Don’t tell me to go profile that. We’ve benchmarked this heavily (we run on several of the top 10 from the top500), and are trying to be zero-copy wherever we can. We currently make no copies here, and regressions would suck.
  • 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-18T21:28:26+00:00Added an answer on May 18, 2026 at 9:28 pm

    You’re right that anything written into the memory by operator new is not guaranteed to be preserved. So no, you can’t retain the current broken call syntax.

    Use a factory function and the library-provided placement new. Something like this:

    struct Msg
    {
      double* const arr1;
      double* const arr2;
    private:
      Msg(double* p1, double* p2) : arr1(p1), arr2(p2) {}
      Msg(const Msg&); // deleted copy-constructor
      // having const members prevents assignment operator from being implicitly generated
    public:
      static Msg* Create( size_t arr1_len, size_t arr2_len )
      {
          void* raw = ::operator new(sizeof (Msg) + (1 + arr1_len + arr2_len) * sizeof (double));
          // note ugly math to properly align double, assumes sizeof (double) is a power of 2
          // consider using alignof (double) instead of sizeof (double) if your compiler supports it
          double* p = reinterpret_cast<double*>((reinterpret_cast<intptr_t>(raw) + sizeof (Msg) + sizeof (double)) & ~(sizeof (double) - 1));
          return new (raw) Msg(p, p + arr1_len);
      }
    };
    

    NOTE: You could possibly retain the current syntax by using thread-local variables and a constructor… basically your custom operator new would put either the pointers or the sizes into the TLS, then the constructor would read the TLS and set the pointers appropriately. I think I would pass the sizes, since the compiler might be asking operator new for a little extra memory and stuffing debugging information in front of the actual object.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I want use html5's new tag to play a wav file (currently only supported

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.