-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modernize: Move some arrays to std::array #412
base: main
Are you sure you want to change the base?
Conversation
77ffd76
to
8f911c9
Compare
Also replaces a macro with a template function
cd12329
to
86ec6c3
Compare
86ec6c3
to
a46f0f0
Compare
I don't see value of these changes. If you trying to solve particular issue, focus on particular part of code. General rule of thumb: "Don't fix that aren't broken". |
Bugfixes - In `ObjInitGeneric` in for loop where `obj_info` wasn't being used. - Twice in WeaponFire.cpp where `fq.thisobjnum` was being assigned `Objects - obj` instead of `obj - Objects`
That's your mistake because there are currently significant issues open that are likely due to memory access issues. |
Prove it, please. Do you have checked code with Valgrind, is there memory issues on changed code? Do all changed in this PR code suffer these issues? |
The purpose of the changes are to ensure that if a memory violation occurs that it will immediately be caught rather than continue, possibly crashing at a later point. I don't understand why you are being so oppositional to improving the code's reliability. Also, I did point out that I found and fixed some bugs while doing this. |
@winterheart |
@@ -1625,7 +1625,7 @@ const char *const Ai_movement_subtype_walking_strings[MAX_AI_INIT_MOVEMENT_SUBTY | |||
#define AI_MAX_MELEE_RANGE 5.0f | |||
|
|||
int AI_NumRendered; | |||
int AI_RenderedList[MAX_OBJECTS]; | |||
std::array<int, MAX_OBJECTS> AI_RenderedList; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as we're doing these conversions, let's default-initialize the elements in the arrays:
std::array<int, MAX_OBJECTS> AI_RenderedList; | |
std::array<int, MAX_OBJECTS> AI_RenderedList{}; |
Both T[N]
and std::array<T, N>
default-initialize only the array object, not the array elements. IE, this gives an indeterminate value:
std::array<int, 4> arr;
int val = arr[0];
// val is indeterminate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean... OK but I was expecting most of these to eventually be changed to std::vector
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vectors would be preferable, definitely. I'm fine with not adding the explicit initialization in this changeset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noticed what i think is a problem in scripts/LEVEL15.cpp
- added array initialization objects. maybe some find-replace artifacts?
don't love the added [[gnu::packed]]
attribute in Descent3/aistruct.h
either.
beyond the above, added some suggestions but this looks largely fine to me.
@@ -713,17 +715,17 @@ struct ain_weapon_hit_info { | |||
|
|||
#define AI_MEM_DEPTH 5 | |||
|
|||
struct ai_mem { | |||
struct [[gnu::packed]] ai_mem { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is struct packing needed? without context, this seems smelly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also [[gnu::packed]] isn't portable, MSVC will just ignore the unknown attribute with warning C5030: attribute [[gnu::packed]] is not recognized.
I believe #pragma pack
is supported by all recent compiler versions nowadays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tophyr this was part of something else I was working on.
@pzychotic MSVC is perpetually behind both GCC and clang. You would be better off using clang/llvm on windows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It isn't about what I use, it's about what this project does. I linked to the current CI build log, because we do use MSVC here and that attribute is being ignored in our windows builds. Unless this projects decides to be GCC/Clang only, this issue remains.
Also don't try to tell me which build environment I have to use, just because you don't like it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really a problem because it's being removed.
@GravisZro for what it's worth, I didn't notice any changes here (other than the use of |
The C++ standard doesn't not specify the behavior of accessing nonexistent elements using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is all great, modulo the [[gnu::packed]]
annotation.
But also only in specific debug builds ( |
Pull Request Type
Description
The ultimate goal is to switch several variables to vectors in order to expand game limitations but the first step is to switch C arrays to
std::array
so that we ensure that memory bounds are respected. There is more in-depth work to adapting how the arrays are used but I didn't want to make the PR too large.Other stuff
SAVE_DATA_TABLE
was replaced with a template function versionOBJNUM
macro was used in place ofobj - Objects
ObjInitGeneric
in for loop whereobj_info
wasn't being used.fq.thisobjnum
was being assignedObjects - obj
instead ofobj - Objects
(replaced withOBJNUM
macro)Checklist