   Java/C# array creation without initialization?
  So in Java and C# when you create an array likeint[] ar = new int[10];It automatically initializes each element to 0. What I'm wondering is, can I get space for an array without initializing to 0, hopefully making the line execute faster?   
  And what would the elements' values be? Some random garbage like C++?But no, you can't. And realistically, zeroing out a block of memory (the default initialization pattern for .net at least) is not going to be a performance concern.   ;  Well, yes I would expect them to contain garbage like c++.How is zeroing everything not a performance issue? Doesn't zeroing everything take O(n) time, whereas just setting aside the memory for the array shouldn't take that long?   ;  It's not a performance issue. Writing N bytes to memory (blocks at a time) is negligible compared to the other things that are going on in your app.Have you actually identified it as a performance bottleneck? ;  Considering that (a) uninitialized values are a significant risk to program stability (and a security risk) and (b) "new" in Java/C# is significantly faster than "new" in C++, it should be pretty obvious that the benefits outweigh the costs.   ;  Why would you need garbage anyway?   ;  Do not worry about this. Focus on bottlenecks when everything works. Favour algorithmic optimisation.   ;  I'm not basing this question on a current problem or anything. It's just that more often than not when I'm using an array, I have to insert the values myself and it just seems like a waste to have them initialized to 0 then reinitialized to whatever I'm using. I was asking so that if at some point in the future I write a function which gets executed many times per second which requires the use of a local array, it seems to me a useless O(n) operation would in fact slow performance.   ;Quote:I was asking so that if at some point in the future I write a function which gets executed many times per second which requires the use of a local array, it seems to me a useless O(n) operation would in fact slow performance.But this allocation operation happens once. You aren't proposing allocation and deallocation of memory very frequently in one second, right?   ;Quote:Original post by jamesd128Well, yes I would expect them to contain garbage like c++.How is zeroing everything not a performance issue? Doesn't zeroing everything take O(n) time, whereas just setting aside the memory for the array shouldn't take that long?As it happens, it is likely an O(1) or perhaps even O(0) operation, depending on when the values get zeroed. If zeroing happens at garbage collection time, then when memory is allocated, there is no need to zero it again.In C#, the behavior may depend on which heap the instance is allocated from (large object heap is treated differently).The cost of zeroing sequential memory is likely under 1 cycle per byte, and needs to happen anyway, whether during GC or during allocation - same is true in C++, the difference in C++ comes from "reusing" existing allocation (where previous values might be undefined), the equivalent of which in managed languages is reusing a previously allocated array.Quote:I was asking so that if at some point in the future I write a function which gets executed many times per second which requires the use of a local array, it seems to me a useless O(n) operation would in fact slow performance."Doctor, if I allocate a new instance it hurts!""Well, what do you think you should stop doing?"Allocate once, reuse the instance.The problem here has nothing to do with zeroing, but with potentially creating redundant instances.   