如何编辑Qvector中的Qvector的内容

时间:2013-05-08 20:57:32

标签: c++ qt vector

我正在编写一个程序来模拟缓存。我正在使用QVector作为结构。我有一个QVector的QVector的整数。但是,当我尝试编辑整数时,会导致运行时错误:

*** glibc detected *** ./CompOrgProject3: free(): invalid next size (fast): 0x00000000010d2550 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7fd31b469b96]
./CompOrgProject3[0x402558]
./CompOrgProject3[0x402bf3]
./CompOrgProject3[0x402005]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fd31b40c76d]
./CompOrgProject3[0x4023c1]
======= Memory map: ========
00400000-00405000 r-xp 00000000 08:01 404625                             /home/**/Documents/CompOrgProject3/CompOrgProject3
00604000-00605000 r--p 00004000 08:01 404625                             /home/**/Documents/CompOrgProject3/CompOrgProject3
00605000-00606000 rw-p 00005000 08:01 404625                             /home/**/Documents/CompOrgProject3/CompOrgProject3
010cb000-010ec000 rw-p 00000000 00:00 0     

这是正在运行的鳕鱼: 主:

#include <fstream>
#include <QStringList>
#include <iostream>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <cstdlib>
#include <string>
#include "cache.h"

using namespace std;

int main()
{

    QFile instFile;
    instFile.setFileName("test.trace");

    int cacheSizes[4] = {1024, 4096, 16384, 65536};
    int blockSizes[4] = {8, 16, 32, 64};
    QString types[4] = {"DM", "2W", "4W" , "FA"};

    if(!instFile.open(QIODevice::ReadOnly))
    {
        return -1;
    }

    QTextStream in(&instFile);

    QStringList instList;
    while(!in.atEnd())
    {
        QString temp;
        temp = in.readLine();
        instList.append(temp);
    }

    for(int i = 0; i < instList.size(); i++)
    {
        cout << instList.at(i).toStdString() << "\n";
    }

    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            for(int k = 0; k < 4; k++)
            {
                Cache c(cacheSizes[i], blockSizes[j], types[k], instList);
                c.simulateCache();
            }
        }
    }

    return 0;

}

这是我有QVectors的模拟函数:

void Cache::simulateCache()
{

    if(this->instList.size() < 1)
    {
        screenOut << "Cannot simulate cache: there are no instructions";
    }
    else
    {
        screenOut << "Starting simulation...\n";
        int entry_per_block = block_size / 4;
        block_count = cache_size / block_size;
        QVector<QVector<int> > ca(block_count);
        for(int i = 0; i < block_count; i++)
        {
            ca[i].resize(entry_per_block);
        }
        screenOut << "QVector created.\n";

        for(int i = 0; i < this->instList.size(); i++)
        {
            QStringList inst_parts = instList.at(i).split(QRegExp(" "));
            QString instType = inst_parts.at(0);
            int mem_addr = inst_parts.at(1).toInt(0, 16);

            if(this->type.compare("DM") == 0)
            {
                int block_index = (int) (mem_addr / block_size) % block_count;
                int block_offset = mem_addr % block_size;

                if(ca.at(block_index).at(block_offset) == mem_addr)
                {
                    this->hits++;
                    if(instType.compare("W") == 0)
                    {
                        this->cacheToMem+=block_size;
                    }
                }
                else
                {
                    this->misses++;
                    this->memToCache += block_size;
                    ca[block_index][block_offset] = mem_addr;

                    if(instType.compare("W") == 0)
                    {
                        this->cacheToMem+=block_size;
                    }
                    for(int i = block_offset - 1; i >= 0; i--)
                    {
                 //ca[block_index][i] = mem_addr - ((block_offset - i) * 4);
                        //ca[block_index].insert(i, mem_addr - ((block_offset - i) * 4) );
                    }
                    for(int i = block_offset + 1; i < entry_per_block; i++)
                    {
                 //ca[block_index][i] = mem_addr + ((i - block_offset) * 4));
                        //ca[block_index].insert(i, mem_addr + ((i - block_offset) * 4));
                    }
                }

            }

        }

        hit_rate = hits / instList.size();
        screenOut << hit_rate << "\n";
    }
}

这是导致错误的行,我相信:

ca[block_index][block_offset] = mem_addr;

1 个答案:

答案 0 :(得分:1)

以下是使用嵌套QVector的简单示例。您可能还想查看此链接:

http://www.qtcentre.org/threads/49026-2D-array-in-Qt-QVector-or-QList

此外,您可能希望在代码中添加一些qDebug语句,以查看您是否拥有您认为的元素数量。

QVector < QVector < int > > matrix;

matrix.resize(10);
for(int i = 0; i< matrix.size(); i++)
{
    // Note that you can't use foreach here!
    matrix[i].resize(10);
}

// Sanity check:
qDebug() << matrix.size() << matrix.at(0).size();

for(int r = 0; r < matrix.size(); r++)
{
    for(int c = 0; c < matrix.at(r).size(); c++)
    {
        matrix[r][c] = 10;
        // qDebug() << matrix[r][c];
    }
}

希望有所帮助。

编辑:这是另一个检查的例子:

Qt 3D-array with Qt-Objekts like QVector