About strcpy Segment Errors

Source: Internet
Author: User

int main () {

char * s = "Hello,world";

Char *t = "Happy";

strcpy (s,t); Error

return 0;

}

I thought there was nothing wrong, but actually running, there was a segment error,

Later modified char *s = (Cahr *) malloc (12); Ok

Because "Hello,world" is stored in the character constant area, s points to this area, and this area is a const attribute of non-modifiable;

Therefore, there will be a paragraph error when copying again;

A program is divided into:

1. Stack (stack)--the compiler automatically assigns the release, the main storage function parameter value, local variable value, etc. 2. Heap Area (heap)--distributed by programmer, 3. Global or static zone----holds global variables and static variables, which is released by the system at the end of the program.        It is divided into the global initialization zone and the global uninitialized zone; 4. Character constant area--the constant string is placed with this, the program is released at the end of the system; 5. program code area int a=0;       Global initialization zone char *P1;          Global uninitialized zone void Main () {int B;  Stack char s[]= "BB";       Stack char *p2; Stack char *p3= "123"; Where, "123\0" constant area, p3 in the stack area * * * point to non-modifiable, with a const attribute; static int c=0;   Global Zone p1= (char*) malloc (10);    10 byte area in the heap area strcpy (P1, "123"); "123\0" in the constant area, the compiler may be optimized to point to the same area as the P3} specific reference: http://blog.csdn.net/yujiaming493323/article/details/8765345

About strcpy Segment Errors

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.