4 ms·
C experiments in Swift
- tambourine_man 12y agoCheck this talk, "Swift and C" by Mike Ash: http://vimeo.com/107707576 http://vimeo.com/107707576
- ankurpatel 12y agoYes that talk is also good. You can see his playground here https://mikeash.com/tmp/%f0%9f%90%8d.zip https://mikeash.com/tmp/%f0%9f%90%8d.zip
- genieyclo 12y agoThanks, I dig the emoji+Swift love.
- pjmlp 12y agoThanks for pointing it out.
- asveikau 12y agoI wonder how longjmp() interacts with ARC. My guess is poorly.
- bitserf 12y agoYou can manually retain / release using Unmanaged, but that seems like a lot of work when you can mix C/C++ and Swift in the same target.
- ankurpatel 12y agoRight if you are using longjmp you probably want to manage memory youself.
- mikeash 12y agoYou also need to use volatile if you expect writes to variables to persist when calling longjmp(), because any value the compiler decides to stash in a register will be overwritten. Since Swift lacks volatile, you won't be able to make that work properly. It's fun to play with though.
- stonewhite 12y agoEverything is great until the flamebait at end. "It is possible to do it all in Swift making it a versatile language unlike Java or Scala" Anybody cares to elaborate on that?
- ankurpatel 12y agoHaving done Java programming I have not been able to write code where I can directly allocate memory in the heap and iterate over it like in C. Correct me if I am wrong with an example.
- virmundi 12y agoI don't think this addresses your question directly, but there is always Unsafe. You can do a lot of direct memory manipulations through it. http://highlyscalable.wordpress.com/2012/02/02/direct-memory-access-in-java/ http://highlyscalable.wordpress.com/2012/02/02/direct-memory...
- ankurpatel 12y agoGood article but trying to write code like in the examples seems very cumbersome compared to writing it in Swift. The Java language syntax is partly to blame.
- panzi 12y agoWhy would you want to do that in Java?
- tedunangst 12y agonew byte[]?
- Dewie 12y ago> where I can directly allocate memory in the heap Doesn't the "new" keyword always heap-allocate? I guess modulo optimizations like "this is a value-class that won't escape the scope of the method: "in-line" it" (if that is a technique that is in use/possible). Also, it seems reasonable that an array of primitive values would actually be represented as a contiguous piece of memory in the heap. Sorry if that wasn't what you asked about.
- sirseal 12y ago"Writing try-catch block using setjmp and longjmp" Great! Now I can have fun with some bad, old programming ideas!
- panzi 12y agoI guess using setjmp/longjmp to implement something like try-catch in C is something everyone does when they discover this the first time. I did. But I was aware that it isn't a good idea, I just wanted to try it. It was some time between 13 and 11 years ago. I guess other computer science students did the same since the 80ies?
- userbinator 12y agoThe most interesting use of setjmp/longjmp I've seen is in QEMU, which uses them to implement coroutines.