I'm trying to learn C coming from Python. I was curious on how a multi dimensional array looks like under the hood in C. It turns out, its just a chunk of memory.
For say, a 3x3 0x1000 0x1004 0x1008 0x100C 0x1010 0x1014 ... ... and so on. It would cause undefined behaviour if I try to access array[1][3] on a 3*3. If the array is just a big chunk of memory side by side, how does C know that it was out of bounds, and not returning the value of array[2][0] since google says "In essence, the C programmer is the primary "runtime check" for array bounds and other memory safety issues"
I'm trying to learn C coming from Python. I was curious on how a multi dimensional array looks like under the hood in C. It turns out, its just a chunk of memory.
Yes, and the same for every object of every kind in C. C is among the lowest-level of the high-level languages. It gives you direct access to memory, with almost no abstraction and no hidden structure.
As for arrays in particular, they are represented as contiguously arranged sequences of objects of their element type. Multidimensional arrays are, more formally, arrays whose elements are arrays. However many dimensions there are, that ultimately works out to a block of contiguous objects of some non-array type.
I know it would throw an error if I try to access array[2][4] on a 3*3.
Do you really? I don't know that. Probably most people who have ever written a C program have observed C not throwing an error when their program performs an out-of-bounds array access (whether they realize it or not). And pretty much all of them have also observed misbehavior, large and small, from such accesses, up to and including program crashes. Such an action invokes undefined behavior. That can manifest in the form of some kind of error, but it can manifest in any other way, too, including the program behaving as if the array were larger than it was declared to be.
how does C know that it was out of bounds
C does not know. One of the ideas behind undefined behavior is that C implementations don't need to predict or detect that a program does something it shouldn't. They don't need to make any provision for it at all. In particular, modern C implementations often assume that programs exhibit only defined behaviors when they analyze source to determine what optimizations to perform. This means that it is not safe to try to apply any particular mental model of what will actually happen if your program, say, attempts an out-of-bounds array access, unless you have some basis outside the C language spec for knowing what will happen.
and not returning the value of array[3][1]
Note that your 3 x 3 array does not have an array[3][1]
, either. Arrays are indexed from 0 (just as Python lists), so the element of a 3x3 array at the greatest address is array[2][2]
.
google says "In essence, the C programmer is the primary "runtime check" for array bounds and other memory safety issues"
I'm sure Google itself does not say that. Its AI answer bot might have done, but even that will have been attributed to an original source. And that original source is quite right. C does not have much in the way of safety rails. The programmer is responsible for writing correct programs, and C washes its hands of the situation when the programmer does otherwise. For their part, C compilers generally will attempt to spot issues such as this and warn you about them, but it is pretty easy to perform this or many other kinds of actions with UB in ways that the compiler cannot recognize at build time.