5 ms·
bufsize += LSH_RL_BUFSIZE; Shouldn't that be: bufsize += LSH_RL_BUFSIZE * sizeof(char);
by soheil 6y ago
bufsize += LSH_RL_BUFSIZE;
Shouldn't that be:
bufsize += LSH_RL_BUFSIZE * sizeof(char);
- ChrisRR 6y agoProbably but a char is often 1 byte so it doesn't make a difference
- soheil 6y agoSure but he does it the first time
- amelius 6y agoI think the multiplication should be here: buffer = realloc(buffer, bufsize * sizeof(char)); This makes the code symmetric wrt. the malloc() call. And better allows the code to be rewritten when moving to e.g. a wide-character type.
- messe 6y ago> often 1 byte char is always 1-byte according to the C standard. (Actually, I think it's more like the smallest addressable unit is 1-char, but it's been I while since I've read the spec)
- vlovich123 6y agoIt is now. There were more esoteric platforms for which it was defined as 2 bytes (eg csr8675 and the Kalimba DSP coprocessor was even 24-bit char, short and ints). Until recently C only defined that char was the smallest unit and had size 1. I think as of C11 that may have changed. It definitely did in C++ because basically any platform that mattered defined char as 1 byte and any platforms that didn’t also didn’t really care about portability of existing software that much as the porting is generally more involved anyway (eg I ported a crypto library to Kalimba and it was challenging to get the bit math correct but eventually I did).
- messe 6y ago> and the Kalimba DSP coprocessor was even 24-bit char, short and ints Yes, but sizeof char would still be 1, unless I'm mistaken. I think I might have made a major pedagogical mistake when I declared char == 1 byte. What I meant was that when you're working in C, a char is the smallest addressable unit. Now, that smallest addressable unit is conventionally known as a byte, but it might not be 8-bits wide. These days it is almost always is thankfully. Thank you for clarifying that, because I don't think my original comment was clear enough. I've been meaning for a while to write a blog post on the origins of 8-bit bytes as well as other conventions in CS.
- gens 6y agoEDIT: I was wrong, a char is always sizeof 1, See https://news.ycombinator.com/item?id=26131228 https://news.ycombinator.com/item?id=26131228 // old reply: I already replied to nanis above, so i+l just c/p it here. CHAR_BIT is a minimum of 1 byte (8 bits), but is not guaranteed to be 1. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf (Annex E)
- uhhhhhhhhhhhhhh 6y agoWhat if there were hardware with a smallest adressable size of N bits, but its next size up were a noninteger multiple of N?
- nrclark 6y agosizeof(char) is 1, always. By definition. Sizeof()’s return answer is in units of “number of chars”.
- shakna 6y agosizeof(char) is a _minimum_ of 1, always, by definition. But it is not always 1, though most modern platforms are. > Their implementation-defined values shall be equal or greater in magnitude... - ISO/IEC 9899:201x, Annex E, 5.2.4.2.1
- layoutIfNeeded 6y agoWrong. The quoted part of the standard doesn’t talk about sizeof(char). Sizeof returns the size of its operand in char-sized units, therefore sizeof(char) is tautologically 1.
- ufo 6y agoThe question I had is if it shouldn't be using some kind of *= instead of +=, so it's amortized constant time.
- nanis 6y ago> Shouldn't that be: > bufsize += LSH_RL_BUFSIZE * sizeof(char); sizeof(char) is always and everywhere 1.