Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Arrays and loops question

loop array

  • Please log in to reply
5 replies to this topic

#1 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 25 March 2008 - 06:27 AM

Well I sometimes get confused when it comes to loops and arrays.

Let's say I want 5 numbers to be displayed, I know that in this case it's useless making an array, but I'm just showing an example. So I can either declare an array of 5 and start the loop form 0 like this:

int [] numbers = new int [5];

			for(int i=0;i<5;i++)
			{
				Console.WriteLine(i);
			}
			Console.ReadLine();

Or using this method, declaring an extra 'space' in the array and start the loop from 1

int [] numbers = new int [6];

			for(int i=1;i<6;i++)
			{
				Console.WriteLine(i);
			}
			Console.ReadLine();

The second method is, in some cases when I'm using multidimensional arrays, less confusing. But are there any downsides while using the second method?

Thanks
  • 0

#2 Maurice_Z

Maurice_Z

    CC Regular

  • Just Joined
  • PipPipPip
  • 40 posts

Posted 27 March 2008 - 11:47 AM

Yes, it leaves one empty space in array, which in this particular case wastes... 6 or 8 bytes of memory (Which remains unused). When having lots of objects with array field, it could slow down the game a bit or take more memory than necessary. Either way it is better to get used to the "Confusing" way, as it will save you tons of confusion later :).
  • 0

#3 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 28 March 2008 - 08:02 AM

Yeah, I kinda knew that, I am using this just for small programs, but besides the waste of space, are there any other downsides, maybe even worst than wasting memory?
  • 0

#4 Maurice_Z

Maurice_Z

    CC Regular

  • Just Joined
  • PipPipPip
  • 40 posts

Posted 28 March 2008 - 10:20 AM

There shouldn't be as far as I know. Maybe if using the EachIn, or when working with Array Length, the calculations could be mistaken by one, unless you remember to reduce the outcome by one.

But I'll say it again - You'd better get used to counting starting from Zeroth number. So if you see:
int [] numbers = new int [173];
You intuitively know that there are 173 elements, not 172. Waste some time today, to save lots of it tomorrow :).
  • 0

#5 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 28 March 2008 - 01:15 PM

Well when I see something like that I realize that, I only get confused when I need to use them with arrays.
  • 0

#6 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 30 March 2008 - 09:09 AM

It can be really serious to use up too much memory. If the object references lots of other objects, it can create a memory leak. It's even more important when you're writing games, as you really have to watch performance.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download