Unexpected optimization of strlen when aliasing 2-d array

I checked this and it reproduced with -O1 on gcc 8.3, so I just opened list of gcc optimization flags here and started experimenting with them one by one. It turned out that disabling only sparse conditional constant propagation with -fno-tree-ccp made the problem disappear (oh luck, I planned to test couples of flags if … Read more

strlen in the C preprocessor?

It doesn’t use the preprocessor, but sizeof is resolved at compile time. If your string is in an array, you can use that to determine its length at compile time: static const char string[] = “bob”; #define STRLEN(s) (sizeof(s)/sizeof(s[0])) Keep in mind the fact that STRLEN above will include the null terminator, unlike strlen().

strlen not checking for NULL

The rational behind it is simple — how can you check the length of something that does not exist? Also, unlike “managed languages” there is no expectations the run time system will handle invalid data or data structures correctly. (This type of issue is exactly why more “modern” languages are more popular for non-computation or … Read more

Sizeof vs Strlen

sizeof and strlen() do different things. In this case, your declaration char string[] = “october”; is the same as char string[8] = “october”; so the compiler can tell that the size of string is 8. It does this at compilation time. However, strlen() counts the number of characters in the string at run time. So, … Read more

Add … if string is too long PHP [duplicate]

The PHP way of doing this is simple: $out = strlen($in) > 50 ? substr($in,0,50).”…” : $in; But you can achieve a much nicer effect with this CSS: .ellipsis { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } Now, assuming the element has a fixed width, the browser will automatically break off and add the … … Read more

Why does glibc’s strlen need to be so complicated to run quickly?

You don’t need and you should never write code like that – especially if you’re not a C compiler / standard library vendor. It is code used to implement strlen with some very questionable speed hacks and assumptions (that are not tested with assertions or mentioned in the comments): unsigned long is either 4 or … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)