473,466 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Aggregate Initialization of arrays

180 New Member
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
6 6273
dush
27 New Member
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
9,065 Recognized Expert Moderator Expert
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
ltgbau
41 New Member
memset
why don't you use it?
Sep 21 '06 #4
Banfa
9,065 Recognized Expert Moderator Expert
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
dush
27 New Member
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
ltgbau
41 New Member
yes, you're right, DUSH :)
Sep 29 '06 #7

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

Similar topics

5
by: Unforgiven | last post by:
Me and some other people have been involved in a debate whether or not the following code is legal C++: #include <vector> struct s_stuff { std::string s_search; std::string s_url; }; int...
1
by: Piotr Sawuk | last post by:
just a quick question out of curiosity: how to initialize const arrays? struct srat { long num; ulong den; srat(){} } struct collisions
9
by: Dadi | last post by:
Hi, I can make a simple initialization work like this: Object ONE_ROW = {{"Vodafone", "5550160100197016"}}; But, now I want to create another array that consists of multiple copies of...
2
by: Robert A Riedel | last post by:
This is a repost of a question that received no answer. In a module DATA.CPP, when attempting to initialize a __gc array as follows: // // Begin sample ... // // // Yes, I include all of...
3
by: Raghu | last post by:
Hello all, Can somebody help me hopw to resolve teh probelm of aggregate initialisation in c++. Her eis the piece of code. #include<stdio.h> class MyTest { public:
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
3
by: jaime | last post by:
Hi all. The source code download bundle for "Beginning C: From Novice to Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress) contains a C source file (program9_09.c) which contains...
10
by: Juha Nieminen | last post by:
I suppose you can never know C++ fully. I would have never guessed this actually compiles and works: struct Point { int x, y; }; struct Line { Point endpoint; int weight; };
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.