Well, let's see. #1: Avoiding shifting versus doing division: Nope. Crappy compilers and bad architectures will yield bad results. Know what you're doing. #2: Use a typedef instead of a bare C type, then. Also, on many systems space is king (the last embedded system I wrote had 6 bytes left over in code space, and 16-bit operations were incredibly expensive and nearly unaffordable). #3: Just make the code clear. This…
> #1: Avoiding shifting versus doing division: Nope. Crappy compilers and bad architectures will yield bad results. Know what you're doing. Which compiler, among those still used in 2014, does not convert a division by 2 into a shift?
Dangerous Embedded C Coding Standard Rules (2011)
31–40 of 51 posts
Re: Dangerous Embedded C Coding Standard Rules (2011)
#32Earlier quoted context omitted.
> If you need macros to hack C to enable some functionality not inherent to the language, you should change the approach or switch to a different language. I think if you continue to do serious C hacking, you'll find a lot of places where macros are legitimately a good choice. For example, I've found that macros are very, very useful when it comes to implementing error handling in a robust way. In C, the only real wa…
That is almost exactly like some code I have written in a project and at that time it did it's job. After revisiting the project months later after I have completely forgotten the details, I got frustrated over the macro and what did it actually do( even though I have written it ). It took me some time to track down the macro definition and convert the code in my head to understand it again. But if the code would be…
Right-click -> Go to definition?
Your IDE must suck.
Re: Dangerous Embedded C Coding Standard Rules (2011)
#33I'm not completely on board with #4 (initialization of variables). I agree that 'declaring and then assigning a value' _within the same block_ vs 'initialized declaration' has no speed gains, only downsides. However, if the assignment can happen in a _different block_ (maybe inside an 'if' block) you could save 1 memory write, depending on how many times the if condition is satisfied. This obviously optimizes for spe…
Re: Dangerous Embedded C Coding Standard Rules (2011)
#34Another reason to pay attention to #2: Most CPUs perform simple arithmetic operations fastest on their native word size. On a 32-bit machine, addition and subtraction on 8-bit variables is probably slower than on 32-bit variables. (Often the hardware will transparently cast to 32 bits, perform the operation, then cast back to 8 bits.) I'm not an authority on this topic, so I don't want you to take away the message "a…
http://pubs.opengroup.org/onlinepubs/7999959899/basedefs/std...
Re: Dangerous Embedded C Coding Standard Rules (2011)
#35Earlier quoted context omitted.
> If you need macros to hack C to enable some functionality not inherent to the language, you should change the approach or switch to a different language. I think if you continue to do serious C hacking, you'll find a lot of places where macros are legitimately a good choice. For example, I've found that macros are very, very useful when it comes to implementing error handling in a robust way. In C, the only real wa…
The first block of code seems more readable to me than the second. I'm used to seeing the first; it's idiomatic C. I don't understand why it says "do ... while (0)" in the macro, for instance. Wouldn't you want to handle different errors in different ways anyway? In your example, you're just going to leave the file open and the string allocated? Isn't trying to influence how the compiler compiles it by using a macro…
Re: Dangerous Embedded C Coding Standard Rules (2011)
#36Earlier quoted context omitted.
That is almost exactly like some code I have written in a project and at that time it did it's job. After revisiting the project months later after I have completely forgotten the details, I got frustrated over the macro and what did it actually do( even though I have written it ). It took me some time to track down the macro definition and convert the code in my head to understand it again. But if the code would be…
"It took me some time to track down the macro definition " Right-click -> Go to definition? Your IDE must suck.
Re: Dangerous Embedded C Coding Standard Rules (2011)
#37Earlier quoted context omitted.
> If you need macros to hack C to enable some functionality not inherent to the language, you should change the approach or switch to a different language. I think if you continue to do serious C hacking, you'll find a lot of places where macros are legitimately a good choice. For example, I've found that macros are very, very useful when it comes to implementing error handling in a robust way. In C, the only real wa…
The code I used is more like this: if ((f = open_file()) == NULL) { RETURN_ERROR(OPEN_FILE_ERROR); } if ((s == allocate_string()) == NULL) { RETURN_ERROR(ALLOCATE_STRING_ERROR); } if ... And RETURN_ERROR is defined as: #define RETURN_ERROR(my_error) { \ if (f != NULL) { fclose(f); f = NULL; } \ if (s != NULL) { free(s); s = NULL; } \ ... return (my_error); \ }
if(failure_1) {
goto error;
}
if(failure_2) {
goto error;
}
// Do stuff
return 0;
error:
if(f != NULL) {
fclose(f);
}
if(s != NULL) {
free(s);
}
return -1;Re: Dangerous Embedded C Coding Standard Rules (2011)
#38Completely agree with no. 5. After a year with C I realized that macros are only to be used as control structures for headers and ifdefs to enable compatibility. If you need macros to hack C to enable some functionality not inherent to the language, you should change the approach or switch to a different language. Also macros are usually used to "speed up" the code. You should never optimize before finding the real b…
> If you need macros to hack C to enable some functionality not inherent to the language, you should change the approach or switch to a different language. I think if you continue to do serious C hacking, you'll find a lot of places where macros are legitimately a good choice. For example, I've found that macros are very, very useful when it comes to implementing error handling in a robust way. In C, the only real wa…
if ((f = open_file()) == NULL)
return ERROR_CODE;
if ((s = allocate_string()) == NULL)
return ERROR_CODE;
if (write_string_to_file(s, f)
I hope for you that's more 'less' than 'more'. That code leaks resources whenever an error occurs. Can't allocate the string? Oops, kept the file open. Write failed? Oops, kept the file open, and leaked the result of allocate_string.In C, this kind of error handling screams for the use of goto:
int result = ERROR_CODE;
FILE * f = NULL;
char * s = NULL;
if((f = open_file()) == NULL) goto ERR;
if((s = allocate_string() == NULL) goto ERR;
...
result = NOERROR;
ERR:
if( s != NULL) free_string( s);
if( f != NULL) close_file( f);
return result;
Given that you need that to write robust code, I don't think the macros make things much clearer.Re: Dangerous Embedded C Coding Standard Rules (2011)
#39Earlier quoted context omitted.
> #1: Avoiding shifting versus doing division: Nope. Crappy compilers and bad architectures will yield bad results. Know what you're doing. Which compiler, among those still used in 2014, does not convert a division by 2 into a shift?
Memory escapes me...is a shift guaranteed by the standard (and if so, which) to sign-extend or not sign-extend?
Re: Dangerous Embedded C Coding Standard Rules (2011)
#40Earlier quoted context omitted.
> #1: Avoiding shifting versus doing division: Nope. Crappy compilers and bad architectures will yield bad results. Know what you're doing. Which compiler, among those still used in 2014, does not convert a division by 2 into a shift?
Memory escapes me...is a shift guaranteed by the standard (and if so, which) to sign-extend or not sign-extend?
A right shift of a signed, negative value is implementation-defined (which means, something in particular happens, and the compiler or its documentation is supposed to tell you what).
A left shift of a signed value is undefined (which means, the compiler is allowed to launch the missiles, or fail to launch the missiles, whichever is worse for you) if a nonzero bit could conceivable get into the sign.