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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:35:55+00:00 2026-06-02T00:35:55+00:00

How do i allocate memory using new at a fixed location? My book says

  • 0

How do i allocate memory using new at a fixed location? My book says to do this:

char *buf=new char[sizeof(sample)];
sample *p=new(buf)sample(10,20);

Here new is allocating memory at buf’s address, and (10,20) are values being passed. But what is sample? is it an address or a data type?

  • 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-02T00:35:57+00:00Added an answer on June 2, 2026 at 12:35 am

    let me explain this code to you…

    char *buf=new char[sizeof(sample)];
    sample *p=new(buf)sample(10,20);
    

    This is really four lines of code, written as two for your convenience. Let me just expand them

    char *buf;                        // 1
    buf = new char[sizeof(sample)];   // 2
    sample *p;                        // 3
    p = new(buf)sample(10,20);        // 4
    

    Line 1 and 3 are simple to explain, they are both declaring pointers. buf is a pointer to a char, p is a pointer to a sample. Now, we can not see what sample is, but we can assume that it is either a class defined else where, or some of data type that has been typedefed (more or less just given a new name) but either way, sample can be thought as a data type just link int or string

    Line 2 is a allocating a block of memory and assigning it our char pointer called buf. Lets say sample was a class that contains 2 ints, this means it is (under most compilers) going to be 8 bytes (4 per int). And so buf points to the start of a block of memory that has been set aside to hold chars.

    Line 4 is where it gets a big complex. if it where just p = new sample(10,20) it would be a simple case of creating a new object of type sample, passing it the two ints and storing the address of this new object in the pointer p. The addition of the (buf) is basically telling new to make use of the memory pointed to by buf.

    The end effect is, you have one block of memory allocated (more or less 8 bytes) and it has two pointers pointing to it. One of the points, buf, is looking at that memory as 8 chars, that other, p, is looking at is a single sample.

    Why would you do this?

    Normally, you wouldn’t. Modern C++ has made the sue of new rather redundant, there are many better ways to deal with objects. I guess that main reason for using this method, is if for some reason you want to keep a pool of memory allocated, as it can take time to get large blocks of memory and you might be able to save your self some time.

    For the most part, if you think you need to do something like this, you are trying to solve the wrong thing

    A Bit Extra

    I do not have much experience with embedded or mobile devices, but I have never seen this used.

    The code you posted is basically the same as just doing sample *p = new sample(10,20) neither method is controlling where the sample object is created.

    Also consider that you do not always need to create objects dynamically using new.

    void myFunction(){
        sample p = sample(10,20);
    }
    

    This automatically creates a sample object for you. This method is much more preferable as it is easier to read and understand and you do not need to worry about deleting the object, it will be cleaned up for you when the function returns.

    If you really do need to make use of dynamic objects, consider use of smart pointers, something like unique_ptr<sample> This will give you the ability to use dynamic object creation but save you the hassle of manual deleting the object of type sample (I can point you towards more info on this if you life)

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

Sidebar

Related Questions

Is it possible to allocate an arbitrary memory block using the new operator? In
I want to understand better the difference between using 'new' to allocate memory for
I knew that when we allocate memory by using new/new[], then we should release
We allocate memory in C using malloc and in C++ using new. I know
int *p=(int * )malloc(sizeof(int)); delete p; When we allocate memory using malloc then we
I currently allocate my memory for arrays using the MS specific mm_malloc. I align
How do I allocate memory for a char variable (not a char pointer) inside
We know that malloc() and new operation allocate memory from heap dynamically, but where
Which is preferable way to allocate memory for a function that is frequently allocating
When using malloc to allocate memory, is it generally quicker to do multiple mallocs

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.