Author Topic: Built-in managed pointers  (Read 145 times)

lerno

  • Full Member
  • ***
  • Posts: 239
    • View Profile
Built-in managed pointers
« on: March 22, 2019, 11:15:17 AM »
Taking a hint from Cyclone, Rust etc one could consider managed pointers / objects. There are several possibilities:

1. Introduce something akin to move/borrow syntax with a special pointer type, eg. Foo@ x vs Foo* y and make the code track Foo@ to have unique ownership.
2. Introduce ref-counted objects with ref-counted pointers. Again use Foo@ x vs Foo* y with the latter being unretained. This should be internal refcounting to avoid any of the issues going from retained -> unretained that shared_ptr has. Consequently any struct that is RC:ed needs to be explicitly declared as such.
3. Managed pointers: you alloc and the pointer gets a unique address that will always be invalid after use. Any overflows will be detected, but use of managed pointers is slower due to redirect and check.

Sample code for (2)
Code: [Select]
type Foo struct @(refcounted) {
   i32 a;
}

func Bar(Foo@ a)
{
    printf("%d\n", sizeof(Foo)); // prints 8 due to RC
    printf("%d\n", rc(a)); // prints 1
    Foo@ x = a;
    printf("%d\n", rc(a)); // prints 2
    x = nil;
    printf("%d\n", rc(a)); // prints 1
    Foo* y = a;
    printf("%d\n", rc(a)); // prints 1
    Foo* z = malloc(sizeof(Foo)); // Not recommended!
    // TOOD discuss how to properly initialize a RC:ed variable.
}


bas

  • Full Member
  • ***
  • Posts: 217
    • View Profile
Re: Built-in managed pointers
« Reply #1 on: March 24, 2019, 11:18:44 AM »
Managed pointers are part of the core design of a language. Rust for example was built around some
management ideas. In those languages you cannot do without something like this. I don't see how
introducing an ideas like this wouldn't alter a C-like language completely.

I think programming in C requires some skill and experience, but like handling a chainsaw. :)
I prefer cutting a tree with a chainsaw (never actually tried though) than with a plastic toy.. (Not comparing
Rust with a toy!). Once you have some experience in C, memory management is not that hard.

lerno

  • Full Member
  • ***
  • Posts: 239
    • View Profile
Re: Built-in managed pointers
« Reply #2 on: March 24, 2019, 05:19:15 PM »
Rust is very high maintenance in regards to memory management, but I think we should look for a small effort that can take us maybe 70% of the way.