Need help with boolean - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Need help with boolean (/showthread.php?tid=3210) |
Need help with boolean - Petr - 11-13-2024 Quick feature description: The program encodes a text string into a binary string that is encoded in five bits. To achieve this, thanks to the MEM functions, I manipulate a variable of type _Integer64, where I fill the first 5 bytes with five bits, thereby placing 8 characters of the text string in these 5 bytes. Subsequently, I save the variable of type Integer64 in memory and in the next step I overwrite its last three bytes. This gives me a five-byte value that is not directly available in QB64. Finally, I extract the valid length from the memory to the string (for 8 characters it is 40 bits, so 5 bytes). During decoding, the coded string is inserted into memory and is read from it using a string of five bytes, and thanks to the MEM options, this variable is inserted into a number of type Integer64. This number is then decomposed back into the individual array indices that determine the specific letters of the encoded string. I have completely avoided the previous Dec2Bin and Bin2Dec practices here that you can see in my old programs. While this whole thing was supposed to be a nice distraction and fun, it turned into a huge embarrassment. I really don't know what I neglected, that's why I want to ask you for help. Now if you run this program as is, it will work correctly. But. Please disable MemFill on line 81 and run it again. Where do the artifacts come from? Where does the new memory array take those bogus values? I am not able to deduce it logically and I do not see it in the program (with my little knowledge of boolean operations). Can someone explain this to me? Code: (Select All)
RE: Need help with boolean - SMcNeill - 11-13-2024 https://qb64phoenix.com/qb64wiki/index.php/MEMNEW _MEMNEW does not clear the data previously in the memory block it allocates, for speed purposes. To clear previous data from a new memory block, use _MEMFILL with a byte value of 0 RE: Need help with boolean - Petr - 11-13-2024 Well, I really didn't know. It didn't make sense to me, I thought - when there is a new memory block, it is simply new and clean, like a newly dimensioned field or a newly created screen.... I see that MEM commands have a different policy. Thank you for the explanation. RE: Need help with boolean - SMcNeill - 11-13-2024 Memnew just grabs a free spot in memory and reserves it for your use. Now if that memory has always been unused, it's probably all 0s. But if it's using a portion of memory that had been used previously and then freed, then it's going to have that old data in it. Most of the time (like when creating an array), data goes through two steps: Reserve it and Initialize it. Since Mem is optimized for speed, it skips that second step. Instead of: Reserve it. Initialize it to 0. Put data in it. It is simply: Reserve it. Put data in it. I'm certain you can see where the second has to be faster than the first when running it. The only problem comes if you read it before you fill it up with your own data. You end up getting junk in it, as you've seen for yourself in your own stuff above. RE: Need help with boolean - bplus - 11-14-2024 Well this is interesting and good to know should I get into memory stuff. RE: Need help with boolean - Pete - 11-14-2024 I always thought the song that went, "Memories... like the corners of my mind..." was for blockheads. Yeah, me too. Thanks for the _memories. I hope to use them someday . Pete RE: Need help with boolean - TempodiBasic - 11-17-2024 Yes a good remark of syntax about _MEMFILL and _MEMNEW It seems that wiki is clear that there is no initialization of memory block using _MEMNEW _Memnew wiki page !!! Arghhh I need more time to read Wiki pages, but the time saved with the right knowledge ( avoiding unexpected results) is more than that spent to read wiki. |