std :: string vs. char * [英] std::string vs. char*

查看:62
本文介绍了std :: string vs. char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: string在堆栈或堆上存储的数据与char *不同吗?还是只是从char *派生到一个类中?

does std::string store data differently than a char* on either stack or heap or is it just derived from char* into a class?

推荐答案

char *

  • 是您的CPU体系结构的一个指针的大小.
  • 可能是从 malloc calloc new new [] 返回的值.
    • 如果是这样,完成后必须传递给 free delete delete [] .
    • 如果是这样,字符将存储在堆中.
    • char*

      • Is the size of one pointer for your CPU architecture.
      • May be a value returned from malloc or calloc or new or new[].
        • If so, must be passed to free or delete or delete[] when you're done.
        • If so, the characters are stored on the heap.
          • 通常,无法判断 char * 参数是否指向堆栈,堆或全局空间.
          • Generically, no way to tell if a char* argument points to stack, heap, or global space.
          • 是几个指针的大小,通常是三个.
          • 在创建时自行构造:不需要 new delete .
            • 拥有字符串的副本(如果字符串可能会更改).
            • 可以从 char * 复制此字符串.
            • 默认情况下,内部使用 new [] 的方式与获取 char * 的方式相同.
            • Is the size of several pointers, often three.
            • Constructs itself when created: no need for new or delete.
              • Owns a copy of the string, if the string may be altered.
              • Can copy this string from a char*.
              • By default, internally uses new[] much as you would to obtain a char*.
              • 定义 c_str(),该代码将返回 char * 以供临时使用.
              • Defines c_str() which returns a char* for temporary use.
              • string :: iterator 是灵活的:一种实现可能使它成为范围检查的超级安全调试助手,或者只是使之成为超高效的 char * 一个开关.
              • string::iterator is flexible: an implementation may make it a range-checked super-safe debugging helper or simply a super-efficient char* at the flip of a switch.

              这篇关于std :: string vs. char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆