首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >检索元素时数组发生变化

检索元素时数组发生变化
EN

Stack Overflow用户
提问于 2018-12-17 14:21:31
回答 1查看 80关注 0票数 1

编辑:添加构造函数和添加函数

考虑以下三个块:

//ONE
template<typename T>
class List1 {
private:
    uint32_t capacity;
    uint32_t used;
    T* data;
    void checkGrow() {
            if (used < capacity)
                    return;
            T* old = data;
            capacity*=2;
            data = (T*)new char[sizeof(T) * capacity];
            memcpy(data, old, sizeof(T)*used);
            delete (void*)old;
    }
public:
    List1() : capacity(1), used(0), data((T*)new char [sizeof(T)]) {}
    List1(uint32_t initialSize) :
            capacity(initialSize), used(0), data((T*)new char[sizeof(T)]) {}

    List1(const List1& orig) :
            capacity(orig.capacity), used(orig.used), data((T*)new char[used * sizeof(T)]) {
            memcpy(data, orig.data, used * sizeof(T));
    }
    uint32_t serializeSize() const { return sizeof(used) + used*sizeof(T); }
    char* read(char* p) {
            used = *(uint32_t*)p;
            p += sizeof(uint32_t);
            data = (T*)new char[used*sizeof(T)];
            memcpy(p, data, used * sizeof(T));
            return p + used*sizeof(T);
    }
    char* write(char* p) {
            *(uint32_t*)p = used;
            p += sizeof(uint32_t);
            memcpy(p, data, used * sizeof(T));
            return p + used * sizeof(T);
    }

    ~List1() { delete [] data; }

    void add(const T& v) {
            checkGrow();
      data[used++] = v;        
    }
    uint32_t getUsed() const{
            return used;
    }
    uint32_t getCapacity() const{
            return capacity;
    }

    //const T& operator [](int i) const { return data[i]; }
    //T& operator [](int i) { return data[i]; }
    T getData (int i) const{
            return data[i];
    }
    uint32_t size() const { return used * sizeof(T); }
};

//TWO
List1<uint32_t> temp=in.readList<uint32_t>(); // <List1<uint32_t>>();
//BREAKPOINT HERE
for(uint i=0;i<15;i++){
    //cout<<temp[i]<<endl;
    cout<<temp.getData(i)<<endl;
}

//THREE
template<typename T>
T _read() {
    T temp = *(T*)p;
    p += sizeof(T);
    availRead -= sizeof(T);
    return temp;
}

template<typename T>
T read(){
    //cout << (uint64_t)p << endl;
    checkAvailableRead(sizeof(T));
    return _read<T>();
}
template<typename T>

List1<T> readList(){
    uint32_t len = read<uint32_t>();
    List1<T> temp(len);
    for (uint i = 0 ; i < len; i++){
        T val =read<T>();
        temp.add(val);
        //HERE: For some reason code does not work without this print statement
        //cout<<temp.getData(i)<<endl;
        }
    return temp;
}

基本上,问题是数据的值在从getData返回后发生了变化,如下所示。

gdb) p/u *temp.data@15  
$1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
(gdb) p/u *temp.data@15  
$2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
(gdb) p/u *data@15  
$3 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
[New Thread 8444.0xad8]
(gdb) p/u *data@15  
$4 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
[New Thread 8444.0x214c]
(gdb) p/u *temp.data@15  
$5 = {0, 1, 2, 3, 4, 5, 83, 0, 2150008464, 1, 3742232646, 0, 168272, 6, 0}

出于某种原因,向readList添加一条print语句可以解决这个问题,但这不是一个合理的解决方案。我在代码上尝试了几种不同的变体,但都不起作用。

我不确定问题是什么,甚至不知道如何开始调试,因为问题发生在return语句和循环的下一次迭代之间(这里没有什么可讨论的)。

任何建议都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2018-12-18 04:43:00

List1(const List1& orig) :
        capacity(orig.capacity), used(orig.used), data((T*)new char[used * sizeof(T)]) {
        memcpy(data, orig.data, used * sizeof(T));
}

为了使List1正常工作,决不能有List1capacity大于实际分配的大小。但是,如果origcapacity大于其used,则这会创建一个违反此不变量的新used

你可能指的是capacity(orig.used)

这里也有同样的问题:

List1(uint32_t initialSize) :
        capacity(initialSize), used(0), data((T*)new char[sizeof(T)]) {}

如果将capacity设置为initialSize,则不能仅为1个T分配空间。

这也是坏掉的delete (void*)old;。你用new[]分配的东西,你必须用delete[]释放。

请注意,List1只能用于保存没有构造函数或析构函数的POD类型(普通旧数据)。如果您试图使用List1来容纳更复杂的内容,那么您的设计就大错特错了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53809867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档