Re:Conflating pointers with arrays: C's biggest mistake? (2009)
by drfuchs on Sunday July 22, 2018 @03:45AM
(#17585714)
You don't seem to understand heap vs. stack. The call to malloc() does a heap allocation, and the question was "without using the heap in any way". If it makes it conceptually easier, how about a solution where the string is simply a global variable? No malloc allowed.
[ Parent
| on HN ]
Re:Conflating pointers with arrays: C's biggest mistake? (2009)
by vkazanov on Sunday July 22, 2018 @03:53AM
(#17585744)
You don't seem to understand who you are arguing with :-)
Re:Conflating pointers with arrays: C's biggest mistake? (2009)
by geezerjay on Sunday July 22, 2018 @04:34AM
(#17585862)
> You don't seem to understand heap vs. stack.
Please check who you are replying to.
Re:Conflating pointers with arrays: C's biggest mistake? (2009)
by jkabrg on Sunday July 22, 2018 @05:25AM
(#17585985)
WalterBright posted the following code snippet somewhere above:
char foo[9]; foo[] = "abc"; foo[3..3+5] = "1234";
`foo` is an array. `foo[3..8]` is a slice, which is an object that does its own bounds-checking. I don't think the heap is used here.Another explicit example:
char foo[5] = "abcd"; // still NUL-terminated, carries length aswell
char[] bar = foo[2..3]; // a fat pointer with length 1
bar[0] = 'C';
printf(foo); // abCd
bar[1] = 'D'// ERROR!!! bar has length 1
Note that the array `foo` is now bounds-checked, which may affect backwards-compatibility. Also, `bar` is no longer null-terminated, which means you can't do printf on it.
Re:Conflating pointers with arrays: C's biggest mistake? (2009)
by WalterBright on Sunday July 22, 2018 @03:49AM
(#17585726)
I showed you how to do that in another reply.
Re:Conflating pointers with arrays: C's biggest mistake? (2009)
by acehreli on Sunday July 22, 2018 @09:33AM
(#17586636)
Perfectionism is not engineering. Not finding a way to fix every use case that you can come up with is not an excuse to insist on unsafe features that are proven to be disasters in practice.