What happens if I initialize an array to size 0?

initialize array with length 0
how to initialize an empty array
java 8 initialize empty string array
how to create an array of length 0 in java
java string array length 0
how to initialize array in c# with zero
c# new array of objects without size
array.empty c#

Let's say I have a function like:

void myFunc(List<AClass> theList)
{
   string[] stuff = new string[theList.Count];
}

and I pass in an empty list.

Will stuff be a null pointer? Or will it be a pointer to some random place in memory that is uninitialized?

It will create an empty array object. This is still a perfectly valid object - and one which takes up a non-zero amount of space in memory. It will still know its own type, and the count - it just won't have any elements.

Empty arrays are often useful to use as immutable empty collections: you can reuse them ad infinitum; arrays are inherently mutable but only in terms of their elements... and here we have no elements to change! As arrays aren't resizable, an empty array is as immutable as an object can be in .NET.

Note that it's often useful to have an empty array instead of a null reference: methods or properties returning collections should almost always return an empty collection rather than a null reference, as it provides consistency and uniformity - rather than making every caller check for nullity. If you want to avoid allocating more than once, you can use:

public static class Arrays<T>
{
    private static readonly T[] empty = new T[0];

    public static readonly T[] Empty { get { return empty; } }
}

Then you can just use:

return Arrays<string>.Empty;

(or whatever) when you need to use a reference to an empty array of a particular type.

How do I initialize an empty array in C#?, When you declare an array of specific size, you specify the fixed number of an array without specifying size is to have an empty array of size 0. This will allow you to do whatever to dynamically compute the array's size. the first element (nCount[0]) is set to 1. Every other element gets initialized to zero. You can use this approach to initialize a large array to zero as well: int nScores[100] = {0}; This not only declares the array but initializes every element in the array to zero.

Why should it? It will just point to an array of size 0, which is perfectly valid.

I think the confusion here arises from the ambiguity of representing the absence of data either by an array of size 0 or a variable set to null (the same ambiguity exists for strings with an empty string or a string reference set to null). Both are valid ways to indicate such absence and it would arguably make more sense to have only one. Hence, on some databases (Oracle specifically) an empty string equals the NULL value and vices versa and some programming languages (I think, new versions of C# are one of them) allow to specify references to never be null, also eliminating said ambiguity.

Why does C allow array of size 0?, Some think that C should allow you to do ridiculous things and not save you from So there is a debate here about whether C allows arrays of size 0 or not. So one can declare a pointer to an array without knowing its actual size, though any � This will initialize the num array with value 1 at all index. We may also ignore the size of the array: int num[ ] = {1, 1, 1, 1, 1} The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list.

This is fine code. You will get an Array object with zero items (allocations) in it.

Declaring Arrays, int x[5]; x[0] = 25; /* The first array element is assigned the value 25 */ Therefore , when initializing an array of unknown size, the number of initializers in the� Initialization from strings. String literal (optionally enclosed in braces) may be used as the initializer for an array of matching type: . ordinary string literals and UTF-8 string literals (since C11) can initialize arrays of any character type (char, signed char, unsigned char)

stuff will be a reference to an array with length theList.Count with all entries initialized to default(string), which is null.

Zero Length (Using the GNU Compiler Collection (GCC)), A zero-length array can be useful as the last element of a structure that is really a The preferred mechanism to declare variable-length types like struct line Of course, this extension only makes sense if the extra data comes at the end of a� Note that with C++11, the std::array type may have a size of 0 (but normal arrays must still have at least one element). – Cameron Aug 24 '13 at 19:02 Possible duplicate of What happens if I define a 0-size array in C/C++?

[PDF] 14. Arrays, Similarly, we can create and initialize other types of arrays: int[] scores = {45 names[0] and the nth element is names[n-1]. The size of the array is given by: names.length What exactly happens when we try and access an uninitialized array� C Programming: Initializing an Array in C Programming. Topics discussed: 1) Initialization of one-dimensional array. 2) What will happen if the number of elements is less than the length of the array?

Initialize an Array of String in Java, String in Java. We can declare and initialize an array of String in Java by using. We can also create empty String array of size 0 as shown below: String[] arr� The initializer for an array is a comma-separated list of constant expressions enclosed in braces ({ }). The initializer is preceded by an equal sign (=). You do not need to initialize all elements in an array. If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type.

Declare and Initialize Arrays in Java, arrays in Java. We can declare and initialize arrays in Java by using new operator with array. Java also allow to have arrays of size 0 as shown below:. Java Empty Array. Java allows creating an array of size zero. If the number of elements in a Java array is zero, the array is said to be empty. In this case you will not be able to store any element in the array; therefore the array will be empty. Following example demonstrates this.

Arrays (C++), constexpr size_t size = 1000; // Declare an array of doubles to be allocated A zero-sized array is legal only when the array is the last field in a struct or it std:: cout << *p++ << " "; } delete[] numbers; // don't forget to do this! }� The java.util.Arrays class has several methods named fill() which accept different types of arguments and fill the whole array with the same value: long array[] = new long[5]; Arrays.fill(array, 30); The method also has several alternatives which set a range of an array to a particular value: int array[] = new int[5]; Arrays.fill(array, 0, 3, -50);

Comments
  • It's quite useful when you need an empty IEnumerable<T> (usually for testing)
  • Enumerable.Empty<...> is even better.
  • The key point I was confused about was whether an empty array takes a non-zero amount of space, as you call it.
  • As the information about its 'emptyness' has to be stored somewhere (i.e. the count) it has to take up some space. Given what I wrote in my answer, you could of course (but this is dangerous) have the internal guidline that instead of initializing arrays of length 0 you set the reference to null. But this may need some additional checks when such arrays are used and not be worth the slim space savings.
  • @NETscape: well what would you do if you were implementing a method returning an int[] and you didn't have any elements to return?
  • @NETscape: returning null is a pain for all callers. Returning an empty array gives a much more consistent, uniform experience. You can always stash a reference to an empty array to avoid allocating it each time you need to return one....
  • @Pac0: If you can use Array.Empty, use that instead of any of the code from this answer. It didn't exist in 2010 though :)
  • "Both indicate the absence of data and there is no right or wrong way to indicate such absence." But conceptually, there usually is a difference between the two. It's like the difference between having an empty cardboard box and not having a box at all. (I hope I never have to work with databases where "" == NULL, as I would get quite confused by that.)
  • Oracle apparantly does. I also don't 100% agree with that, because of terinary logic (IS NULL vs = NULL).
  • NULL == "" is an evil Oracle "feature" - no sane system should generally treat them as equal. My opinion...^^
  • In a database, NULL generally means "this value is unknown". So MIDDLE_NAME IS NULL indicates the middle name is unknown, while MIDDLE_NAME = '' means this person is known to have no middle name. In C#, the semantics of null are much wider (not applicable, not yet set, etc.), but empty strings and arrays would indicate—to me anyway—that the value has been determined to be empty. (Sometimes in a database NULL means "not applicable", but that irritates the hard-core RDBMS theorists. Use with caution.)
  • @JeffreyLWhitledge In Oracle, '' IS NULL is TRUE. So be careful what you say about databases in general...
  • His point was that you pass an empty list to it, i.e. theList.Count is 0. This of course means that there won't be any entries initialized to anything.
  • In which case it will be a reference to an array with length 0 with all 0 entries initialized to null. ;)