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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:16:38+00:00 2026-05-28T00:16:38+00:00

Consider the following code struct foo { const int txt_len; const int num_len; char

  • 0

Consider the following code

struct foo
{
    const int txt_len;
    const int num_len;

    char * txt;
    int * num;

    foo(int tl, int nl): txt_len(tl), num_len(nl)
    {
        char * tmp = new char[txt_len * sizeof(char) + num_len * sizeof(int)];

        txt = new (tmp) char [txt_len * sizeof(char)];
        num = new (tmp + txt_len * sizeof(char)) int[num_len * sizeof(int)];

        // is this the same as above?
        // txt = tmp;                                  
        // num = (int *) (tmp + txt_len * sizeof(char));
    }

    ~foo()
    {
        delete[] txt; // is this the right way to free the memory?
    }
};

I want *txt and *num to be contiguous, is that the best way to do it?

also is there any difference between placement new and pointer arithmetic? which one should I use?

  • 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-28T00:16:38+00:00Added an answer on May 28, 2026 at 12:16 am

    If you want a contiguous block of memory, you have to allocate it whole with a single call to operator new[] or malloc() or similar. Multiple calls to these functions do not guarantee any contiguity of allocated blocks whatsoever. You may allocate a big block and then carve parts from it as needed.

    And you should delete and free() all blocks previously allocated with new and malloc(), otherwise you’ll leak memory and probably make your program unstable (it will fail to allocate more memory at some point) and exert unnecessary pressure on memory in the OS, possibly slowing down other programs or making them unstable as well.

    Placement new, however, does not actually allocate any memory. It simply constructs an object at the specified location and so you don’t need to free that memory twice.

    One problem that I see in your code is that it doesn’t align ints. On some platforms reading or writing integers bigger than 1 byte from/to the memory must be aligned and if it’s not, you can either read/write values from/to wrong locations or get CPU exceptions leading to termination of your program. The x86 is very permissive in this regard and won’t mind, though may tax you with degraded performance.

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

Sidebar

Related Questions

Consider the following code: struct Foo { mutable int m; template<int Foo::* member> void
Consider the following code: struct Foo { }; struct Bar { explicit Bar(const Foo&)
Please consider the following code, struct foo { foo() { std::cout << Constructing! <<
Consider the following code: #include <iostream> struct foo { // (a): void bar() {
Consider the following code: struct Calc { Calc(const Arg1 & arg1, const Arg2 &
Consider the following code: template <int dim> struct vec { vec normalize(); }; template
please consider the following code: template <typename T> struct foo { template <typename S>
Consider the following code. #include <stdio.h> #include <vector> #include <iostream> struct XYZ { int
Consider the following code template<typename T, int N> struct A { typedef T value_type;
Given the following code: public struct Foo { public Foo(int bar, int baz) :

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.