Introduction

 In 2007 OS project2, many students encountered segmentation fault during doing this homework. Most students did not know how it occur and how to solve it.

Discussion

 Segmentation is one approach to memory management and protection in the operating system. In fact, segmentation fault is the mistake that your problem try to do illegal operation on memory. The most reasons why it occur are as follows :

  • try to write a NULL pointer
  char *foo = NULL;
  strcpy(foo, "bang!");
  • using a uninitialized pointer
  char *foo;
  strcpy(foo, "bang!");
  • try to write elements more than array size
  int bar[20];
  bar[27] = 6;
  • try to write the data on read-only memory
  char *foo = "My string";
  strcpy(foo, "bang!");

UNIX compilers often put string literals like “My string” into read-only areas of memory.

  • Doing naughty things with malloc() and free()
  char bar[80];
  free(bar);

or

  char *foo = malloc(27);
  free(foo);
  free(foo);

Conclusion

 Making one of these mistakes will not always lead to an error, but they are always bad practice. Some systems and compilers are more tolerant than others, which is why programs that ran well on one system can crash when you try them on an another.

documents/segmentation_fault.txt · 上一次變更: 2008/09/24 17:42 由 crilit
CC Attribution-Noncommercial-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0