简体   繁体   中英

How do you allocate memory at a predetermined location?

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?

It is a datatype or a typedef.

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 typedef ed (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 char s.

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 int s 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM