New to C, segmentation fault?

c, scanf

Solution

I note that none of the answers have actually addressed your question:

Could some one explain the error "Segmentation fault"?

In C it is extremely easy to write a program that has "undefined behaviour". You have done so. A program that has undefined behaviour can literally do anything. It can go into an infinite loop. It can give a segmentation fault. It can work normally. It can erase your hard disk after emailing your files to North Korea. It can do anything whatsoever.

An extremely common symptom of undefined behaviour is a segmentation fault. Basically what this means is that you have written a program with undefined behaviour, and you got lucky. Your program attempts to access memory that it has no right to access. And instead of deleting your hard disk, the operating system gives you a segmentation fault. You should be thankful every time you get a segmentation fault; it could have been much, much worse. A seg fault calls attention to the error so that you can fix it easily.

Specifically what is happening here is:

- `buffer` is not initialized to any value. Its value could be any legal integer.

- `fscanf` expects a pointer. Pointers have the property that when dereferenced they turn into a variable. Pointers are often implemented as integers that store an address to the memory location of the variable. (Note that pointers are not required to be implemented like this, but it is a common choice.)

- Instead of a pointer you are giving fscanf an integer, which it interprets as a pointer to a storage location. But the integer contains any possible integer value.

- The operating system maintains a list of memory pages that are known to be in use. If `buffer` just happens to have a value which, when interpreted as a pointer, happens to refer to a page that is not in use, then the operating system will produce a seg fault when `fscanf` attempts to turn the pointer into a storage location.

Now think of what could have happened in other circumstances. `buffer` could have happened to contain an integer which when interpreted as a pointer yields a valid memory address, and that valid memory address might have happened to contain the return address of the current method. And the value put into that location by `fscanf` might happen to be the address of the "format the hard disk" library routine. You would not get a segmentation fault in that case; instead, when the current method returned, it would format your hard disk instead of terminating the program. Again, make sure you understand this: undefined behaviour can literally do anything.

Most of the time you will get lucky and get a segmentation fault. Do not rely on this safety net! Do not write undefined behaviour in the first place.

As a historical note, the term "segmentation fault" comes from the common practice of the operating system "segmenting" memory into sections for code, for data, and so on. There is of course again no requirement that an operating system do this, but most modern operating systems use some form of segmentation to help catch these sorts of errors.

Problem

I see there error in this code. in fscanf, the address of buffer needs to be referenced (&buffer). Could some one explain the error "Segmentation fault"? I am new to compiling things with gcc, and I dont understand what it it trying to tell me. ``` int buffer; char junk; while(fscanf(fp,"%d%c",buffer, &junk) !=EOF) { printf("%d\n",buffer); } fclose(fp); return 0; } ```

Original source