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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:36:31+00:00 2026-06-08T15:36:31+00:00

The code below works for me. How could this work? Wouldn’t this segfault? char

  • 0

The code below works for me. How could this work? Wouldn’t this segfault?

char * buffer = new char[100];
float * in_buf = new(buffer) float[100];

Also i got a class like this:

class Item
{
public:
   Item(int num)
   {
     u = new float[num];
     v = new float[num];
   }
   float * u;
   float * v;
   //And many other variables
}

I want to create a block of memory and allocate v and u within them. Is this approach safe?

class Item
{
public:
   Item(int num)
   {
     buffer = new char[(sizeof(char)+2*sizeof(float))*num];
     u = new (buffer) float[num];
     v = new (buffer+sizeof(float)*num) float[num];
   }
   char * buffer;
   float * u;
   float * v;
   //And many other variables
}
  • 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-06-08T15:36:32+00:00Added an answer on June 8, 2026 at 3:36 pm

    The code below works for me. How could this work? Wouldn’t this segfault?

    char* buffer = new char[100];
    float* in_buf = new(buffer) float[100];  
    

    What does “works” mean? As the news here aren’t required to initialise/construct the data types in question, they don’t (necessarily) trigger reads or writes from the memory you’re implicity planning to use for them. So, one reason you may not see a segfault is that the code’s effectively only doing this:

    char* buffer = new char[100];
    float* in_buf = reinterpret_cast<float*>(buffer);
    

    If you move on to read or write ala in_buf[99] = 2;, you’re more likely to get a segfault but far from guaranteed – it could be that the memory at that address is in the applicaton’s virtual address space. For example, say the request for 100 characters was satisfied by a request for one page of memory and the OS page size was >= 4096 bytes – 1000 4-byte floats would coincidentally fit, or because an earlier new and delete had already mapped the memory. Even if it didn’t actually segfault immediately, it’s likely to crash or corrupt the heap or other heap-hosted data one day. More generally, there are plenty of unsafe things you can do in C++ that won’t actually bite immediately….

    is this safe?

    buffer = new char[(sizeof(char)+2*sizeof(float))*num];
    u = new (buffer) float[num];
    v = new (buffer+sizeof(float)*num) float[num];
    

    Yes (assuming sane values of num), but it’s pointlessly complicated (and the extra character’s pointless and actively confusing). You could simply do:

    u = new float[2 * num];
    v = &u[num];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code I have below works fine but I'm sure it could be more
Could anyone help me in assessing why the code below doesn't work. I'm using
The code below works well right now; however, it's serially executed. I'd like to
The code below works well but grabs only the UIView which is VISIBLE on
the code below works in Windows Phone 7 private void ShowTime() { txtTime.Text =
The code below works fine. But it seem clunky. How would you combine the
the code below works fine but it takes an absolute age to run. How
The code below works great. I have a MySQL database that contains book titles
The code below works nicely. Each title in the MySQL table submission is printed
The code below works unless p.School.SchoolName turns out to be null, in which case

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.