简体   繁体   English

使用“ new”在C ++中动态分配

[英]Dynamic allocation in C++ using 'new'

Bit confused about usage of new to allocate memory dynamically. 有人对使用new动态分配内存感到困惑。

eg If I need to allocate memory for 100 ints(assuming int is 4 bytes), should I say : 例如,如果我需要为100个整数分配内存(假设整数是4个字节),我应该说:

int *ptr = new int[100];

or 要么

int *ptr = new int[100*4]; //assuming int is 4 bytes.
  1. Basially new operator allocates memory in bytes or that many bytes of type T used while invoking the new operator? 容易地,新运算符以字节为单位分配内存,或者在调用新运算符时使用了多少字节的T类型字节?

  2. If my class doesn't have a allocator member function defined, & i need to allocate an array of object of that class type, will the new oeprator find the sizeof(class type) and allocate accordingly or how would it work? 如果我的类没有定义分配器成员函数,并且我需要分配该类类型的对象数组,新的操作者会找到sizeof(class type)并进行相应分配吗,或者它将如何工作?

EDIT: 编辑:

Sorry for clubbing multiple questions, but its related: 很抱歉将多个问题汇总在一起,但与以下问题有关:

will this piece of code work fine if i want to allocate a 2D array of size [100][4] of ints 如果我想分配一个大小为[int] [100] [4]的2D数组,那么这段代码会正常工作吗

int *arr = new int [100][4];

thank you. 谢谢。

-AD -广告

The size given to new is the number of items, not the memory size. 赋予new的大小是项目数,而不是内存大小。 However, consider using std::vector instead. 但是,请考虑改用std :: vector。

For example, new int[100] allocates at least 100 * sizeof(int) bytes (which is 400 when sizeof(int) is 4); 例如, new int[100]至少分配100 * sizeof(int)个字节(当sizeof(int)为4时为400)。 any more it allocates will be due to implementation and runtime details which you (except for very rarely) cannot depend on. 它分配的更多内容将归因于您( 非常罕见的情况除外)无法依赖的实现和运行时详细信息。

If you don't have an operator new or operator new[] in your class (which you usually shouldn't), then the global versions will be used, and they will use sizeof(your_type) correctly. 如果您的班级中没有运算符new或new [](通常不应该),那么将使用全局版本,并且它们将正确使用sizeof(your_type)。


Did you try the code in the update ? 您是否尝试了更新中的代码

Multidimensional arrays are actually arrays of arrays; 多维数组实际上是数组的数组。 so new returns a pointer to the first item just as it does for single dimensional arrays, which is a pointer to an array: 所以new会返回指向第一项的指针,就像针对一维数组一样,它是指向数组的指针:

typedef int int4[4];  // for clarity
int4 *arr = new int4[100];
// above two lines are identical to:
int (*arr)[4] = new int[100][4];

Again, you're almost always better off using a container than managing this yourself. 同样,使用容器比您自己管理几乎总要好。 That's vector, et. 那是向量,等等。 al., but also containers like boost::array and dedicated "matrix" types for "square" 2-dimensional arrays. 等,还包括boost :: array和专用于“正方形”二维数组的“矩阵”类型之类的容器。

The confusion may come because of malloc which uses data size, not the number of items. 混淆可能是由于malloc使用数据大小而不是项目数引起的。 To allocate an array of 100 integers, you can do: 要分配100个整数的数组,可以执行以下操作:

int *ptr = (int *)malloc(100 * 4);

or (better, works for all platforms because an int is not always 4 bytes long): 或(更好,它适用于所有平台,因为int并不总是4个字节长):

int *ptr = (int *)malloc(100 * sizeof(int));

instead of: 代替:

int *ptr = new int[100];

which, as Fred says, automatically uses the data size to allocate the correct area. 正如Fred所说,它会自动使用数据大小来分配正确的区域。 Of course malloc can only be used for built-in types such as int . 当然, malloc仅可用于诸如int类的内置类型。

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

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