Deep copy _ Shallow copy

Source: Internet
Author: User

Deep copy, copy the contents of the memory, the old structure changes, the new structure will also change.

Shallow copy, direct address replication, shared memory, new and old structure complementary effects.

1 #define_crt_secure_no_warnings2 3#include <stdio.h>4#include <stdlib.h>5#include <string.h>6 7 struct string8 {9     Char*p;Ten     intlength; One }; A  - main1 () - { the     struct stringstr1; -Str1.length =Ten; -STR1.P = (Char*)malloc(sizeof(Char) *Ten); -strcpy (STR1.P,"Hello"); +  -printf"str1.p=%s\n", STR1.P); +  A     struct stringstr2;//shallow copy, shared memory atStr2.length =str1.length; -STR2.P =STR1.P; -* (STR1.P) ='k'; -  -printf"str1.p=%s\n", STR1.P); -printf"str2.p=%s\n", STR2.P); in  -System"Pause"); to } +  - Main () the { *     struct stringstr1; $Str1.length =Ten;Panax NotoginsengSTR1.P = (Char*)malloc(sizeof(Char) *Ten); -strcpy (STR1.P,"Hello"); the  +printf"str1.p=%s\n", STR1.P); A  the     struct stringstr2;//Deep Copy +Str2.length =str1.length; -STR2.P = (Char*)malloc(sizeof(Char) *Ten); $ strcpy (STR2.P, STR1.P); $  -printf"str1.p=%s\n", STR1.P); -printf"str2.p=%s\n", STR2.P); the  -System"Pause");Wuyi}

Deep copy _ Shallow copy

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.