11-13-2024, 10:32 PM
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.
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.