2 ms·
> Sure ownership protects against more things. But some coding patterns are impossible under it. In Rust you're told that if it's impossible under ownership th
by LoganDark 3y ago
> Sure ownership protects against more things. But some coding patterns are impossible under it.
In Rust you're told that if it's impossible under ownership then you should find a different way to express it rather than trying to circumvent ownership. I guess it's different in C.
- thradams 3y agoIt works in both ways. We can just tell the compiler to ignore some function. We can be also creative writing the code that at same time is good and makes the static analysis happy. A good sample is linked lists. #include <ownership.h> #include <stdlib.h> #include <assert.h> #include <stdio.h> #include <string.h> struct node { char * owner text; struct node* owner next; }; struct list { struct node * owner head; struct node * tail; }; void list_append(struct list* list, struct node* owner node) { if (list->head == NULL) { list->head = node; } else { assert(list->tail->next == 0); list->tail->next = node; //ZERO OVERHEAD } list->tail = node; } void list_destroy(struct list* obj_owner list) { struct node * owner p = list->head; while (p) { struct node * owner next = p->next; free(p->text); free(p); p = next; } } void list_print(const struct list* list) { const struct node * p = list->head; while (p) { printf("%s ", p->text); p = p->next; } } int main() { struct list list = {}; struct node * owner p = calloc(1, sizeof * p); if (p) { p->text = strdup("item1"); list_append(&list, p); } list_print(&list); list_destroy(&list); }