2

Bit confused about usage of new to allocate memory dynamically.

e.g. If I need to allocate memory for 100 ints(assuming int is 4 bytes), should I say :

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?

  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?

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 *arr = new int [100][4];

thank you.

-AD

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
goldenmean
  • 18,376
  • 54
  • 154
  • 211

2 Answers2

4

The size given to new is the number of items, not the memory size. However, consider using std::vector instead.

For example, new int[100] allocates at least 100 * sizeof(int) bytes (which is 400 when sizeof(int) is 4); 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.


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:

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.

Fred Nurk
  • 13,952
  • 4
  • 37
  • 63
  • @Fred: Thanks for prompt reply. Since my orignal code is a mix of C + C++ (80%/20%), and frankly not so well versed with C++ as C. Didn't really think about it. But can you point a brief test code example to use std::vector? – goldenmean Jan 31 '11 at 11:56
  • @goldenmean: Better than that, I can point to several: see *Accelerated C++*, *Programming: Principles and Practice Using C++*, or [others](http://stackoverflow.com/q/388242). – Fred Nurk Jan 31 '11 at 11:58
  • will this piece of code work fine if i want to allocate a 2D array of size [100][4] of ints: int *arr = new int [100][4]; – goldenmean Jan 31 '11 at 12:07
  • Heer you can find a brief example of vector usage: http://www.cplusplus.com/reference/stl/vector/vector/ – Kos Jan 31 '11 at 12:10
  • @Fred: Good explanation. Doubt clear:-) Will remember this now forever. Thanks for the answers & links as well. – goldenmean Jan 31 '11 at 12:17
  • @Kos: Thank you Kos for the link. – goldenmean Jan 31 '11 at 13:37
2

The confusion may come because of malloc which uses data size, not the number of items. To allocate an array of 100 integers, you can do:

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

or (better, works for all platforms because an int is not always 4 bytes long):

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. Of course malloc can only be used for built-in types such as int.

Emmanuel
  • 13,935
  • 12
  • 50
  • 72
  • Malloc can be used for any type, but you still need to construct (and subsequently destruct and free) the object(s). Malloc should be compared to operator new (the function with that name) rather than the new operator. – Fred Nurk Jan 31 '11 at 15:31
  • You're right - you can indeed allocate space for any type, built-in or not (but the space allocated then needs calls to constructors to be properly initialized, whereas for a built-in type the area can be used right away). – Emmanuel Feb 01 '11 at 12:56