简体   繁体   English

为什么在对同一个字符数组使用 sizeof 时会得到不同的结果?

[英]Why I am getting varied results when using sizeof for same char array?

In main class I print sizeof(person->name) and then I do sizeof(name) which are same as I pass same char array to Person constructor.在主类中,我打印 sizeof(person->name) 然后我做 sizeof(name) 这与我将相同的 char 数组传递给 Person 构造函数相同。 But why I get varied results in both cases但是为什么我在这两种情况下都得到了不同的结果
in first case sizeof returns 32 whereas in second case sizeof return 6 This is output在第一种情况下 sizeof 返回 32 而在第二种情况下 sizeof 返回 6这是输出

This is the code :-这是代码:-

#include <iostream>
#include "Person.h"

int main()
{

    char name[] = {'H','o','b','b','i','t'};
    Person *person = new Person(name , 203);

    std::cout << "p->Name size - " << sizeof(person->name) << " char array size   " << sizeof(name) << std::endl;

    delete person;

    return 0;

}
#include "Person.h"
#include <iostream>

Person::Person(){};

Person::Person(char name[],int age)
{
    this->name = name;
    this->age = age;
}

Person::~Person()
{
    std::cout << "\n Destructor called" << std::endl;
}

void Person::sayHello()
{
    std::cout << "\n Hello " << this->name << " " << this->age << std::endl ;   
}

sizeof in a std::string does not measure the complete amount of memory the string occupies. std::string中的sizeof不测量字符串占用的完整内存量。 It measures how large the std::string object is.它测量std::string对象的大小。

The sizeof on a std::string is a constant value that is independent on the length of the string that is stored in it. std::string上的sizeof是一个常量值,与存储在其中的字符串的长度无关。

A std::string does have an overhead compared to a char name[] due to meta information and small string optimizations, and that overhead is (depending on the implementation) at a maximum of around 20 bytes.由于元信息和小字符串优化, std::stringchar name[]相比确实有开销,并且该开销(取决于实现)最多约为 20 个字节。

For a range of 0 - ~20 chars a std::string will always occupy ~20 bytes memory.对于 0 - ~20 个字符的范围, std::string将始终占用 ~20 字节内存。 For more than ~20 chars the std::string will occupy ~20 + number of char bytes.对于超过约 20 个字符, std::string将占用约 20 + 字符字节数。

So yes for a really small number of chars it can be 5 times more bytes.所以是的,对于非常少的字符,它可以是 5 倍多的字节。 But for a normal use case, that overhead can be ignored.但是对于正常用例,可以忽略该开销。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么使用sizeof()char数组的大小相同 - why char array's size is same using sizeof() 为什么我在 char 数组的末尾得到 i? - why I am getting i at the end of char array? 字符串 vs 字符数组; 为什么我得到不同的结果? - String vs Char array; Why I'm getting diffrent results? 不知道为什么使用字符串或char时会得到不同的长度 - Not sure why I am getting different lengths when using a string or a char 当我使用 sizeof(array)/sizeof(array[0]) 作为 for 循环的条件时,为什么我不能在第二个循环中计算出数组? - why i cant cout the array in the second loop when i use sizeof(array)/sizeof(array[0]) as a condition for the for loop? 为什么我在使用&这时会出错? - Why am I getting an error when using &this? C ++:使用sizeof的char数组的大小 - C++: size of a char array using sizeof C++:为什么我在实际使用字符串时收到错误消息:“数组初始值设定项必须是初始值设定项列表或字符串文字”? - C++: Why am I getting an error: "array initializer must be an initializer list or string literal" when I am in fact using a string? 为什么在尝试将opencv图像数据指针转换为char *时出现错误? - Why I am getting error when I try to cast an opencv image data pointer to a char* 为什么sizeof(std :: variant &lt;char&gt;)== 8使用libc ++而不是2(比如MSVC的STL和libstdc ++)? - Why is sizeof( std::variant< char > ) == 8 when using libc++ and not 2 (like with MSVC's STL and libstdc++)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM