Segmentation fault errors appear in Linux __linux

Source: Internet
Author: User

Write your own program when encountered this problem many times, most are pointers do not dynamically allocate space directly using subscript caused.

Like what:

Char *s;

cin>>s;

int Len=strlen (s);

for (int i=0;i<len;i++) s[i] ....

I've had this mistake a few times, and I'm going to be impressed. If it is a pointer and does not point to an array, use functions such as malloc to allocate space.

Char *s;

s= (char *) malloc (sizeof (char) *100);

cin>>s;

int Len=strlen (s);

for (int i=0;i<len;i++) s[i] ....

Or point the pointer directly at the array, but it doesn't make sense.

Char s[100];

Char *p;

cin>>s;

P=s;

int Len=strlen (s);

for (int i=0;i<len;i++) p[i] ....

Found an article about the cause of the error under Linux:

Segmentation Fault in Linux
1
Segmentation Fault in Linux
--Reasons and avoidance
Author:zx_wing (xing5820@163.com)
Segmentation Fault in Linux
2
It's written in front of you.
Recently the Cu (Chinaunix) appeared a lot to ask segmentation fault
The post, in fact this is also a "menstrual paste", Bubble cu years, every month
People are asking. In order to reduce repetitive replies, the author combined their own experience, summed up
The mechanism of SIGSEGV in Linux and the actual examples to generalize which
Some programming errors can easily cause SIGSEGV. Because of my limited experience, the paper
Inevitably there are omissions and errors, please find a friend sent letters to Xing5820@163.com
Correct, the author is good even if modified.
(Copyright notice: This article is welcome to reprint, but not allowed for commercial purposes without permission)
Segmentation Fault in Linux
3
Content Summary
This paper briefly introduces the causes of the occurrence of segmentation fault, and combines real
An example describes a stream in which the kernel sends a SIGSEGV signal to a user-state program
Ride. In this paper, some common SIGSEGV problems are answered with examples, such as "for
what function returns the stack can also be accessed. "," why Free () after the memory
You can still use the "," Why I met SIGSEGV instead of
Sigill signal "and so on. Finally, the author combined with his own experience, enumerated some pre
Anti-SIGSEGV programming habits, for your reference. SIGSEGV strictly depends on
Operating system, compiler, hardware platform, this paper is based on Linux, GCC, 32bit
IA32 architecture, but also useful for other platform operating systems.
Segmentation Fault in Linux
4
Revision History
Date Version Description
2009.12.21 1.0 Initial Release
Segmentation Fault in Linux
5
1. What is "segmentation fault in Linux".
We'll take a quote from the wiki to answer that question.
A segmentation fault (often shortened to SIGSEGV) is a particular
Error condition that can occur during the operation of computer
Software. A segmentation fault occurs when a program attempts to
Access a memory location that it isn't allowed to access, or
Attempts to access a memory location in a way this is not allowed
(for example, attempting to write to a read-only location, or to
Overwrite part of the operating system).
Segmentation is one approach to memory management and protection in
The operating system. It has been superseded by paging for most
purposes, but much of the terminology of segmentation is still used,
"Segmentation fault" being an example. Some Operating Systems still
Have segmentation at some logical level although paging is used as
The main memory management policy.
On Unix-like operating systems, a process that accesses a invalid
Memory address receives the SIGSEGV signal. On Microsoft Windows, a
Process that accesses invalid memory receives the
Status_access_violation exception.
The above text does not give a definition of SIGSEGV, just say it is "a computer software operation in the process of a mistake
Situation ". The text describes when SIGSEGV occurs, which is "when a program attempts to access an area of memory that is not allowed to be accessed
(for example, try to write a piece of memory that belongs to the operating system), or access the memory area with the wrong type (for example, try Writing
A read-only memory). This description is accurate. In order to deepen our understanding, we will give a more detailed summary of SIGSEGV.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.