简体   繁体   English

你如何在预定位置分配 memory?

[英]How do you allocate memory at a predetermined location?

How do i allocate memory using new at a fixed location?如何在固定位置使用 new 分配 memory? 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.这里 new 在 buf 的地址分配 memory,(10,20) 是传递的值。 But what is sample?但什么是样本? is it an address or a data type?它是地址还是数据类型?

It is a datatype or a typedef.它是一种数据类型或 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.第1行和第3行解释起来很简单,都是在声明指针。 buf is a pointer to a char , p is a pointer to a sample . buf是指向char的指针, p是指向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现在,我们看不到sample是什么,但我们可以假设它是在其他地方定义的 class,或者一些已经被typedef编辑的数据类型(或多或少只是给了一个新名称)但无论哪种方式, sample都可以被认为是一种数据类型只是链接intstring

Line 2 is a allocating a block of memory and assigning it our char pointer called buf .第 2 行是分配一个 memory 的块,并将我们的char pointer 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).假设样本是一个包含 2 个整数的 class,这意味着它(在大多数编译器下)将是 8 个字节(每个整数 4 个)。 And so buf points to the start of a block of memory that has been set aside to hold char s.因此buf指向 memory 块的开始,该块已被预留用于保存char s。

Line 4 is where it gets a big complex. 4号线是一个大综合体。 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 .如果它只是p = new sample(10,20)这将是创建一个新的 object 类型的简单案例sample ,将两个int传递给它并将这个新的 object 的地址存储在指针p中。 The addition of the (buf) is basically telling new to make use of the memory pointed to by buf .添加(buf)基本上是告诉new使用buf的 memory。

The end effect is, you have one block of memory allocated (more or less 8 bytes) and it has two pointers pointing to it.最终效果是,您分配了一个 memory 块(大约 8 个字节),并且有两个指针指向它。 One of the points, buf , is looking at that memory as 8 chars, that other, p , is looking at is a single sample .其中一个点buf正在将 memory 视为 8 个字符,而另一个p正在查看的是单个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.现代 C++ 已经使new的 sue 变得多余了,有很多更好的方法来处理对象。 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.我猜想使用这种方法的主要原因是,如果出于某种原因你想保留一个 memory 的分配池,因为获取大块 memory 可能需要时间,你也许可以节省一些时间。

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.您发布的代码基本上与执行sample *p = new sample(10,20)相同,这两种方法都没有控制创建sample object 的位置。

Also consider that you do not always need to create objects dynamically using new.还要考虑到您并不总是需要使用 new 动态创建对象。

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

This automatically creates a sample object for you.这会自动为您创建sample object。 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.这种方法更可取,因为它更容易阅读和理解,你不必担心删除 object,当 function 返回时,它会为你清理。

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)如果您确实需要使用动态对象,请考虑使用智能指针,例如unique_ptr<sample>这将使您能够使用动态 object 创建,但省去了手动删除 object 类型sample的麻烦(我如果您还活着,可以为您指明更多相关信息)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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