zoukankan      html  css  js  c++  java
  • C语言中的深拷贝和浅拷贝

    //C语言中的深拷贝和浅拷贝
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    typedef struct _student{
        char name[30];
        char *title;
        int age;
    }Student;
    
    
    void main(){
        Student s1;
        Student s2;
        s1.age = 12;
        strcpy(s1.name, "小明");
        s1.title = (char *)malloc(sizeof(char)* 30);
        strcpy(s1.title, "总经理");
        s2 = s1;
        printf("s1的age=%d,s1的name=%s,s1的title=%s
    ", s1.age, s1.name, s1.title);
        printf("s2的age=%d,s2的name=%s,s2的title=%s
    ", s2.age, s2.name, s2.title);
        printf("s1的地址%x
    ", s1.title);   //打印b71408
        printf("s2的地址%x
    ", s2.title);   //打印b71408
        printf("s1的地址%x
    ", s1.name);   //打印d5fd18
        printf("s2的地址%x
    ", s2.name);   //打印d5fce8
        //这说明s1和s2中成员char *title;只是浅拷贝,两个指针指向同一块堆内存,
        //当释放free(s1.title);时,s2.title指向的内存空间也没释放了,所以再次释放会报错
        if (s1.title != NULL)
        {
            free(s1.title);
        }
        //错误代码
        /*if (s2.title != NULL)
        {
            free(s2.title);
        }*/
    
    
        //要想实现深拷贝,那么必须给s2.title也分配一段内存空间,
        //然后通过strcpy()将s2.title指向的字符串复制到s2.title指向的内存空间内
    
        //由此证明,结构体之间的赋值(s2 = s1;),是进行了结构体内部所有数据的拷贝,
        //如上s1.name的地址s2.name的地址不同,说明是把s1.name中的数据复制到了s2.name中
        system("pause");
    }
  • 相关阅读:
    OCR中表格识别及WORD生成
    太阳能跟踪系统
    QA300太阳能移动电源
    OCR之表格识别
    2012112
    2012123
    2012121
    2011101
    201191
    2011828
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5421804.html
Copyright © 2011-2022 走看看