C++ 将字符串存储为char[]并使用新位置将其取回

C++ 将字符串存储为char[]并使用新位置将其取回,c++,string,pointer-arithmetic,placement-new,C++,String,Pointer Arithmetic,Placement New,我想写一个类,它在内存中保存关于字符串的信息,并将其返回给我。所以我从一个拥有字符串大小的并集开始。(为什么union在这里不重要,但以后对于其他类型它需要是union)构造函数获得一个传递的字符串,并且应该将字符串作为c_str放在我放置为new的对象的末尾。 该类如下所示: class PrimitivTyp { public: explicit PrimitivTyp(const std::string &s); std::shared_ptr<std::st

我想写一个类,它在内存中保存关于字符串的信息,并将其返回给我。所以我从一个拥有字符串大小的并集开始。(为什么union在这里不重要,但以后对于其他类型它需要是union)构造函数获得一个传递的字符串,并且应该将字符串作为c_str放在我放置为new的对象的末尾。 该类如下所示:

class PrimitivTyp
{
public:
    explicit PrimitivTyp(const std::string &s);
    std::shared_ptr<std::string> getString() const;
private:
    union
    {
        long long m_long; //use long long for string size
        double m_double;
    } m_data;
    ptrdiff_t m_next;
};
int main(int argc, char* argv[])
{
    //checking type
    char buffer[100];
    PrimitivTyp* typ = new(&buffer[0]) PrimitivTyp("Testing a Type");
    LOG_INFO << *typ->getString();
}
class PrimitivTyp
{
公众:
显式PrimitivTyp(const std::string&s);
std::shared_ptr getString()常量;
私人:
联盟
{
long long m_long;//使用long long表示字符串大小
双m_双;
}m_数据;
ptrdiff_t m_next;
};
Ctor和get函数的impl看起来是这样的,我想它不能正常工作

PrimitivTyp::PrimitivTyp(const std::string& s)
{
    m_data.m_long = s.size();
    m_next = reinterpret_cast<ptrdiff_t>(nullptr);
    //calc the start ptr
    auto start = reinterpret_cast<ptrdiff_t*>(this + sizeof(PrimitivTyp));
    memcpy(start, s.c_str(), s.size()); //cpy the string
}

std::shared_ptr<std::string> PrimitivTyp::getString() const
{
    auto string = std::make_shared<std::string>();
    //get the char array
    auto start = reinterpret_cast<ptrdiff_t>(this + sizeof(PrimitivTyp)); //get the start point
    auto size = m_data.m_long; //get the size
    string->append(start, size);//appand it
    return string;//return the shared_ptr as copy
}
PrimitivTyp::PrimitivTyp(const std::string&s)
{
m_data.m_long=s.尺寸();
m_next=重新解释强制转换(nullptr);
//计算启动ptr
自动启动=重新解释强制转换(此+sizeof(PrimitivTyp));
memcpy(start,s.c_str(),s.size());//cpy字符串
}
std::shared_ptr PrimitivTyp::getString()常量
{
自动字符串=std::make_shared();
//获取字符数组
auto start=reinterpret_cast(this+sizeof(PrimitivTyp));//获取起点
自动大小=m_data.m_long;//获取大小
字符串->追加(开始,大小);//追加
return string;//将共享的\u ptr作为副本返回
}
用法应该是这样的:

class PrimitivTyp
{
public:
    explicit PrimitivTyp(const std::string &s);
    std::shared_ptr<std::string> getString() const;
private:
    union
    {
        long long m_long; //use long long for string size
        double m_double;
    } m_data;
    ptrdiff_t m_next;
};
int main(int argc, char* argv[])
{
    //checking type
    char buffer[100];
    PrimitivTyp* typ = new(&buffer[0]) PrimitivTyp("Testing a Type");
    LOG_INFO << *typ->getString();
}
intmain(intargc,char*argv[])
{
//检查类型
字符缓冲区[100];
PrimitivTyp*typ=new(&buffer[0])PrimitivTyp(“测试类型”);
LOG_INFO getString();
}
这会崩溃,我找不到调试器的错误。我认为这是一个关于位置计算的问题,
this
this+sizeof(PrimitivTyp)
不是你所想的,你想要
this+1
或者
重新解释(this)+sizeof(PrimitivTyp)

< C和C++中的指针运算考虑了指针的类型。


因此,
T*T
(t+1)
&t[1]
(假设
运算符不重载&
)或
重新解释强制转换(重新解释强制转换(t)+sizeof(t))
,但这不指向内存中的第一个元素吗?(所以第一位成员)非常感谢。我想我需要把它改成更简单的东西,比如:`autostart=this+1;memcpy(start,s.c_str(),s.size())//cpy字符串`如果我检索到相同的字符串,只需将开头转换为(char*)。我不知道它会像你说的那样考虑类型。谢谢你的澄清