21.07.2016, 13:23
Quote:
Today I learned that the fact that PAWN arranges static/global data linearly can be 'abused'.
Code:
static hello[] = {''H','e','l','l','o'}, world[]={' ', 'W', 'o', 'r', 'l', 'd', '!', 0}; print(hello); The main potential I see for this idea is you can avoid repeated addtiton of a constant to an array. Code:
static str[144] = "123_"; while(pos < 100) str[pos + 3] = some_other_String[pos++]; print(str); Code:
static prefix[4] = {'1', '2', '3', '_'}, str[144]; while(pos < 100) str[pos] = some_other_String[pos++]; print(prefix); //will print the contents of str along with the contents of prefix since prefix is not null terminated. |
Quote:
Fixed a typo in the example. You can avoid the +3 and thats it!
Of course, its stupid to do such useless optimizations because it murders the readablity. In the example, I try to copy a string to another string such that the destination string has a prefix "123_". In the first code, I had to add +3 while assigning the array element in every iteration so that the first 4 characters are skipped whereas in the second code doesn't need it at all. The example is just bad... strins would do it faster. I couldn't find a better example. I now feel guilty for calling it "main potential". |