Error about segmentation fault (core dumped)

Source: Internet
Author: User

whining

Say this error is very hateful Ah, yesterday afternoon has been, eat dinner back again, always get back to the dormitory when, think of try again, unexpectedly found the wrong. My program is a book of routines, not full letter ah. Just a quick note.

cause of the error

I was prompted by the Internet "nothing more than a few details of the reasons, such as: the declaration of the pointer but did not initialize, the result of the subsequent indirect reference", for example,

Char *p;

Char test[]= "Hello";

strcpy (p,test);

The passage of words from; http://www.cnblogs.com/gudai/archive/2006/06/27/436601.html

And my mistake is this: when I do a shared memory experiment, there's a paragraph

Char *buff;

Char *shm_addr;

Fgets (Buff,2048,stdin); there's no mistake in here.

strncpy (Shm_addr,buff,strlen (buff)); There's been a mistake.

I compile without errors, runtime, when typing Hello, prompts for segmentation fault (core dumped), as shown below


Inspired by the above, I will char *buff; One sentence modified into Char buff[20]; No errors, and the program is running normally.

Today, a careful analysis of the reason, just understand that I have defined the pointer buff, but in the 4th sentence using strlen (buff), to find the length of the type of pointer, this guy's length I did not give it definition, not overflow is strange. Modified to char buff[20], Buff is the first address of the array, with a length of 20. So there won't be any mistakes.

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.