首页 > 其他分享 >char* 和std::string的生命周期

char* 和std::string的生命周期

时间:2023-02-16 10:36:12浏览次数:40  
标签:std abc string char lifetime local

std::string跟普通的c++对象一样,在对应的local/global的域内自动释放(包括string指向的字符串)。

因此

{

std::string abc = "abc";

}

"abc"在花括号外就被自动释放了。

而char* 不一样,如果不用New,而是用char* abc = "abc";

abc将成为string literal,是一个static object,将在进程退出后释放。

 


Yes, the lifetime of a local variable is within the scope({,}) in which it is created.

Local variables have automatic or local storage. Automatic because they are automatically destroyed once the scope within which they are created ends.

However, What you have here is a string literal, which is allocated in an implementation-defined read-only memory. String literals are different from local variables and they remain alive throughout the program lifetime. They have static duration  lifetime.

 

 

 

标签:std,abc,string,char,lifetime,local
From: https://www.cnblogs.com/simpleminds/p/17125830.html

相关文章

  • pycharm
    header设置File->Settings->Editor->FileandCodeTemplates->PythonScript#!/usr/bin/envpython#-*-coding:utf-8-*-#@Date:${DATE}${TIME}#@Author......
  • [ABC272F] Two Strings 题解
    [ABC272F]TwoStringsSolution目录[ABC272F]TwoStringsSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面给定两字符串$S,T$,求......
  • APS.NET Core 6.0Json任何类型读取到字符串属性The JSON value could not be converte
    在升级.netsdk到6.0版本后出现TheJSONvaluecouldnotbeconvertedtoSystem.String.原因是我代码定义的类型是string,但是传参的时候写了int,publicoverridevoidC......
  • Unreal Engine C++ :Character
    1.新建一个CharacterC++类默认包含:1//构造函数2ATheCharacter();34//开始5virtualvoidBeginPlay()override;67//更新8virtualvoidTick(f......
  • hex转string,hex转字符串,hex16进制转字符串,hex转中文
    hex转字符串,hex16进制转字符串,在线工具 https://www.toolscat.com/decode/hexhex转字符串,hex转string,string转hex,16进制转字符串,hex转字符串在线工具,hex转str在线......
  • String概述以及常用方法
    publicclassDemo01{publicstaticvoidmain(String[]args){Stringname="hello";//“hello”常量存储在字符串池中,字符串池在方法区中,字符串字面值可......
  • String案例演示
    publicclassApplication{publicstaticvoidmain(String[]args){Stringstr="thisisatext";//1将str中的单词单独获取出来Strin......
  • String.format()在android中的应用
    android中string.xml中%1$s、%1$d等的用法一,不使用xliff的用法:1、整型,比如“小姐今年23岁了”,这个23是整型的。在string.xml中可以这样写,Java代......
  • pycharm配置opencv库
    我走的弯路:对于刚刚入手OpenCV的小白,又第一次接触pycharm编译器。所以在配置opencv库的时候遇到很多问题。如下:1、如果直接在Terminal运行界面输入【pipinstallopencv-p......
  • 正确解决包含汉字的QString串转cv::String并format格式化
    QStringnow=QDateTime::currentDateTime().toString("yyyy-MM-dd_HH-mm-ss-zzz");QStringfileName1=QString::fromUtf8("D:/frame/小区监控图像_");......