zoukankan      html  css  js  c++  java
  • c++ string memcpy

    //----------------------------------------------------

    //AUTHOR: lanyang123456

    //DATE: 2014-10-30

    //---------------------------------------------------


    /*
    str5.cpp
    $ g++ -o test str5.cpp
    OS Ubuntu
    */

    #include <string>
    #include <iostream>

    #include <string.h>
    #include <stdio.h>

    using namespace std;

    int main()
    {
    string t1("123456789");
    string t2;

    cout<<"t1 string:"<<t1<<endl;
    printf("t1 string:%s ",t1.c_str());//返回一个以null终止的c字符串
    printf("sizeof t1 = %lu ", sizeof(t1));
    printf("t1.capacity = %lu ", t1.capacity());//返回当前容量(即string中不必增加内存即可存放的元素个数)
    printf("t1.max_size = %lu ", t1.max_size());//返回string对象中可存放的最大字符串的长度
    printf("t1.size = %lu ", t1.size());//返回当前字符串的大小
    printf("t1.length = %lu ", t1.length());//返回当前字符串的长度

    memcpy(&t2, &t1, sizeof(t1));//problem

    cout<<"t2 string:"<<t2<<endl;
    printf("t2 string:%s ",t2.c_str());//返回一个以null终止的c字符串
    printf("sizeof t2 = %lu ", sizeof(t2));
    printf("t2.capacity = %lu ", t2.capacity());//返回当前容量(即string中不必增加内存即可存放的元素个数)
    printf("t2.max_size = %lu ", t2.max_size());//返回string对象中可存放的最大字符串的长度
    printf("t2.size = %lu ", t2.size());//返回当前字符串的大小
    printf("t2.length = %lu ", t2.length());//返回当前字符串的长度

    return 0;
    }

    $ ./test
    t1 string:123456789
    t1 string:123456789
    sizeof t1 = 4
    t1.capacity = 9
    t1.max_size = 1073741820
    t1.size = 9
    t1.length = 9
    t2 string:123456789
    t2 string:123456789
    sizeof t2 = 4
    t2.capacity = 9
    t2.max_size = 1073741820
    t2.size = 9
    t2.length = 9
    *** Error in `./test': double free or corruption (fasttop): 0x08120008 ***
    Aborted (core dumped)

    字符串真是存放地址如下:

  • 相关阅读:
    javascript核心基础(正则专题)
    JsonUtil(工具类)
    身份证验证要用到的代码
    来cnblog了
    强智科技教务处模拟登录
    Thinkphp学习日记:jQuery_ajax数据提交
    在win8中使用服务器管理器来管理windows server 2012
    在windows server 2012运行锐捷客户端
    CentOS 6.4 配置LAMP 环境 与安装 phpmyadmin
    Linux下的光盘挂载与卸载
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/15457721.html
Copyright © 2011-2022 走看看