您好,再次遇到另一个 vector 类问题。
这是我的.CPP文件
#include "myStringVector.hpp"
void myStringVector::reserve(int n){
if(!base){
base = allocate<myString>(n);
last = base;
limit = base+n;
}
else{
myString *p = allocate<myString>(n); //new base
myString *q = p; //new last
limit = p + n; //new limit
myString *i = uninitialized_copy(base, last, q);
destroy(i);
deallocate(base);
base = p; //update base
last = q; //update last
}
}
myStringVector::myStringVector(){
this->reserve(0);
}
myStringVector::~myStringVector(){
if(!base){
deallocate(base);
deallocate(last);
deallocate(limit);
}
else{
base = initialized_destroy(base, last);
deallocate(base);
deallocate(last);
deallocate(limit);
}
}
myStringVector::myStringVector(const std::initializer_list<myString>& list){
std::cout << list.size() << "\n";
this->reserve(list.size());
last = uninitialized_copy(list.begin(), list.end(), last);
}
int myStringVector::size() const{
return (last - base);
}
bool myStringVector::empty() const{
return (this->size() == 0);
}
myString myStringVector::operator[](int index){
return *(base+index);
}
这是我的.HPP文件#ifndef MYSTRINGVECTOR_HPP
#define MYSTRINGVECTOR_HPP
#include "myString.hpp"
class myStringVector{
private:
myString *base = nullptr;
myString *last = nullptr;
myString *limit = nullptr;
public:
void reserve(int);
myStringVector();
~myStringVector();
myStringVector(const std::initializer_list<myString>&);
int size() const;
bool empty() const;
myString operator[](int);
};
#endif //MYSTRINGVECTOR_HPP_INCLUDED
也使用这些功能...template<typename T>
inline T* uninitialized_copy(T const* first, T const* last, T* out)
{
while (first != last) {
construct(out, *first);
++out;
++first;
}
return out;
}
...template<typename T>
inline T* initialized_destroy(T* first, T* last)
{
while (first != last) {
destroy(first);
++first;
}
return first;
}
...template<typename T, typename... Args>
inline T* construct(T* p, Args&&... args)
{
return new (p) T(std::forward<Args>(args)...);
}
...template<typename T>
inline T* allocate(int n)
{
return reinterpret_cast<T*>(::operator new(n * sizeof(T)));
}
和...template<typename T>
inline void deallocate(T* p)
{
::operator delete(p);
}
我的主要是...{
myStringVector v;
assert(v.empty());
std::cout << "Passed default ctor" << std::endl;
myStringVector v1 {"a", "b", "c"};
myStringVector v2 {"d", "e", "f"};
std::cout << v1.size() << " " << v2.size() << "\n";
std::cout << v1[0] << v1[1] << v1[2];
}
myString只是一个自定义字符串类。它像普通的字符串类一样工作。当我运行main时,我期望v1包含a,b和c。 v2包含d,e和f。但这就是我得到的...
ap÷bp÷cp÷
所以看起来每个字符后面都有多余的垃圾初始化每个索引。我试过直接初始化而不使用reserve()和uninitialized_copy,它工作正常。但是,我的教授要求我使用这些令人困惑的内存管理功能。我以为我使用的函数不正确,但是当编译器没有给我任何错误时,很难知道要解决的问题。
如果有人可以帮助我看看我在做什么错以及为什么我看到垃圾值,我将非常感激。
谢谢!
最佳答案
你说
myString is just a custom string class
但是您是如何实现的呢?
我的直接印象是您的自定义字符串类正在复制字符,并且没有在它们的末尾添加一个终止null。
另一个关键点是您正在使用
cout
,因此您需要知道如何编写自定义字符串。例如:如果您有一个operator const char*
,那么它将被拾取,并且流输出将期望一个以终止为null的真实C字符串。看到oofString output
ostream&
operator<<(ostream& os, const oofString& str)
{
os << str.chars();
return os;
}
其中chars()定义为:inline const char*
oofString::chars() const
{
if (mBody)
return mBody;
else
return "";
}
我在此问题中看到的所有内容都没有指出myStringVector
是问题的根源。
关于c++ - 自定义 vector 类的构造方法,将额外的垃圾值分配给指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65084579/