By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,763 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

Aggregate Initialization of arrays

100+
P: 180
Hi guys,
Cannot I initialize arrys using aggregate initialization

something like this

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.  
  4.       enum{ sz=10 };
  5.       int arr[sz];
  6.   public:
  7.           A()
  8.           {
  9.              arr[sz] = {0};
  10.           }
  11.          ~A(){}
  12. };
Please dont tell me to initialize using for loops....neither tell me to use static int arr[sz] and to declare outside like this
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.  
  4.       enum{ sz=10 };
  5.       static int arr[sz];
  6.   public:
  7.           A()
  8.           {
  9.              arr[sz] = {0};
  10.           }
  11.          ~A(){}
  12. };
  13. int A::arr[] = {0}; 
I want to declare arrays at one shot...Isnt that possible

Any comments will be highly appreciated
Sep 19 '06 #1
Share this Question
Share on Google+
6 Replies


P: 27
Hi

I dont have any idea how to initialize array without using loops.
I made up tiny loop for that purpose:

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {                  
  3.   int arr[10];
  4.  
  5. public:
  6.  
  7. //my compact constructor:
  8.  
  9.   A()
  10.    {
  11.      int* i = arr;
  12.      while (i<(int*)(&arr+1)) *i++ = 0; //array initialized with zeros
  13.    }
  14.  
  15. //my most compact constructor (only one line of code):
  16.  
  17.   A() {for (int* i=arr; i<(int*)(&arr+1); i++) *i=0;}
  18. }
  19.  
Please post here if you know the more simple way.
Thanks
Sep 19 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.  
  4.       enum{ sz=10 };
  5.       int arr[sz];
  6.   public:
  7.           A()
  8.           {
  9.              arr[sz] = {0};
  10.           }
  11.          ~A(){}
  12. };
  13.  
Wont work because it is not an initialisation it is an assignment, and arr[sz] has type int and is (by definition) not within the bounds of the memory allocated for arr.

In fact it is not possible to provide explicit initialisation for array members. You could try using memset though.

Why are you so anti loop?

I don't think squishing a loop onto 1 line makes it better, it just makes it harder to read.
Sep 20 '06 #3

P: 41
memset
why don't you use it?
Sep 21 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
memset
why don't you use it?
Strictly speaking it probably still uses a loop internally which is way I wanted to know what the OP had agaist loops.

However I agree this is probably the easiest/best method to get the desired result.
Sep 21 '06 #5

P: 27
Hi

Yes memset is best method, I didnt know about it :) . However it is usefull only for arrays of chars. What if you want to initialize array of int numbers or long numbers or any other fundamental type to the other value than 0 ? (memset would manage it with zeros of course)

I agree that zero initialization is used most often, so most of the time memset is perfect solution, however for non zero value and non char array initialization I still dont know better way than:

for int type:

for (int* i=arr; i<(int*)(&arr+1); i++) *i=500;

float:

for (float* i=arr2; i<(float*)(&arr2+1); i++) *i=0.5;

...
Sep 21 '06 #6

P: 41
yes, you're right, DUSH :)
Sep 29 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.