Snaipe / libcsptr

Smart pointers for the (GNU) C programming language
https://snai.pe/c/c-smart-pointers/
MIT License
1.57k stars 143 forks source link

Requiring smart pointers to be initialized #4

Closed Snaipe closed 9 years ago

Snaipe commented 9 years ago

In C++, unique_ptr and shared_ptr require a value, since they have ownership over it -- but in this case, since we don't wrap a pointer but return actual unintialized memory, I was wondering if the unique_ptr and shared_ptr macro should take a mandatory value and initialize the memory with it.

Usage would be:

smart int *some_int = unique_ptr(int, (1));
smart double *some_array = unique_ptr(double[4], ({1, 2.5, 3.2, 0.999}), some_dtor);
smart long *some_zero_padded_array = unique_ptr(long[25], ({1}), &some_meta, sizeof (some_meta));

struct { 
    int a;
    long b;
    double c;
} s;
smart struct s *some_struct = unique_ptr(struct s, ({ .a = 1, .b = 2 }), dtor, &some_meta, sizeof (some_meta));

Parenthesis over the value are unfortunate, but would be mandatory to allow struct and array literals to be passed, since curly braces are known to not group commas together in macro parameters.

I am still not sure about that to be honest since one could desire to initialize the memory in a more specific way, but to be fair, that's what smalloc should be for.

Snaipe commented 9 years ago

This has been addressed in commit 333c650.