Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Uninitialised variables and timing / race conditions (especially if using threading) come to mind. CppCheck can help as well as -Wall -Wextra.
-
@Fast-Nop
That happens when I am initialising a struct member which has the data type int.
The program successfully scanf's the input and crashes all of the sudden (depending on the compile times). -
@-ANGRY-CLIENT- maybe wrong argument to scanf, one & too much or too few so that scanf writes to somewhere in memory where it shouldn't be writing. Or the format string.
Scanf with strings especially is tricky because just using "%s" readily invites buffer overflows. -
@Fast-Nop I have seen people recommending %a(gcc), %[^\n]s and stuff like that. Tried them. They made no difference so I sticked to %s.
Btw. I am not allowed to use any other libraries, but stdlib.h and stdio.h.
That is what bothers me a lot. -
@-ANGRY-CLIENT- That should work everywhere - note that there is no '&' when scanf'ing a string, and I'd zero terminate manually afterwards because I'm not quite sure whether scanf always puts in a zero when truncating:
char my_string[20];
scanf("%.19s", my_string);
my_string[19] = '\0'; -
@Fast-Nop
Is the dot in .19 a typo?
Afaik it reads until the index 19 shows up.
And should not that be "my_string[20]='\0';"? -
@-ANGRY-CLIENT- oh, yeah, that has to be without the dot.
But the 19 in the last statement is correct because arrays in C start indexing from zero. The declaration declares an array of 20 characters length, and since my_string[0] is the first character, my_string[19] is the 20th. my_string[20] would be a buffer overflow. -
@Fast-Nop how does that lead to a buffer overflow?
char My_string[20]; //21 vars of the same data type. -
@-ANGRY-CLIENT- because "char my_string[20];" declares room for 20 elements, not 21. The indexing in usage starts from zero, but the declaration starts from 1.
That's because "char mystring[0];" declares an array with 0 accessible elements, and while that does have use cases, it's quite rare.
Related Rants
Fuck randomly appearing and disappearing bugs when I compile a project in C with no changes in the source code.
Fuck these microbits in the ass.
Took me three fucking days to battle them. Aaaargh!
rant
a wild "fucking microbit" appeared once again
c