Segmentation faults: what they are and general tips for how to avoid them?
-
Most sources I come across define segmentation faults as something along the lines of this: that they occur when a program references an undefined area of virtual memory But since I haven't yet taken a class on Operating Systems, I usually have no idea what this means that it's an "undefined area" of virtual memory... Also, a lot of my C programs tend to run into the segmentation fault, and unfortunately I have no idea how to fix them. Are there any good tips for how to avoid these faults, and what to do when one has been encountered when all the logic of the program seems alright?
-
Answer:
To put this more simply a segmentation fault is that you are reading or writing memory that you are not allowed access to because you do not own it. A simple example is using a variable on the stack followed by some code. Say we have char a[4]; int i=0; for(i=0; i<1000; ++0) a[i]='a'; printf("Hi this line will never get printed because last line killed it\n"); So on the stack we have 4 bytes allocated for 'a', another 4 for int 'i' and now we have the code that is the function. Thing is the for statement writes a 1000 bytes into 'a' which overwrites 'i', and also kill the code following it.
Dark Templar at Stack Overflow Visit the source
Related Q & A:
- How To Avoid Recaptcha?Best solution by Stack Overflow
- How To Avoid Power Cut?Best solution by Yahoo! Answers
- How To Avoid On Omegle?Best solution by wikihow.com
- What things are the best thing to eat (and avoid) before an exam?Best solution by Yahoo! Answers
- What are some tips on how to become famous on youtube?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.