30

I need to dynamically create an array of integer. I've found that when using a static array the syntax

int a [5]={0};

initializes correctly the value of all elements to 0.

Is there a way to do something similar when creating a dynamic array like

int* a = new int[size];

without having to loop over all elements of the a array? or maybe assigning the value with a for loop is still the optimal way to go? Thanks

3

6 Answers 6

48

Sure, just use () for value-initialization:

 int* ptr = new int[size]();

(taken from this answer to my earlier closely related question)

Sign up to request clarification or add additional context in comments.

13 Comments

@shartptooth I tried that and get an error: error: ISO C++ forbids initialization in array new [-fpermissive]
@Arno that should compile. What compiler are you using?
@juanchopanza GCC g++ compiler within eclipse cdt
@Arno maybe it is an old version. I have no problem with 4.6 or 4.7.
@CppLearner: you seem to be looking for std::uninitialized_fill
|
6

I'd do:

int* a = new int[size];
memset(a, 0, size*sizeof(int));

4 Comments

@Arno The proper way is sharptooth's answer.
'without having to loop over all elements of the a array' - Isn't that effectively what you're doing with memset? Albeit that memset will no doubt be machine optimised.
@Component10: Yes, it will most certainly be optimised. But I agree with Luchian, the proper answer is sharptooth's. Bear in mind thought that even with that syntactically proper solution, the "memset" will be done "under the hood".
With GCC, the memset will generate a warning for non-trivial types. Here, the int is trivial, so it works. For other types, that may have a non-default constructor, or default initialized fields, this will produce a warning, with a suggestion to use value initialization.
5

I'd advise you to use std::vector<int> or std::array<int,5>

Comments

4

Value initialize the elements with ()

Example:

int *p = new int[10];       // block of ten uninitialized ints
int *p2 = new int[10]();    // block of ten ints value initialized to 0

Comments

3

To initialize with other values than 0,

for pointer array:

int size = 10;
int initVal = 47;
int *ptrArr = new int[size];
std::fill_n(ptrArr, size, initVal);
std::cout << *(ptrArr + 4) << std::endl;
std::cout << ptrArr[4] << std::endl;

For non pointer array

int size = 10;
int initVal = 47;
int arr[size];
std::fill_n(arr, size, initVal);

Works pretty Much for any DataType!

!Be careful, some compilers might not complain accessing a value out of the range of the array which might return a non-zero value

Comments

0
int *a=new int[n];
memset(a, 0, n*sizeof(int));

That sets the all the bytes of the array to 0. For char * too, you could use memset. See http://www.cplusplus.com/reference/clibrary/cstring/memset/ for a more formal definition and usage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.