C, to me, requires a lot of discipline about good coding style and conventions. I personally like that a lot, even though, of course, it sometimes causes one to shoot oneself in the foot. But, and I can't point to exactly why, I don't think I want to change anything. Maybe Rust or something new will come along sometime soon to fix all the issues that exist with C. Oh, and for those who haven't seen it, there was a co…
Fixing C
81–90 of 123 posts
Re: Fixing C
#82Earlier quoted context omitted.
You can sizeof them if the declaration is in scope. If you pass them as function argument, this information is not obtainable inside function. You have to pass length as well. This is not very elegant and sometimes you elect to process them without using functions just because you can iterate them easier.
Now, at one point, you stopped talking about arrays and started talking about pointers.
Worse, it's an easy way to get burned on the sizeof() function, especially if you at some point refactor the code and put that chunk in a function separate from the original declaration.
This is why C programmers get gunshy about relying on that information and instead just treat strings like pointers all of the time.
Re: Fixing C
#83Earlier quoted context omitted.
.. until you pass them as a function argument. And strlen() only works if your null terminator is valid, and doesn't tell you anything about available space in the string buffer for adding to the string. This kind of thing keeps coming up in vulnerability reports.
This happens because people don't use the tools properly.
Re: Fixing C
#84Re: Fixing C
#85Earlier quoted context omitted.
#include int main(int argc, char argv) { const int *b = 9; b = 10; printf("%d",b); } what does (should) this print? (It compiles with warnings on llvm 7.3 / clang703 OSX)
I like that you have an example, but I'm not getting the results you are. Perhaps there is some oddity with your odd use of pointers? I switched the code to this: #include int main(int argc, char **argv) { const int b = 9; b = 10; printf("%d",b); } And I get a very clear "error" and nothing compiled with gcc, icc, and clang. Did you maybe have a left over binary from a previous compilation? I didn't try OSX, but it's…
Re: Fixing C
#86Earlier quoted context omitted.
#include int main(int argc, char argv) { const int *b = 9; b = 10; printf("%d",b); } what does (should) this print? (It compiles with warnings on llvm 7.3 / clang703 OSX)
It will print 10. There's absolutely no issue there at all. You've changed the value of the pointer which is NOT const. const int \*b Means a pointer to a thing that is const. The pointer itself (which is on the stack in this case) is NOT const. const int *b = 9; *b = 10; ^^^ This will NOT compile. const int b = 9; int main() { int *a = (int *)b; *a = 10; } ^^^ This will compile (possibly with warnings). If you run i…
Re: Fixing C
#87Earlier quoted context omitted.
YOUR ACTIONS SPEAK SO LOUD I CANNOT HEAR WHAT YOU ARE SAYING. git clone --depth=1 git@github.com:git/git.git find git -name '*.c' -type f | xargs grep -P '^( {4}|\t){4}' | wc -l 11375 git clone --depth=1 git@github.com:torvalds/linux.git find linux -name '*.c' -type f | xargs grep -P '^( {4}|\t){4}' | wc -l 732937 Stupid shitty broken software.
Oh, stop :p "$ find git -name .c -type f | xargs fgrep '{' | wc -l" 19288 "$ find linux -name .c -type f | xargs fgrep '{' | wc -l" 1408368
Re: Fixing C
#88Closures. This is the only thing that I miss in C. I always want to declare local functions, and return pointers to them.
Re: Fixing C
#89C's biggest mistake: http://www.drdobbs.com/architecture-and-design/cs-biggest-mi...
That's definitely the best idea I've seen.
Re: Fixing C
#90(Braces though? Really?!?)