Why does Erlang have a C NIF drop-in replacement for malloc
, enif_alloc
, but not calloc
? Thereby forcing one to use memset()
after enif_alloc
for array access.
Asked
Active
Viewed 226 times
3

Hynek -Pichi- Vychodil
- 26,174
- 5
- 52
- 73

BAR
- 15,909
- 27
- 97
- 185
-
4I don't know Erlang very well, but in C `calloc()` isn't all that useful. Well-written C code that avoids reading array elements that it hasn't written can use `malloc()` rather than `calloc()`. And `calloc()` isn't guaranteed to set pointers to null or floating-point objects to 0.0 (though it happens to do so on most systems). – Keith Thompson Feb 02 '13 at 22:19
-
Keith, you should post that as an answer. Answers as comments are not a very good idea since it defeats the purpose of the Q&A system. – Emil Vikström Feb 08 '13 at 09:58
-
So your saying since well written c code will have set array elements itself, that there is no need for calloc? – BAR Feb 08 '13 at 15:15
1 Answers
2
Well, I assume this has to do with the fact (from documentation):
"NIFs where introduced in R13B03 as an experimental feature."
On a side note, the Erlang review board is also quite conservative, probably due to the history of being born from a telelcom company, and that is understandable for fault tolerant languages.
I guess I don't see the problem you have since you are always welcome to write a proper port using calloc() and use the old marshaling methods. http://www.erlang.org/doc/tutorial/erl_interface.html
Happy C coding!

ngunder
- 36
- 1
-
Congrats on registering. I was under the impression NIF's would be fastest. On my system I get a 7 us overhead for calling one. I wonder what that will be with a port, I assume if it's a system port it will be close to the same speed. But at least I still get that raw c power - born of pure c. – BAR Feb 16 '13 at 22:38