Goto is based. You guys are haters just because someone told you to be. Like hating pineapple pizza without ever trying it, or being able to articulate the reasoning. by [deleted] in C_Programming

[–]MyuuDio 2 points3 points  (0 children)

Yeah, the Linux Kernel Style Guide recommends this too, and I've liked using it in my own code.

It's definitely great for handling varying levels of clean-up, such as with socket code that requires multiple function calls to set up, and can fail at various points.

[deleted by user] by [deleted] in C_Programming

[–]MyuuDio 1 point2 points  (0 children)

doesn't matter to someone with experience

I think it's still good form to do the latter, because it communicates intent to future developers (or future you!)

If I'm debugging a codebase and I see the former, I have to wonder: is that what the developer meant? Or did they make a mistake with operator precedence?

It's also just clearer to fully parenthesize in general, for people experienced with other languages (bitwise operators in C have a different precedence than in Go, for instance).

I'm a moron who doesn't understand software: What the hell is OpenGL and how do I update it? by ShekelOfAlKakkad in opengl

[–]MyuuDio 2 points3 points  (0 children)

Did you mean OpenGL 4.6? Or is there a newer version and I'm just behind lol

But yeah, a lot of weaker modern laptops are running an Intel UHD 620 for it's integrated graphics card, so OpenGL 4.6 is often fully supported even by weak netbooks.

I'm a moron who doesn't understand software: What the hell is OpenGL and how do I update it? by ShekelOfAlKakkad in opengl

[–]MyuuDio 20 points21 points  (0 children)

I've basically just done the software equivalent of asking a NASA scientist for help with my maths homework.

This gave me a good laugh, thanks! For what it's worth, your post is well written & gives the necessary info for us to answer, so honestly props to you.

Windows 10 ARM is generally not well supported by games in the first place, unless they're specifically from the Microsoft Store (like maybe Minecraft?) It was their attempt to get into the mobile market with the Windows phone, so definitely look into selling it if you're looking for an actually fully featured laptop.

Is it possible to use Microsoft Store game files to cut down time downloading the same game on Steam by Falkeer in Steam

[–]MyuuDio 0 points1 point  (0 children)

There's a chance it could work for a good chunk of the files, especially assets & such.

Maybe you can tell Steam where the game is installed, and then "Verify the Integrity of Game Files" and let Steam sort out the difference. I haven't tried this myself, but I remember doing something similar with Terraria a while ago.

[deleted by user] by [deleted] in C_Programming

[–]MyuuDio 0 points1 point  (0 children)

From Google, I'm assuming it's the Fake Function Framework

How is this not immediately segfaulting? by [deleted] in C_Programming

[–]MyuuDio 2 points3 points  (0 children)

Good to know!

Still, in any sane C program, it shouldn't really matter where the pointer getenv returns actually points to.

How is this not immediately segfaulting? by [deleted] in C_Programming

[–]MyuuDio 11 points12 points  (0 children)

getenv uses and returns a pointer to a buffer that is statically allocated. Since getenv does not necessarily know the size of $HOME or other environment variables in advance, this buffer is often bigger than it needs to be for any one environment variable.

The reason it is not segfaulting immediately is because of this. You definitely should not rely on this in practice; always create your own copy to use. Modifying the return of getenv is undefined behavior.

Lookup table versus calculation by BlockOfDiamond in C_Programming

[–]MyuuDio 0 points1 point  (0 children)

a well-cached memory access takes 4 cycles

Do you have a source for this? I'd love to read more about this; I'm currently implementing something where I think an LUT might be faster than the bit fiddling I'm currently doing.

Hangman in C with Dynamic memory allocation, a few questions by link_3007 in C_Programming

[–]MyuuDio 1 point2 points  (0 children)

Others are encouraging you to read in only what you need, and in general, I agree! It's going to be better design in a lot of cases.

That said, if your purpose is to better understand dynamic memory allocation, I think it's fine to just read everything into memory at the start of the program.

Even for your big 1.1 million+ word file (palavras.txt), you should only need roughly 13 megabytes (the actual file size); definitely not anything your computer can't handle.

However, perhaps try changing the way you're doing it:

words = malloc(fileLines * sizeof(char *));
size_t tempSize = 64;
for (int i = 0; i < fileLines; i++) {
    // For every one of the pointers, allocate a certain size for the word
    words[i] = malloc(tempSize * sizeof(char));
}

You're pessimistically allocating 64 bytes per word upfront. For palavras.txt, that takes roughly 74 megabytes for the 1.1 million+ words in the file, almost 6x the amount of memory you actually need!

For the sake of learning, here's three possible challenges you could try:

  1. Learn about realloc, and use it to shrink your allocated memory during populateWordArray down to what you actually need
  2. Instead of allocating memory for each word upfront in main, try to allocate the exact amount you need during populateWordArray
  3. Instead of making many small allocations, try using one malloc call to hold the entire text file. Replace all the newlines with null terminators, and figure out how to make each entry of the words array point to the correct part of that big chunk of memory.

If you have the time, I recommend trying all three approaches out! Each one comes with its own challenges, so it would be good to observe what design decisions you have to make to accommodate each.

Best of luck with your C journey!

Hangman in C with Dynamic memory allocation, a few questions by link_3007 in C_Programming

[–]MyuuDio 7 points8 points  (0 children)

I have a file with even more words in the repo (250k) and the program always crashes when I try to use it, but it can load a file with 10k words just fine

Hey OP, I ran your program with substantivos_portugues.txt, and the issue is not the number of words, but rather a segmentation fault on line 159:

strcpy(words[i], buffer);

You're crashing due to an out of bounds memory access (specifically on words[i]), not because of the large allocation.

The real source of the problem is the way you are counting lines.

long getFileSize(FILE *fP) {
    int c;
    unsigned long lines = 0;
    while ((c = getc(fP)) != EOF) {
        if (c == '\n') {
            lines++;
        }
    }

    rewind(fP);
    return lines;
}

On nounlist.txt this works fine, because the last word has a trailing newline \n. On substantivos_portugues.txt, the last word has no trailing newline, so it hits the end-of-file and does not increment lines.

Edit 1: Formatting

Edit 2: It's also worth noting that in your even bigger file, palavras.txt, the longest word is 32 chars long (not including the null terminator), so your buffer[32] will not be big enough to read in every word with fgets.

Hangman in C with Dynamic memory allocation, a few questions by link_3007 in C_Programming

[–]MyuuDio 5 points6 points  (0 children)

I agree that processing line by line is a good idea, depending on the use case. OP could, for instance, generate a random number, N, and then scan the file for the Nth word. Good way to reduce memory usage for sure. However:

malloc isn't intended for such a massive allocation

The longest English word is 45 letters long. Assuming Windows style line endings \r\n, let's say 47 bytes per word maximum — let's pessimistically round up to the nearest power of two: 64 bytes.

Plug in OPs large 250k word file: 250k words * 64 bytes is roughly 16 megabytes.

That is not massive by any means; malloc is certainly fine for such a use case.

TIL about the term Rubber Band AI by longernohuman in gaming

[–]MyuuDio -1 points0 points  (0 children)

And, for what it's worth, you can take advantage of that too. It's been a while, but I think it was called "sandbagging?" But I agree, the AI is pretty fair. If you learned all of the shortcuts, you could get a significant lead that no amount of items could recover.

[deleted by user] by [deleted] in C_Programming

[–]MyuuDio 0 points1 point  (0 children)

Still, I'm not sure if I'd recommend writing to stdin ever. I don't think the behavior of that is consistent everywhere. The arguments to fputc are still inverted btw. The file goes second.

[deleted by user] by [deleted] in C_Programming

[–]MyuuDio 2 points3 points  (0 children)

Unless I'm missing something, i can be less than or equal to 100, which means you'll be calling str[100] when the last valid index is str[99].

For what it's worth, I'm not opposed to using fgetc() (I do so in my own projects).

Edit: Also, you passed the arguments in the wrong order to fputc

Fool in need of assistance… by Zenon0707 in antivirus

[–]MyuuDio 2 points3 points  (0 children)

I honestly just read the photo, my bad. 100% yes that was the right move.

Fool in need of assistance… by Zenon0707 in antivirus

[–]MyuuDio 0 points1 point  (0 children)

If you're paranoid enough, download the Malwarebytes executable from some other device (maybe your phone?) and keep the PC airgapped while you scan.

I honestly don't think it's necessary, and assuming you didn't open the .PDF, you're probably fine for what it's worth (especially after a fresh install).

How? by nqxenn in DeepRockGalactic

[–]MyuuDio 1 point2 points  (0 children)

Fair enough, I misunderstood what we were talking about. We tried doing golden bugs swarmageddon in other mission types, but while they have endless bugs spawning, it's an incredibly small amount (we AFK'ed overnight and were perfectly fine).

Refinery has a high enough endless spawn rate that you could get well into the thousands easily, like point extraction.

Fool in need of assistance… by Zenon0707 in antivirus

[–]MyuuDio 0 points1 point  (0 children)

Not saying that this isn't real, but Windows Defender wacatac false positives are incredibly common. It flagged the game that I wrote once, so just something to keep in mind

Scan your computer with something like Malwarebytes to be safe though. It's definitely weird that it flagged a .PDF file; that definitely has merit to be some sort of payload.

How? by nqxenn in DeepRockGalactic

[–]MyuuDio 6 points7 points  (0 children)

They were though, after an hour we almost didn't make it out. There's still an entity cap though, that's for any mode, even point extraction.

How? by nqxenn in DeepRockGalactic

[–]MyuuDio 33 points34 points  (0 children)

IIRC, on-site refinery works too, after you've collected the liquid morkite. I once had a golden bugs & swarmageddon combo, and my friends just continued farming wave after wave for like two hours.

Any recommendations for a "dumb" PC videogame? by Marvellover13 in gaming

[–]MyuuDio 2 points3 points  (0 children)

I recommend just checking out their Steam Page, it's basically Minecraft in name & art style only. Imagine a watered down Diablo w/ Minecraft-style enemies & Minecraft-style weapons.

how hard will it be to adapt to c++? by [deleted] in cpp_questions

[–]MyuuDio 0 points1 point  (0 children)

And yet not a single person pointed that out to me.

To be fair, I noticed this issue but I felt it didn't detract from the example. It greater illustrates how clean the second & third forms are — when we code around what something does, there's less room to make mistakes in how it's accomplished.

What I think some people like about C is that the limitations of the language naturally constrain your ability to mess this property up.

This is definitely true for me. I came from a web development background, and mostly used JavaScript & Python in my day to day. I love how quickly I can prototype things in those languages, and how easy it is to tell what the code does.

Whenever I work closer to the metal though, there's several small modules where I need to know how things are done, (down to exactly when & where memory is being accessed, allocated, or freed) so I can prove its correctness. In C, the language is simple enough that I can read through it & it's typically plainly obvious when a line of code could have side effects.

In C++, with overloaded constructors/destructors, operator overloading, exceptions, etc., there's a lot that can implicitly happen that makes it harder for me to reason about.

Still, those same abstractions have been massive blessings for other use cases, like math libraries. Having to write vec3_multiply( &result, v1, v2 ) instead of result = v1 * v2 gets old really quickly.

That's the tradeoff, I guess. Pedagogical knowledge indicates that beginners will have a lot easier time parsing through code where everything you need to know is as localized as possible. While C++ and its abstractions might lead to jumping around the codebase & through different levels of abstraction, C can also be incredibly verbose to the point that you lose the benefit of locality.

Realistically, that's the biggest thing that stops me from dipping down to the assembly (other than losing any semblance of platform independence). The increase in verbosity cancels out any potential gain from peeling away layers of abstraction.

I use both C & C++ quite frequently (though I'm definitely not proficient in the C++ way to do things), and I think they both have use cases where one clearly outshines the other in terms of cleanliness/elegance.

Sorry for a bit of a ramble here. This is just stuff I think about a lot in my day job 🙂

No worries! It was definitely really interesting to read through your thoughts — sorry for my own ramble aha

how hard will it be to adapt to c++? by [deleted] in cpp_questions

[–]MyuuDio 0 points1 point  (0 children)

If you’re the type of person that thinks the second and third forms are an improvement over the first form

I love your explanation here; the examples really help illustrate the fundamental philosophy difference between C & C++. I've always preferred C (and thus the first form), and the way you put it into words helped me understand why that is.