The C language copies the string malloc, And the C language string malloc.

Source: Internet
Author: User

The C language copies the string malloc, And the C language string malloc.
Today, I am reading the code of my predecessors. Sometimes, copying strings directly assigns pointers to another pointer, some malloc a memory, and then copy the entire string value, which is a bit confusing, after studying it, I found that there are no mysteries. It is actually very simple, but it is better to record it. Conclusion: if the memory of the source string to be copied is recycled, malloc must be used to copy the entire string. (sometimes malloc is used to prevent the source string from being modified, but do not consider this factor). If it is not recycled, it will not be used. The following is the test code.

# Include <stdio. h>
# Include <string. h>

Typedef void (* str_cpy_slk) (char * name );
Void test (str_cpy_slk cb );
Void call_back (char * name );

Char * test_name = NULL;
Int main (void)
{
Test (call_back );

Printf ("name: % s \ n", test_name );

Return 0;
}

Void test (str_cpy_slk cb)
{
Char myname [8] = {0 };
Snprintf (myname, sizeof (myname), "% s", "slk ");
Printf ("myname: % s \ n", myname );
Cb (myname );
}

Void call_back (char * name)
{
Test_name = name;
Printf ("test_name: % s \ n", test_name );
}

Cannot be displayed. Use malloc to open up memory. If you assign a value, you can

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

Typedef void (* str_cpy_slk) (char * name );
Void test (str_cpy_slk cb );
Void call_back (char * name );

Char * test_name = NULL;
Int main (void)
{
Test (call_back );

Printf ("name: % s \ n", test_name );

Free (test_name );

Return 0;
}

Void test (str_cpy_slk cb)
{
Char myname [8] = {0 };
Snprintf (myname, sizeof (myname), "% s", "slk ");
Printf ("myname: % s \ n", myname );
Cb (myname );
}

Void call_back (char * name)
{
Test_name = (char *) malloc (strlen (name) + 1 );
Snprintf (test_name, strlen (name) + 1, "% s", name );
Printf ("test_name: % s \ n", test_name );
}

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.