[quote='757425022, DTS Engineer, /thread/722360?answerId=757425022#757425022']
In a C-based language arrays and pointers are (more or less :-) the same thing,
[/quote]
To be pedantic... This is a common misconception. The nuance here is that a static C array is effectively an allocated address on the stack. It is always just an address. This is because a typical array variable is statically allocated at compile time and its size is known beforehand based on the compiler parsing the language. The address of the first element is what the array is.
Meanwhile, a pointer is another type of variable. A pointer is effectively a space to store an address to something else. At compile time, the compiler knows the size of an address (on a particular machine architecture) and statically allocates a place on the stack to store the address of something else (the thing being pointed to). A pointer has it's own address. The value stored in that place is also another address.
So, given the above, effectively a C array is not a pointer. Although, due to the way that pointer arithmetic works, people often mistake the behavior of each to be similar.
In fact, it turns out that the syntax to set an array element and the syntax to do the same via pointer arithmetic both result in the same machine code or assembly generated by the C compiler.
The stride of an array is known from the type it contains, and therefore pointer arithmetic can be used on the address of an array to access or set its elements. It still does not change the fact that an array is literally the address of the first element. Meanwhile, a pointer is another place storing an address as its value.
Huw Collingbourne made a great video explaining this in detail here: https://www.youtube.com/watch?v=bFAO99USrYI
EDIT: I'm not sure how the above situations apply on Apple Silicon (aarch64/arm64) and how a compiler would allocate a large array on the stack when compiling to machine code on ARM. Huw's example is most definitely on Intel x86 architecture, and therefore the example disassembly is in Intel x86 assembly. It makes sense that changing to use malloc() and place an array on the heap is working around the issue if something with stack-allocated large arrays is different on Apple Silicon.
Topic:
Developer Tools & Services
SubTopic:
General
Tags: