Diff: new int [] & new int [2]

Quote:
int * foo;
foo = new int [5];

Alright, the above is taken from cplusplus tutorial, it shows that foo is a pointer pointing to the 1st int which is int[0]. The program then allocates 5 memory space for 5 int. But what if instead of 'int [5]' i use 'int []' instead?

Below is a sample code which i wrote and i tried using both 'new int [2]' and 'new int []', there's no error etc and both results are the same. Would appreciate if someone could enlighten me on the difference between these 2. Thanks.

int *i = new int [2];
i[0] = 1;
i[1] = 2;
i[3] = 4;
cout << i[0] << endl;
cout << i[1] << endl;
cout << i[2] << endl;
cout << i[3] << endl;
1
2
3
4
int *i = new int [2];
i[0] = 1;
i[1] = 2;
i[3] = 4;


If you do this... you are accessing unallocated memory. This is very bad and may cause very strange bugs in your program.

It might crash.
It might cause other, completely unrelated variables in your program to change.
It might do nothing



and 'new int []', there's no error etc and both results are the same.


new int[]; will not compile. You should be getting an error message.
It really surprised me that statement like "int *i=new int[];" can be compiled without an error.

OK, I tried this code to find what happened.
The only thing I found at last is that this statement would actually return an address without calling the constructor. But I'm still confused about the size of the memory allocated from the heap by this statement.
By the way, I am using VS2010 to compile this code.

class Object{
public:
Object() { std::cout << "Constructed.\n"; }
};

int main()
{
Object *ptr = new Object[];
ptr[0] = Object();
ptr[1] = Object();
return 0;
}
Last edited on
This must be an error/mistake in VS. You can't allocate an array without specifying the size of the array.

Other compilers will give you an error if you try to do this:
http://ideone.com/iSi5jC

So yeah... don't do this.
Hmm ok.. I get it.. But I understand people use new int[2] as it can be created at run time right?

What then is the diff between
int i[2] and int *i = new int[2]?
I understand the first is an array, and the second is also an array but with pointer i pointing at the first object. But then why do we have these 2 way of doing array and when do we use which?
If you know the size at compile time, there is no reason to use new.
It's mostly a scope thing.

 
int x[2];

That creates a local array that is automatically destroyed when it goes out of scope.


 
int* x = new int[2];

This creates an array on the heap that has a lifetime for as long as you need it (it is never automatically destroyed... it is only destroyed when you delete[] it).



Also... the standard does not allow you create a local array with a variable size. The size has to be known at compile time. However, for heap arrays, you can have a variable size. So:

1
2
3
4
int size = getSomeValueFromUser();

int* foo = new int[size];  // Ok
int bar[size]; // Not ok (though some compilers may allow it anyway... others might error) 




when do we use which?


Use the local array when you just need a small array for use in a function... or if you need a reasonably small, fixed-size array.

Use the 'new' version... well... never. Manual memory management in C++ should be avoided. There are usually better alternatives.

Better alternatives would be a vector:

1
2
3
4
5
int size = 22;
std::vector<int> foo(size);
foo[0] = 1;
foo[1] = 4;
// etc 


Or a unique_ptr (C++11):
1
2
3
4
5
int size = 22;
std::unique_ptr<int[]> foo(new int[size]);
foo[0] = 1;
foo[1] = 4;
// etc 



Both have the advantage of automatically cleaning up. Vector also has the advantage of being resizable (so if you need the array to grow or shrink, just call .resize() with a new size and vector will add/remove elements to the end to accommodate the new size).
Topic archived. No new replies allowed.