How to write this in another (perhaps shorter) way? Is there a better way to initialize an allocated array in C++?
int main(void) {
int* a;
a = new int[10];
for (int i=0; i < 10; ++i) a[i] = 0;
}
How to write this in another (perhaps shorter) way? Is there a better way to initialize an allocated array in C++?
int main(void) {
int* a;
a = new int[10];
for (int i=0; i < 10; ++i) a[i] = 0;
}
int *a =new int[10](); // Value initialization
ISO C++ Section 8.5/5
To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized
For differences between the terms zero initialization, value initialization and default initialization, read this
How about 3 ways?
1. int *a = new int[10]();
2. std::vector<int> a(10, 0);
3. int *a = new int[10];
memset(a, 0, sizeof(int) * 10);
Due to popular demand, a couple more:
4. int *a = new int[10];
std::fill(a, a + 10, 0);
5. std::vector<int> a(10);
std::fill(a.begin(), a.end(), 0);
fill, but I just like the number 3 more than 4. :)int a[10] = {0}; could be an alternative way.You could use memset
Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
memset. std::fill is a more generic way of getting the same thing (which means the code can be consistent) at no loss.memset changes by bytes, it cannot be used for non-zero values. stackoverflow.com/a/17288891/2680660int main(void) { int *a; a = new int[10]; for(int i=0;i<10;++i) a[i]=0; }
;-)
int main(){int i,*a=new int[10];for(i=0;i<10;++i)a[i]=0;}Maybe you could try something like this:
int* initIntArray(int size) {
int *temp = new int[size];
for(int i = 0; i < size; i++) {
temp[i]=0;
}
return temp;
}
int main () {
int* a = initIntArray(10);
int* b = initIntArray(10);
int* c = initIntArray(10);
//do stuff with arrays
delete [] a;
delete [] b;
delete [] c;
return 0;
}
by the way, what about using calloc()? say
int i*=(int[10])calloc(10*sizeof(int))
well i'm just another C guy.. any comment is welcomed here
new and if you use calloc you have to use free instead of delete.int i*=(int[10])calloc(10,sizeof(int));I'm a C guy and not too sure what "new" really does, but could this work?
int
main( void ) {
int i = 10; // start at the far end of the array
int *a = new int[10];
while ( i-- ) a[i] = 0; // while ( i == 9, 8, 7, ... , 0 )
}
Just to show off my new loop-counter favorite: while(condition).
i static? 2) Why while instead of the established, idiomatic for?static is zero initialized; and while is different from for. I agree with you, though: outside the scope of this question, I'd be upset with someone who presented the code shown as a good way of doing things.