NOTE: C specific answer โ
The following are common causes of segmentation or segmentation dysfunction:
Invalid format control string in printf or scanf statements: Make sure that the format control string has the same number of conversion specifiers (% s), since printf or scanf have arguments that should be printed or read accordingly, and that the specifiers match the type of variable that need to print or read. This also applies to fprintf and fscanf.
Forgetting to use "&" on scanf arguments: The scanf function takes as arguments the format control string and the addresses of the variables in which it will put the data that it reads. "&" (address) is used to supply the address of a variable. Usually forget to use "&". with each variable in a scanf call. Omitting "&" can cause segmentation disruption.
Access outside the array: Make sure that you do not violate the boundaries of any array that you use; those. You did not index the array with a value less than the index of its lowest element, or greater than the index of its highest element.
Inability to initialize a pointer before accessing it: A pointer must be assigned a valid address (i.e., appears on the left side of the task) before it is available (i.e. appears on the right of the task). Make sure you point to all pointers to a valid memory location. Correct initialization of the pointer can be done in several ways. Examples are given below.
Misuse of the "&" (address) and "" (dereferencing) statements: Make sure you understand how these statements work. Know when they should be applied and when not to apply. As mentioned above, it is customary to forget to use "&". with each variable in a scanf call. Remember that scanf requires the address of the variables it reads. Especially, you know, when "&" and "" are absolutely necessary, and when it is better to avoid using them.
Troubleshooting:
Check EVERY place in your program that uses pointers, array indexes, or uses the address operator (&) and the dereference operator (*). Each of them is a candidate for causing a violation of segmentation. Make sure you understand the use of pointers and related operators. If the program uses a lot of pointers and has many occurrences of and and *, add some printf statements to determine where the program causes the error and examine the pointers and variables involved in this statement.
Remember that printf statements must have a newline character (\ n) at the end of formatting control lines for debugging purposes to force the print buffer to be flushed.
wrapperm
source share