为指针分配额外垃圾值的自定义向量类的构造函数



你好,我又来了一个向量类问题。

这是我的.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);
}

这是我的.PP文件

#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只是一个自定义字符串类

但是您是如何实现它的?

我的直接印象是,您的自定义字符串类正在复制字符,并且未能在字符末尾放置终止null。

另一个关键点是您使用的是cout,因此您需要了解自定义字符串是如何编写的。例如:如果您有一个operator const char*,那么它将被拾取,并且流输出将期望一个终止为null的trueC字符串

请参阅oofString输出

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是问题的根源。

最新更新