473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

c++: dynamic array

A
Hi,

consider the following code:

int i = new int[10];
myArray* a = new myArray[10];

question: sometimes you need to initialise all elements in an array to 0 (eg
using a for loop) before you can start adding items to the array, and other
times you just declare it and use it straight up. My question is, when do
you know when to do it?

regards
A

Jul 19 '05 #1
2 20365
> consider the following code:

int i = new int[10];
myArray* a = new myArray[10];

question: sometimes you need to initialise all elements in an array to 0 (eg using a for loop) before you can start adding items to the array, and other times you just declare it and use it straight up. My question is, when do
you know when to do it?


Well you should understand that for a (local) array of fundamental types
(AKA POD=Plain Old Data types such int, long, etc.), each element will
normally contain garbage (an uninitialized value) and you shouldn't carry
out any processing based on these elements until you later initialize them.
You can safely do this at your own convenience, looping through all of them
and initializing each, or you can initialize items 1, 3 and 8 only if you
really want. It makes no difference so long as you don't use a given element
until it has been initialized. The timing is up to you and your app's
requirements (initializing them all at once is common however). Your "i"
array is such an example since an "int" qualifies as a POD type. Each "int"
will initially contain garbage noting that this applies to local arrays only
however (as stated). For global arrays (those defined outside a function
essentially), each element *will* be initialized to zero for you (since this
applies to global variables in general, not just arrays). Note that for an
array of non-POD types however (your own classes basically), the default
constructor for each element is called when the array is created (unlike the
POD case). So each element will already be initialized based on the default
constructor for that class. Note that it's still up to you to initialize any
POD members of your class however (in the constructor) and you normally
should. Otherwise they'll also contain garbage unlike the non-POD members
whose default constructors will be called. The bottom line is that an
array's elements need not be initialized until you actually need them.
Jul 19 '05 #2
> consider the following code:
int i = new int[10];
myArray* a = new myArray[10]; question: sometimes you need to initialise all elements in an array to 0 (eg using a for loop) before you can start adding items to the array, and other times you just declare it and use it straight up. My question is, when do
you know when to do it?


You're writing the program, so you should know what you intend to do :-)

More seriously, why are you using "new" at all? If you did it this way:

std::vector<int> i;

then the vector would start out with no elements at all, so you wouldn't
have to worry about how to initialize them. Then, when you wanted to add
items to the vector, you would write something like this:

i.push_back(n);

and the vector would have one element more than it did before.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
1
by: lemonade | last post by:
Hello! Can someone explain to me the difference between dynamic array of pointers vs dynamic array of objects by giving a real life example. Following is the code that I am using for dynamic...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
0
by: pjr | last post by:
Using VS2005, I dynamically create an event delegate. Code follows question. My method gets the event's parameters and passes them onto a common event handler. My delegate gets called when expected...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
13
by: kwikius | last post by:
Does anyone know what a C99 dynamic array is, and if it will be useable in C++? regards Andy Little
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.