Discover why your tilemap numbers appear `random` in C+ + and how to fix this common pitfall.
---
This video is based on the question https://stackoverflow.com/q/72523412/ asked by the user 'BLSPR' ( https://stackoverflow.com/u/16844067/ ) and on the answer https://stackoverflow.com/a/72523426/ provided by the user 'David Schwartz' ( https://stackoverflow.com/u/721269/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Storing numbers manually into int ** makes some numbers random | c+ +
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding the Problem: C+ + Random Numbers in Array Initialization
When creating a tilemap for a video game, you may encounter the challenge of numbers appearing random when you store them in a 2D array. This frustrating experience can often arise from how you are managing memory and variables in your C+ + code. In this guide, we will explore a specific example that leads to this issue and provide a clear solution to help you overcome it.
The Specific Code and Its Behavior
You have a method called get_story_map(int lvl), which initializes a 2D array (or a pointer to a pointer in C+ + ) with various hardcoded values representing a level in your game. However, upon execution, you notice that certain values in the output appear incorrect or even random.
Example Code
Here's a snippet of the problematic code:
[[See Video to Reveal this Text or Code Snippet]]
When you call this function in your main() method, the first few numbers display correctly, but others seem to generate random values. The output reveals a mix of your intended values and strange, unexpected numbers.
Identifying the Core Issue
The root cause of your problem lies in how you handle the local arrays (line01, line02, etc.). These arrays are declared within the scope of the get_story_map function, making them local to that function.
Key Insight: Pointers and Scope
Here’s the core issue:
When the function get_story_map finishes executing, the local arrays go out of scope and are destroyed.
Consequently, the pointers (map[0], map[1], etc.) in the map array now point to memory that is no longer valid, resulting in undefined behavior.
How to Fix the Problem: Use Dynamic Memory Allocation
To resolve this issue, you need to ensure that the arrays remain in scope for as long as you need to access them. This can be accomplished by allocating the arrays dynamically, so they stay valid until explicitly deallocated.
Proposed Solution
Here's an updated version of your original function:
[[See Video to Reveal this Text or Code Snippet]]
Summary: Always Manage Memory Wisely
In C+ + , it’s crucial to understand how pointers interact with scope and memory management. By dynamically allocating your arrays, you prevent them from going out of scope prematurely and causing undefined behavior in your programs.
Remember:
Scope Matters: Always be aware of the lifetime of your variables.
Use Dynamic Allocation: When dealing with pointers, consider using new to allocate memory that persists beyond the function’s scope.
Deallocate Memory: Don’t forget to free any dynamically allocated memory when it’s no longer needed to avoid memory leaks.
Now, with this knowledge in your toolkit, you can continue developing your tilemap and ensure that your game functions correctly without unintended behavior arising from memory issues.
Информация по комментариям в разработке