Re:Tell HN: C Experts Panel – Ask us anything about C
by dooglius on Tuesday April 14, 2020 @10:57AM
(#22866792)
You can very easily make a struct consisting of a pointer and length, is adding such a thing to the standard really a big deal? Personally, I don't see a problem with passing two arguments.
[ Parent
| on HN ]
Re:Tell HN: C Experts Panel – Ask us anything about C
by Someone1234 on Tuesday April 14, 2020 @11:05AM
(#22866857)
- In your example there's no guarantee that the length will be accurate, or that the data hasn't been modified independently elsewhere in the program.
- In other words you've created a fantastic shoe-gun. One update line missed (either length or data, or data re-used outside the struct) and your "simple" struct is a huge headache, including potential security vulnerabilities.
- Re-implementing a common error prone thing is exactly what language improvements should target.
Re:Tell HN: C Experts Panel – Ask us anything about C
by rwmj on Tuesday April 14, 2020 @11:18AM
(#22867032)
I mean, this is C so "fantastic shoe-gun" is part of the territory. But in C you can wrap this vector struct in an abstract data type to try to prevent callers from breaking invariants.
Re:Tell HN: C Experts Panel – Ask us anything about C
by dooglius on Tuesday April 14, 2020 @11:25AM
(#22867125)
>In your example there's no guarantee that the length will be accurate, or that the data hasn't been modified independently elsewhere in the program.
And having a special data-and-length type would make these guarantees... how? You're ultimately going to need to be able to create these objects from bare data and length somehow, so it's a case of garbage-in-garbage-out.
Re:Tell HN: C Experts Panel – Ask us anything about C
by gbear605 on Tuesday April 14, 2020 @12:41PM
(#22868116)
Declaring it with a custom struct:
int raw_arr[4] = {0,0,0,0};
struct SmartArray arr;
arr.length = 4;
arr.val = raw_arr;
some_function(arr);
Smart declaration with custom type: (assume that they'll come up with a good syntax) smart_int_arr arr[4] = {0,0,0,0};
some_function(arr);
With the custom struct, it requires the number `4` to be typed twice manually, while in the second it only needs a single input.
Re:Tell HN: C Experts Panel – Ask us anything about C
by fkeeal on Wednesday April 15, 2020 @06:35PM
(#22883930)
You actually never need to specify the size explicitly in C.
Here are some other ways to declare your struct without needing as much boiler plate per declaration.
#define MAKE_SMARTARRAY(_smartarr, _array) \
do {\
_smartarr.val = (_array);\
_smartarr.len = sizeof((_array))/sizeof((_array[0]));\
}while(0)
struct SmartArray
{
int *val;
int len;
};
int main()
{
int array[] = {0,0,0,0};
struct SmartArray arr = {.val = array,
.len = sizeof(array)/sizeof(array[0])};
struct SmartArray arr2;
struct SmartArray arr3;
MAKE_SMARTARRAY(arr2, ((int[]){5,6,7,8}));
MAKE_SMARTARRAY(arr3, array);
return 0;
}
Re:Tell HN: C Experts Panel – Ask us anything about C
by flatfinger on Tuesday April 14, 2020 @08:45PM
(#22873196)
How about having an attribute which, if applied to a structure that contains a `T*` and an integer type, would allow a `T[]` to be implicitly converted to that structure type?
Re:Tell HN: C Experts Panel – Ask us anything about C
by FpUser on Tuesday April 14, 2020 @12:08PM
(#22867665)
In Delphi/FreePascal there are dynamic arrays (strings included) that are in fact fat pointers that hide inside more info than just length. All opaque types and work just fine with automatic lifecycle control and COW and whatnot.