On Wednesday, 13 June 2018 at 16:40:51 UTC, Steven Schveighoffer
wrote:
On 6/13/18 10:43 AM, Michał wrote:
When I pass my struct to function something is going wrong. I
don't know how to fix it.
Code:
import std.stdio;
void print(ref Vector v, string s){
writefln("%s==%s %s", &v.x, v.ptr, s);
}
struct Vector {
int x;
int* ptr;
this(this) {
ptr = &x;
print(this, "postblit");
}
}
void someFunc(Vector t) {
print(t, "in someFunc");
}
void main() {
auto tmpA = Vector();
tmpA.ptr = &tmpA.x;
print(tmpA, "start");
someFunc(tmpA);
}
Result on my machine:
7FFF7D70BC00==7FFF7D70BC00 start
7FFF7D70BBF0==7FFF7D70BBF0 postblit
7FFF7D70BBD0==7FFF7D70BBF0 in someFunc
In the last line pointers are not matching. I thought that
postblit will do the thing but it is not the case. How to make
'ptr' to be null or '&this.x' all the time?
D allows moving any struct instance without calling postblit,
as long as the original is no longer used.
The optimizer is likely seeing here that the memory can be
copied without calling postblit, because nobody is using tmpA
after the call.
In general, you should NOT store an internal pointer in a
struct, unless you allocate it on the heap.
-Steve
I need internal pointer because I want to implement vector(like
in C++) with 'small vector optimization', when i have internal
pointer it is very easy and functions like 'add' don't have
additional checks.
If storing internal pointers is forbidden do you know some way to
implement 'small vector optimization' without additional checks
in 'add' function?