Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fold pointer_from_objref into pointer? #14950

Closed
StefanKarpinski opened this issue Feb 5, 2016 · 16 comments
Closed

fold pointer_from_objref into pointer? #14950

StefanKarpinski opened this issue Feb 5, 2016 · 16 comments

Comments

@StefanKarpinski
Copy link
Sponsor Member

The pointer_from_objref gives a pointer to a heap-allocated object. Why not just make this a method of the pointer function, which already gives the pointer location of things like arrays?

@yuyichao
Copy link
Contributor

yuyichao commented Feb 5, 2016

So what should it return for array?

@JeffBezanson
Copy link
Sponsor Member

This is very unsafe and already quite overused. We need to do everything we can to discourage things like this.

@yuyichao
Copy link
Contributor

yuyichao commented Feb 5, 2016

We need to do everything we can to discourage things like this.

Yeah. This too and the more offending one is unsafe_pointer_to_objref.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Feb 5, 2016

i try to encourage exclusive usage of Ref, which already folds these concepts together, plus it has the side benefit that it can't return an invalid pointer like unsafe_pointer_to_objref and pointer can

@StefanKarpinski
Copy link
Sponsor Member Author

Is it any more unsafe than pointer(::Array)?

@yuyichao
Copy link
Contributor

yuyichao commented Feb 5, 2016

I don't think it's more unsafe and so we are moving away from pointer(::Array) too.

@simonster
Copy link
Member

It might be more unsafe than pointer(::Array) depending on how you use the resulting pointer. At least pointer(::Array) is guaranteed to give you a pointer that remains valid as long as the array doesn't fall out of scope (and you don't resize it in some way). This is not the case for e.g. pointer_from_objref on an immutable, which is allowed but should basically never be used.

@StefanKarpinski
Copy link
Sponsor Member Author

I think that we should rename pointer_from_objref of a mutable value to pointer and disallow taking a pointer of an immutable – which, as you point out, one shouldn't do at all.

@StefanKarpinski
Copy link
Sponsor Member Author

It's unclear to me what the plan to entirely eliminate pointer is. Sure it's unsafe, but it's pretty necessary for a lot of interfacing with C and is very handy for exploring the memory layout of data structures.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Feb 6, 2016

if you can give some examples, I'll try to see what the equivalent Ref expression would be

@StefanKarpinski
Copy link
Sponsor Member Author

I just spent a bunch of time calling pointer on vectors of rationals of bigints and trying to figure out what the exact memory layout was so that I could write corresponding C code to access the data structure. It's pointlessly annoying keep having to remember that if I want a pointer to an array I call pointer but if I want a pointer to a rational or a bigint, I call pointer_from_objref – which is tied for worst function name exported from Base with unsafe_pointer_to_objref. Sure, we don't want to encourage using pointer but it MEANS THE SAME THING for arrays and mutable objects, so why do we spell it differently? If we want to replace it entirely and delete the pointer function, fine, but until we do that, these should be the same function.

@JeffBezanson
Copy link
Sponsor Member

which is tied for worst function name exported from Base

Yes, and that was intentional, because people use these functions to write crashy messes that are hard to debug.

pointer actually has a bunch of definitions, e.g. for subarrays and string types, that give the address of the "data you care about" part. This is why there's a separate function for getting the address of a julia object. Sometimes you need the julia object address e.g. for passing a closure pointer through to a callback of a C library. There needs to be a way to distinguish this from something like passing the data of a string as a char*.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Feb 6, 2016

Sometimes you need the julia object address e.g. for passing a closure pointer through to a callback of a C library. There needs to be a way to distinguish this from something like passing the data of a string as a char*

except that you don't actually need a pointer for those things. yes, you need an API that says "pass this as the right type of pointer", but you don't actually need the pointer. that is the key distinction that Ref tries to force upon the user. (where it fails is when C needs / returns an object-or-null-pointer, since Julia does not have a first-class null type).

fwiw, Stefan, you could define: pointer{T}(x::T) = unsafe_convert(Ptr{T}, Ref{T}(x))

@StefanKarpinski
Copy link
Sponsor Member Author

Literally no one has argued that this function doesn't mean the same thing as pointer – and in Julia we generally have one name for a concept, with different methods for different types. The counterargument seems to amount to "this is a dangerous operation so we should not follow the usual conceptual standards that we normally follow in this language", which frankly seems like a bad argument to me.

@yuyichao
Copy link
Contributor

yuyichao commented Feb 6, 2016

That was what I want to argue with #14950 (comment)

@JeffBezanson
Copy link
Sponsor Member

My last comment addresses meaning: pointer means the address of the "canonical data" of a value (in a manner related to write), and pointer_from_objref means the address of the julia object itself. In general these are different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants