3 ms·
if (scanf("%9lu",&len) < 1) barf(); /* >999999999 bytes is bad */ if (getchar() != ':') barf(); buf = malloc(len + 1); /* malloc(0) is not portab
by Joker_vD 3mo ago
if (scanf("%9lu",&len) < 1) barf(); /* >999999999 bytes is bad */
if (getchar() != ':') barf();
buf = malloc(len + 1); /* malloc(0) is not portable */
if (!buf) barf();
if (fread(buf,1,len,stdin) < len) barf();
if (getchar() != ',') barf();
Ah, the wonders of error-handling in C. Also, I wonder what's wrong with
buf = malloc(len ? len : 1);
- deleted 3mo ago[deleted]
- kazinator 3mo agoOr even: if (len == 0) return null_buffer_singleton; /* special shared representation for 0: */
- repiret 3mo agoBecause then the caller needs to treat it differently from non-empty buffers.
- kazinator 3mo agoNo, it doesn't. It calls: void buffer_free(void *buf) { if (buf != null_buffer_singleton) free(buf); }
- Joker_vD 3mo ago...which treats it differently from non-empty buffers :) But frankly, having a 1-byte buffer, pointer to which can serve as a sentinel value à la NULL (but dereferenceable!), and which you can pass to free() without it being deallocated is indeed rather useful.