What is the maximum length of an array that I can declare in Go, Java and C#? Does it relate to the maximum memory at runtime? Or do they have a standard?
Asked
Active
Viewed 1,515 times
-1
-
In java, the `length` of an array is an `int` field, so the max length of an array is determined by `Integer.MAX_VALUE`. – Luiggi Mendoza Oct 29 '14 at 03:43
-
..and is similar in .NET with `int.MaxValue`. – Simon Whitehead Oct 29 '14 at 03:45
-
@LuiggiMendoza Appears to be `Integer.MAX_VALUE - 5`. See [here](http://stackoverflow.com/a/3039805/3558960) – Robby Cornelissen Oct 29 '14 at 03:45
-
This is some information on the max array limit for C#. http://msdn.microsoft.com/en-us/library/ms241064(v=vs.110).aspx – deathismyfriend Oct 29 '14 at 03:46
-
@RobbyCornelissen well, that answer is from 2010, so I would have to test it on JAva 8 and with more memory dedicated to the JVM and check if it gives the same error. – Luiggi Mendoza Oct 29 '14 at 03:47
-
@LuiggiMendoza It does on recent Oracle Java 8 JVM. – Robby Cornelissen Oct 29 '14 at 03:48
1 Answers
1
The Go Programming Language Specification
An array is a numbered sequence of elements of a single type, called the element type. The number of elements is called the length and is never negative.
The length is part of the array's type; it must evaluate to a non-negative constant representable by a value of type
int
.A numeric type represents sets of integer or floating-point values.
There is a set of predeclared numeric types with implementation-specific sizes:
uint the set of all unsigned integers, either 32 or 64 bits int the set of all signed integers, same size as uint
Go array length is a value of type int
, which is a 32 or 64 bit signed integer, depending on the compilation architecture (GOARCH), for example, 386 or amd64. It's also subject to any hardware or operating system memory size limits.
package main
import (
"fmt"
"runtime"
"strconv"
)
func main() {
fmt.Println("int is", strconv.IntSize, "bits on", runtime.GOARCH)
}
Output:
int is 64 bits on amd64

peterSO
- 158,998
- 31
- 281
- 276