12 ms·
How do you know how big your array has to be in a single pass? I don't think the WinXP source uses vectors or similarly ergonomic auto-growing arrays. You could
by wongarsu 6d ago
How do you know how big your array has to be in a single pass? I don't think the WinXP source uses vectors or similarly ergonomic auto-growing arrays. You could preallocate an array big enough for 100 paths of length MAX_PATH, but that's a bit wasteful. And it doesn't sound like you'd actually end up with fewer lines of code (in that flavor of C++, in python it would be different)
- ulrikrasmussen 6d agoYes, you could allocate it on the stack. I think back then (still?) a filename could be at most 260 characters, each encoded with 16 bits, so about 52k of stack allocation.
- wongarsu 6d ago52k on the stack is pretty significant, given Windows defaults to just 1MB stack size per thread
- adrianmonk 6d agoYou could use a linked list. Practically speaking, I might just allocate an array of 100 pointers. That's only 400 bytes. Then as you encounter each filename, allocate just enough memory for the actual length of the string (plus null terminator) and store the pointer in the array.
- ulrikrasmussen 6d agoThat will require a second pass though, because you have to free all your strings again.