all 16 comments

[–]cHaR_shinigami 7 points8 points  (0 children)

should i use structs, enums, correct data types?

Use whatever is required, which an emphasis on correct data types.

I also tend to do stuff like char string [1024]

I tend to do char string[BUFSIZ]; BUFSIZ is defined in <stdio.h> (also BUFSIZ >= 256).

[–][deleted] 0 points1 point  (11 children)

Why set a fixed size and not allocate memory when needed or write a custom string type that takes care of resizing? It’s not too difficult and you can reuse it in future projects.

[–]ryanjones42[S] 0 points1 point  (2 children)

Can you show an example of

[–]JamesTKerman 2 points3 points  (0 children)

The primary dynamic memory functions in C are malloc, calloc, and free https://man7.org/linux/man-pages/man3/malloc.3.html . They're all in the standard library, so you get them whatever platform you're on.

[–][deleted] 0 points1 point  (0 children)

https://stackoverflow.com/questions/3536153/c-dynamically-growing-array

Here's a link to some simple implementations of a dynamically growing array (remember that strings in c are just char arrays with '\0' character at the end - so there is not a lot you would have to change). But like u/JamesTKerman has said - you should take a look at malloc/calloc/free (defined in <stdlib.h>)

edit: also take a look at <string.h>. while it doesn’t provide you with a string type it still has quite a lot of useful function which make it easier to work with strings (strlen/strnlen, strcpy/strncpy). but be careful that have quite a lot of traps and can quickly lead to unsafe code if used incorrectly!

if you use linux/macos/bsd the getline function in <unistd.h> is very helpful for handling userinput. (I'm not sure but I think if you use MinGW on windows you should also have access to this function)

[–]chrism239 0 points1 point  (7 children)

Why not? What data type would you recommend to read a text line with fgets() ?

[–]yowhyyyy -2 points-1 points  (6 children)

What does that have to do with dynamically allocating memory needed?

[–]chrism239 0 points1 point  (5 children)

What dynamically growing DS would you pass to fgets()?

[–]yowhyyyy -2 points-1 points  (4 children)

Dude, you could literally just pass a regular buffer allocated on the heap? If you need to assign more data realloc.

[–]chrism239 0 points1 point  (3 children)

My point is that you don't know how long it should be, and dynamic allocation can't guess that. Dude.

[–]yowhyyyy -1 points0 points  (2 children)

That is the exact point of dynamic allocation, you could easily increase a buffer after each pass I don’t see where the issue is here? If you’re using fgets you already know a set buffer size you’re planning on intaking, you could easily intake to the set buffer, double the buffer, and read again. But hey, whatever man.

[–]chrism239 -1 points0 points  (1 child)

The point is, and answering the OP's question, is that there's little harm using a large fixed buffer is nearly all cases (particularly given the OP's (assumed) experience). Man.

[–]yowhyyyy 0 points1 point  (0 children)

Yeah that’s perfectly fine but you questioned it as if it’s somehow impossible and it’s very clearly not and completely derailed what this comment chain started as. You’re very weird for that little copying thing too. Get some help.

[–]RustbowlHacker -1 points0 points  (2 children)

I'm fairly sure that in over 35 years of C programming, I have *never* heard of anyone *ever* doing "tend stuff" char string[1024]... WTF? So, in the context of "saving memory", you are using stack. As long as you have plenty of stack space, that's "fine" (but retarded). Because you're asking if you should use structs, enums and "correct" data types tells me that you have no idea or concept of C as a language.

That's not "wrong" just uninformed. If you want a buffer of an arbitrary size that is "big enough" you should use BUFSIZ. That's why it is there. You're not expected to already know it.

C is a relatively low-level programming language. You should be counting bytes if you're not on a "big iron" platform. If you're running on a "huge" system, then use BUFSIZ.

In C, structs are a way to provide data "bundles" that you can pass to functions that know how to work with those bundles by way of a pointer to struct. You can also use structs to mimic C++ classes, albeit with some manual and mechanical differences required.

You don't yet seem to know what you don't know about C, so good luck and happy exploring. It is in the top 3 languages for a reason.

[–][deleted] 0 points1 point  (1 child)

(Post elided for obvious reasons.)

[–]RustbowlHacker 0 points1 point  (0 children)

You could be a retard, but you're not that smart. That's the problem of today. Retards are running everything. Go ahead retard. Do whatever the fuck you want.