Compare two ways of declaring an array

int buf1[] = {0,0,0,0,0};

int* buf2 = new int[5]; //assume every element is initialzed to 0 as well

The only difference I can think of is buf1 is a reference to the array while buf2 is a pointer pointing to the array. In other words, buf1 always refers to the array while buf2 can point to other places as well.

Besides the mentioned one, is there any other difference between the two ways of declaring(and initializing an array)?

Answers


buf1 is an automatic object (or static if it's in the global scope); *buf2 is a dynamic object. That is, the lifetime of buf1 is controlled automatically, while the lifetime of *buf2 is yours to manage. (buf2 lives until you say something like delete[] buf2;.)

The initializers are also different; buf1 is brace-initialized, while *buf2 is default-initialized (i.e. its int elements are uninitialized).


Need Your Help

bind/unbind service example (android)

android binding service

can you give me a simple example of application with background service which uses bind/unbind methods to start and stop it? i was googling for it for an halfhour, but those examples uses

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.