Skip to main content

Timeline for My first random password generator

Current License: CC BY-SA 4.0

8 events
when toggle format what by license comment
Oct 2, 2019 at 15:54 history edited Baldrickk CC BY-SA 4.0
added 449 characters in body
Oct 2, 2019 at 15:43 comment added Peter Cordes Yes, I'd put that in. Hard-coding a length requires updating 2 things if you ever add another special character, so it's highly frowned-upon and usually only justifiable for performance reasons. An answer that recommends hard-coding lengths / magic numbers isn't very good for coding-style. Since macros allow us to get just as efficient machine code without hard-coding lengths, we should use that. (And as a bonus, it's no worse to read than a runtime strcat + strlen). Your method (selecting a different length) is clearly more elegant and doesn't offend the reader's sense of efficiency.
Oct 2, 2019 at 15:25 comment added Baldrickk godbolt.org/z/T8-gm_ demonstrates this working in the context of this question :) @PeterCordes worth editing into the answer do you think?
Oct 2, 2019 at 15:08 comment added Peter Cordes That's what I said: you can use sizeof for all_character_len but not alphanumerics_len. Good idea though; with macros you could concat everything for the actual definition, or just the alphanumeric part for sizeof("abcdefg...") as a compile-time-constant integer. Yes that works: godbolt.org/z/ikLz_3 I thought I was going to have to cast it to an anonymous array or something. (Just using a string literal as an operand to sizeof doesn't make it part of your program so it doesn't even need to be "optimized away") Oh, another answer on the Q you linked points that out.
Oct 2, 2019 at 15:01 comment added Baldrickk @PeterCordes you can use sizeof (see stackoverflow.com/a/5022113/4022608) but only to find the maximum length, so you would need to have a hard-coded value regardless... There is probably something a little more inventive that can do it, but would possibly impact readability, or memory use. I can think of a readable alternative using the preprocessor, but it would rely on the compiler optimising out the creation of unused arrays, which is a little dirty.
Oct 2, 2019 at 14:57 comment added Peter Cordes If you put the special chars first, you could use strchr(characters, 'a') to find a new start position (and a smart compiler could optimize that to pointer += constant). But choosing just a different length with the same start is most efficient. I guess you could strrchr at runtime if you wanted to avoid hardcoding lengths that match the string contents. Or memrchr if you use an array so total length is a compile-time constant.
Oct 2, 2019 at 14:54 comment added Peter Cordes Too bad C doesn't make it easy to put labels part way through a string so you could have the compiler calculate alphanumerics_len for you. That would be easy in assembly language: just put a label after those bytes, and another label at the end of the string, so you can do end1-start or end2-start instead of hardcoding either length. (You could have used an array so you could at least use sizeof for all_character_len though.)
Oct 2, 2019 at 10:29 history answered Baldrickk CC BY-SA 4.0