Size: 2616
Comment: Should I initialize variables?
|
← Revision 5 as of 2017-12-30 01:30:09 ⇥
Size: 2583
Comment: converted to 1.6 markup
|
Deletions are marked like this. | Additions are marked like this. |
Line 9: | Line 9: |
(answer lifted from a [http://cm.bell-labs.com/wiki/plan9/How_to_contribute/index.html Plan9 wiki]) | (answer lifted from a [[http://cm.bell-labs.com/wiki/plan9/How_to_contribute/index.html|Plan9 wiki]]) |
Line 16: | Line 16: |
Performance is certainly one reason [for not immediately initializing a variable value]. An example is kmalloc() vs kzalloc() - kmalloc() doesn't initialize the allocated memory but kzalloc() does. If you are allocating memory for a buffer and you know |
Performance is certainly one reason [for not immediately initializing a variable value]. An example is kmalloc() vs kzalloc() - kmalloc() doesn't initialize the allocated memory but kzalloc() does. If you are allocating memory for a buffer and you know |
Line 25: | Line 20: |
Line 27: | Line 21: |
Line 29: | Line 22: |
Line 31: | Line 23: |
Line 33: | Line 24: |
Line 35: | Line 25: |
Line 37: | Line 26: |
Line 42: | Line 30: |
Line 46: | Line 33: |
Line 49: | Line 35: |
{{{ | |
Line 50: | Line 37: |
Line 52: | Line 38: |
Line 54: | Line 39: |
Line 56: | Line 40: |
Line 58: | Line 41: |
}}} | |
Line 60: | Line 44: |
Line 62: | Line 45: |
Line 64: | Line 46: |
Line 66: | Line 47: |
Line 70: | Line 50: |
Line 72: | Line 51: |
Line 76: | Line 54: |
["CategoryFAQ"] | [[CategoryFAQ]] |
What coding style should I use?
If you are working on a new piece of code, you should follow the coding style from the file Documentation/CodingStyle in your kernel source directory.
When working on an existing piece of code, follow the coding style from that code. After you edit a file, a reader should not be able to tell just from coding style which parts you worked on.
What do I do if my patch got rejected due to the coding style?
(answer lifted from a Plan9 wiki)
The patch may well come back in the "sorry" category rather than the "applied" category, with suggestions on what should be changed to make it better and a request to resubmit it. This doesn't mean you should give up. It means you did a good job and the maintainers think it's worth trying to get you to make the job even better. Remember: you can't polish a turd.
Should I initialize variables?
(anwer graciously provided by Jesper Juhl)
Performance is certainly one reason [for not immediately initializing a variable value]. An example is kmalloc() vs kzalloc() - kmalloc() doesn't initialize the allocated memory but kzalloc() does. If you are allocating memory for a buffer and you know that whatever you pass the buffer to will overwrite all of it anyway (or at least some of it and you'll know how much) then having kmalloc() initialize it all to zero first would just be a waste of time. On the other hand, when you are allocating some memory and you possibly only write to part of it and the rest absolutely need to be zeroed, then it's probably a good idea to just use kzalloc() instead so you don't have to wory about adding logic to zero the remaining part of the buffer.
So, you have different API's for different needs.
In the case of this patch, we are talking about a automatic variable.
In C automatic variables are not initialized unless you do it explicitly. And if you have code like
int a; if (some_condition) a = 1; else a = 2;
then it would be completely pointless to have the compiler initialize the variable at the declaration since it would always be overwritten. Variables with explicit initialization also take up a bit of space in the kernel image file and there's no reason to waste memory if it's not needed.
So, to save on .text size, to save on CPU cycles doing pointless initializations etc variables and other storage is only initialized when the following code won't always do it.