←back to thread

1371 points yett | 5 comments | | HN request time: 0.792s | source
Show context
db48x ◴[] No.43772686[source]
[flagged]
replies(8): >>43772700 #>>43772801 #>>43772908 #>>43773054 #>>43773261 #>>43774132 #>>43774982 #>>43777757 #
mschuster91 ◴[] No.43772801[source]
To u/db48x whose post got flagged and doesn't reappear despite me vouching for it as I think they have a point (at least for modern games): GTA San Andreas was released in 2004. Back then, YAML was in its infancy (2001) and JSON was only standardized informally in 2006, and XML wasn't something widely used outside of the Java world.

On top of that, the hardware requirements (256MB of system RAM, and the PlayStation 2 only had 32MB) made it enough of a challenge to get the game running at all. Throwing in a heavyweight parsing library for either of these three languages was out of the question.

replies(11): >>43772845 #>>43772895 #>>43773040 #>>43773086 #>>43773123 #>>43773186 #>>43773306 #>>43773332 #>>43773461 #>>43774704 #>>43775548 #
Magma7404 ◴[] No.43772895[source]
The comment reappeared, and while you're right about using proper libraries to handle data, it doesn't excuse the "undefined behavior (uninitialized local variables)" that I still see all the time despite all the warning and error flags that can be fed to the compiler.

Most of the time, the programmers who do this do not follow the simple rule that Stroustrup said which is to define or initialize a variable where you declare it (i.e. declare it before using it), and which would solve a lot of bugs in C++.

replies(3): >>43773025 #>>43773146 #>>43773364 #
1. trinix912 ◴[] No.43773364[source]
While it doesn't excuse the bad habits, we do have to keep in mind C++98 (or whatever more ancient was used back then) didn't have the simple initializers we now take for granted. You couldn't just do 'Type myStruct = {};' to null-initialize it, you had to manually NULL all nested fields. God forbid you change the order of the variables in the struct if you're nesting them and forget to update it everywhere. It was just considerably more practical to do 'Type myStruct;' then set the fields when needed.
replies(2): >>43773458 #>>43775233 #
2. badc0ffee ◴[] No.43773458[source]
You could always `bzero` or `memset` the entire struct to 0.
replies(1): >>43773484 #
3. trinix912 ◴[] No.43773484[source]
But only if it contains strictly POD members, otherwise it's UB.
4. mrighele ◴[] No.43775233[source]
I haven't been using C++ for a number of years but I think you could set the default values of fields even back then. Something like

    struct test {
        int my_int = 0;
        int* my_ptr = std::nullptr;
    };
Or is this something more recent ?

You cannot initialize them with a different value unless you also write a constructor, but it not the issue here (since you are supposed to read them from the file system)

replies(1): >>43775923 #
5. MrRadar ◴[] No.43775923[source]
That's C++11 syntax. Before then you'd have to manually initialize them in every constructor, with a hand-written default constructor as a minimum:

    struct test {
        int my_int;
        int *my_ptr;
        
        test() : my_int(0), my_ptr(NULL) {}
    };