3 ms·
let's say you have a newline delimited dates in &junk. now you do a while loop, whoops quadratic.
by knuthsat 6y ago
let's say you have a newline delimited dates in &junk. now you do a while loop, whoops quadratic.
- nitrogen 6y agojunk is a character, not a string, though.
- JdeBP 6y agoThe calls to sscanf() in the preceding post have been edited since xe wrote that. My guess is that xe meant the parameter now named "input".
- kazinator 6y agoThe valid point is that the junk could be arbitrarily long, and that glibc's sscanf will scan through it in taking the length of the input. But we are not continuing to scan into the junk here to get more tokens here; we don't have a loop that will cause O(N*N) behavior. E.g. this could be user input from a form field where they are expected to type a date, and we want to reject that if there is trailing junk. At that point, the user gets an error, and has to fix the input. We don't look for anything in the trailing junk.
- jemfinch 6y agoAre you not listening to what anyone else is telling you here? Did you not read the article? It's not about "scanning into junk". The problem would happen if you used scanf to extract integers from a string of `' '.join([1]*1000000)`. Because you would make 1,000,000 scanf calls, and _each_ of those calls would call strlen on the string, quadratically scanning over the string to repeatedly find the end. In fact, every sscanf is O(size of the string) even when the string is well formed. This is not a well behaved function.
- cozzyd 6y agoWell, not if you do something like char * start = data; char * next = strchr(data,'\n'); while(next) { *next=0; sscanf(start, ...) start=next+1; char * next = strchr(start,'\n'); }