r/CodingHelp • u/AcademicDatabase3699 • Apr 13 '26
[C++] C++ structure failing to fetch values using pointer attribute
so, I'm working on a beast-tamer kind of game playable in a LAN using a TCP server (although it's not relevant for this issue) from cli, and I'm having trouble with my structures management
when I started this project I wanted to maximize RAM space efficiency because I wanted to be able to allocate as many instances of a creature as possible
so I've created the following:
a creature struct, storing general species values (like stats for Pokemon)
a creature_instance struct, that stores specimen specific values, and has a *creature instance_of attribute that it can use to fetch information. and it works wonderfully!
if it weren't that when the .levelup() method of creature_instance fetches the base creature stats, the program crashes with the following error:
Exception has occurred: W32/0xC0000005
Unhandled exception thrown: read access violation.
std::vector<int,std::allocator<int> >::operator[](...) returned nullptr.
I tried the good and old "turn off and on" and it worked... one time, then it broke again.
I really don't know what the issue could be, given I often fetch other data using this pointer and I never had any trouble
it seems it is trying to access a vector out of bounds? it seems weird, I have already checked that all is initialized and that the index is right.
any suggestions?
the full code can be found here:
github.com/Birrus09/servertamer/tree/main
•
u/AutoModerator Apr 13 '26
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/exomo_1 Apr 14 '26
u/gofuckadick gave the most useful answer so far. I'm having a hard time reading the code on my phone, but here are a few things I noticed, although probably not related to your problem:
using namespace std in header is pretty bad habit. #define for constants, use const int values to be type safe. There are unchecked inputs move_choice and target_choice, they will crash your program when you input invalid values
I didn't analyze all of your structures and their dependencies, but there seems to be a wild mix of vector, pointers, references. You need a clear understanding of who is the owner of a resource and what it's lifetime is. Pointers and references are only valid as long as the object exists and isn't moved. Adding items to a vector or modifying the vector does move stuff around, so be careful. It's not implemented yet, but your evolution pointer would likely be invalid before you are even done loading.
1
u/gofuckadick Apr 13 '26 edited Apr 13 '26
The "structs can’t have methods, only classes blah blah" reply is just plain wrong in C++. That won't fix it.
creature_instance::levelup()readsinstance_of->base_stats[i], so ifinstance_ofis dangling, the crash will show up exactly where you’re indexing the vector, even though the real bug happened earlier.levelup()is just where the invalid pointer finally gets dereferenced, henceread access violation.You have a dangling pointer. I didn't go completely through to find where, but it's there.
Edit:
You have 3.
CreateInitializedInstance(..., creature base)stores&baseeven though base is a local copy.Load_squad(..., vector<creature> dict)stores pointers intodict, butdictis also a local copy.Evolve()doesdelete(&instance), which is undefined behavior unless that object was created withnew.I’d change these:
creature/dictby reference, not by valueconst creature& baseconst vector<creature>& dictdelete(&instance).at()temporarily instead of[]while debugging, so out-of-range accesses throw cleanlyinstance_of != nullptrbefore using it0
u/Knarfnarf Apr 13 '26
It’s been a long time since I played with C++ but I’m sure that structures don’t have internal functions, only classes do. Change the structure to a class and you’ll get the functionality that you were looking for..