A pointer assumes that the object lies somewhere in memory which might not be true. The object might not be created at all.
How can you tell the difference? If it is isbits it is stored inline. What you take out from the array can be a pointer or a copy or something, since its immutable it doesn’t matter.
Uhm… I cannot imagine an example of that, but I will try to figure that out. Only if the object is never used and the compiler makes the decision of not creating it, I don’t know.
EDIT: I understand that one case where it does make sense for an immutable object not to have pointer is, specifically, when it is part of a larger array which itself has one pointer. This makes sense.
Clearly it doesn’t matter for the use of the data, but if it might be a pointer, that means that the vector y of the example is not contiguous in memory. I was reasoning that a vector of immutable structs is in principle allocated when it is created and, thus, possibly contiguous, while the vector of mutable types clearly isn’t.
(By the way: I found now this interest post that might be of interest of people reaching here: http://schurkus.com/2018/01/09/mutability-and-performance-in-julia/ - which made me think that the references to immutable objects in a vector may or may not be copies, and the compiler will decide that trying to optimize performance).
Ps. 2. : Thank you very much kristoffer.carlsson for even taking your time to read this… Julia has an interesting peculiarity that the people answering the questions here are so deeply involved in the language that even very simple questions frequently derive into deep discussions on more profound aspects of the language design.
and you call f(3+4im) — i.e. you pass a Complex{Int}, an immutable struct comprising its real and imaginary Int parts. The intermediate computations like 2z^2 are also Complex{Int} values, but the compiler never allocates a struct of them anywhere (either on the heap or on the stack) — instead, these computations are all inlined and the real and imaginary parts of all of the intermediate values are stored separately in CPU registers (which don’t have a pointer address).
(You can see this explicitly if you do @code_native f(3+4im) — only the movq instructions at the beginning and the end load/store explicit Complex{Int} values from/to memory for the input and final output values, respectively.)
Ok, I understand that if we want an array of mutable structs, the contiguous array will contain pointers to the structs. The struct itself is somewhere on the heap.
This can have performance implications due to the extra pointer lookup as well the resulting as non-sequential memory access.
Is it possible to have an array of mutable structures, but the structures are laid out contiguously in memory?