Click here to Skip to main content
15,908,675 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: help in erasing the part of string Pin
tasumisra14-Dec-08 22:15
tasumisra14-Dec-08 22:15 
QuestionDangling pointers and incorrect data being written Pin
Mustafa Ismail Mustafa14-Dec-08 20:02
Mustafa Ismail Mustafa14-Dec-08 20:02 
AnswerRe: Dangling pointers and incorrect data being written Pin
Cedric Moonen14-Dec-08 20:48
Cedric Moonen14-Dec-08 20:48 
GeneralRe: Dangling pointers and incorrect data being written Pin
Mustafa Ismail Mustafa14-Dec-08 21:11
Mustafa Ismail Mustafa14-Dec-08 21:11 
GeneralRe: Dangling pointers and incorrect data being written Pin
Stuart Dootson14-Dec-08 21:31
professionalStuart Dootson14-Dec-08 21:31 
GeneralRe: Dangling pointers and incorrect data being written Pin
Mustafa Ismail Mustafa14-Dec-08 21:42
Mustafa Ismail Mustafa14-Dec-08 21:42 
GeneralRe: Dangling pointers and incorrect data being written Pin
Stuart Dootson14-Dec-08 22:07
professionalStuart Dootson14-Dec-08 22:07 
GeneralRe: Dangling pointers and incorrect data being written Pin
Mustafa Ismail Mustafa15-Dec-08 1:43
Mustafa Ismail Mustafa15-Dec-08 1:43 
Sorry about the lateness, I had to run out for a few errands.

Sadly Stuart, those are the two I'm not having problems with. Its the rest of the class.

If you'll look below, that's where all the fuss is happening.

I'd like to direct your kind attention to the methods CreateCacheBlocks() and CreateCacheSets() in particular the latter. It is then that the contents of the CacheBlock object retrieved by getCacheBlocks()[i] is incorrect and that is where I'm losing my mind because it was in the preceding function CreateCacheBlocks() that I set the values (correctly I might add).

If you're wondering what this is, its the framework for a cache simulator for my Computer Arch II class at university where after I'm supposed to be simulating different algorithms and trying to make them better through changing one thing or another.

<br />
#ifndef _SIMULATOR_H<br />
#define	_SIMULATOR_H<br />
<br />
/******************************************************************************<br />
*           REMOVE ASAP<br />
******************************************************************************/<br />
#define DEBUG<br />
<br />
#include <iostream><br />
#include <vector><br />
#include "definitions.h"<br />
#include "settings.h"<br />
#include "filereader.h"<br />
#include "cacheset.h"<br />
#include "cacheblock.h"<br />
<br />
<br />
using namespace std;<br />
<br />
class Simulator<br />
{<br />
<br />
public:<br />
    Simulator();<br />
    Simulator(const Simulator& orig);<br />
    virtual ~Simulator();<br />
<br />
    void getUserSettings();<br />
    void GetData();<br />
    void Simulate();<br />
    void Splash();<br />
<br />
    void CreateCacheBlocks();<br />
    void CreateCacheSets();<br />
<br />
    /*settings*/<br />
    Settings getSettings() const;<br />
    void setSettings(Settings const &value);<br />
    <br />
<br />
    /*CacheSet*/<br />
    vector<CacheSet> getCacheSet() const;<br />
    void setCacheSet(vector<CacheSet> const &value);<br />
    <br />
    /*CacheBlocks*/<br />
    vector<CacheBlock> getCacheBlocks() const;<br />
    void setCacheBlocks(vector<CacheBlock> const &value);<br />
<br />
    /*Data Buffer*/<br />
    vector<uint> getDataBuffer() const;<br />
    void setDataBuffer(vector<uint> const &value);<br />
<br />
private:<br />
    Settings m_Settings;;<br />
    //CacheBlock* m_CacheBlock;<br />
    vector<CacheBlock> m_CacheBlock;<br />
    //CacheSet* m_CacheSet;<br />
    vector<CacheSet> m_CacheSet;<br />
    vector<uint> m_DataBuffer;<br />
<br />
};<br />
<br />
#endif	/* _SIMULATOR_H */<br />
</vector></iostream>


***********************************************
<br />
#include "simulator.h"<br />
#include <vector><br />
#include "cacheset.h"<br />
<br />
<br />
Simulator::Simulator()<br />
{<br />
    m_Settings.setAccessesToSimulate(0);<br />
    m_Settings.setBlockSize(0);<br />
    m_Settings.setCacheAssociativity(0);<br />
    m_Settings.setCachePolicy(Settings::LRU);<br />
    m_Settings.setCacheSize(0);<br />
}<br />
<br />
Simulator::Simulator(const Simulator& orig)<br />
{<br />
}<br />
<br />
Simulator::~Simulator()<br />
{<br />
}<br />
<br />
<br />
/*<br />
 ******************************************************************************<br />
 *      Public Methods<br />
 ******************************************************************************<br />
 */<br />
void Simulator::getUserSettings()<br />
{<br />
    #ifdef DEBUG<br />
    Settings s;<br />
    s.setDefault();<br />
    setSettings(s);<br />
<br />
    return;<br />
    #endif<br />
<br />
    //ask the user for the settings<br />
    //TODO: Ask the user for the settings<br />
    <br />
    //TODO: An Associativity of 0 means a Fully Associative cache<br />
}<br />
<br />
void Simulator::GetData()<br />
{<br />
    FileReader fr;<br />
    Settings s;<br />
    s = getSettings();<br />
    setDataBuffer(fr.ReadFile(s));<br />
<br />
}<br />
<br />
<br />
void Simulator::Simulate()<br />
{<br />
    Splash();<br />
<br />
    getUserSettings();<br />
<br />
    cout.clear();<br />
    cout << "Reading trace file..." << endl;<br />
   <br />
    GetData();<br />
    sleep(2);<br />
<br />
    cout << "\nData read.\nStarting Simulation..." << endl;<br />
    sleep(1);<br />
<br />
    // simulation start<br />
<br />
    //create the data blocks and the cache sets<br />
<br />
    //create the Cache Blocks<br />
    CreateCacheBlocks();<br />
<br />
    //create the Cache Sets<br />
    CreateCacheSets();<br />
<br />
}<br />
<br />
void Simulator::CreateCacheBlocks()<br />
{<br />
    //*************************************************************************<br />
    //              DO NOT FORGET, WE'RE USING 32 BIT addressing!             *<br />
    //*************************************************************************<br />
    //              This is because each access is 4 * 8 bits = 32 bits       *<br />
    //But because I'm a good student, I'll still calculate the shifts instead *<br />
    //of simply hardwiring the values.                                        *<br />
    //Steve                                                                   *<br />
    //*************************************************************************<br />
<br />
    uint numBlocks = getSettings().getCacheSize()/getSettings().getBlocksSize();<br />
    <br />
    /*<br />
    uint maxOffset = CalculateMaxOffset();<br />
<br />
    //tag is easily defined as "which data|instruction in the block?"<br />
    uint bTag = CalculateTag(0,maxOffset);<br />
<br />
    //index is easily defined as "which set?"<br />
    uint bIndex = CalculateTag(0, bTag, maxOffset);<br />
    */<br />
<br />
    uint maxOffset = getSettings().getOffsetParity();<br />
    uint bTag = getSettings().getTagParity();<br />
    uint bIndex = getSettings().getIndexParity();<br />
    bool isValid = false;<br />
    char* bData = new char[maxOffset - 1]; //just to make it 0 based<br />
    for(int i = 0; i < maxOffset; i++)<br />
    {<br />
        bData[i] = NULL;<br />
    }<br />
<br />
    <br />
    vector<CacheBlock> v;<br />
    <br />
    CacheBlock cb;<br />
    cb.Initialize(maxOffset,bTag,bIndex,isValid,bData);<br />
    <br />
    cb.setSettings(m_Settings);<br />
    //initialize each cache block to an initial state<br />
    for(uint i = 0; i < numBlocks; i++)<br />
    {<br />
        cb.setBlockNumber(i);<br />
        cb.setBlockAddress(cb.CalculateBlockAddress(i));<br />
        v.push_back(cb);<br />
    }<br />
    //TODO: fix this ASAP<br />
    setCacheBlocks(v);<br />
    //m_CacheBlock = v;<br />
<br />
    #ifdef DEBUG<br />
    cout << "\n\n" << endl;<br />
    cout << "Vector Length: \t" << getCacheBlocks().size() << endl;<br />
<br />
    cout << "\n\n" << endl;<br />
<br />
    for(int i = 0; i < getCacheBlocks().size(); i++)<br />
    {<br />
        cout << "v[" << i << "].BlockAddress = \t" << v[i].getBlockAddress() << endl;<br />
    }<br />
    #endif<br />
}<br />
<br />
void Simulator::CreateCacheSets()<br />
{<br />
    /*the number of cache sets is equal to the<br />
    *       associativity/num blocks<br />
    *and in the case of  a fully associative cache there is only one set<br />
    *and every block belongs to it<br />
     *<br />
     *Create the correct number of cache sets and associate the proper<br />
     *cacheblock to this set<br />
    */<br />
<br />
    uint numBlocks = getSettings().getCacheSize()/getSettings().getBlocksSize();<br />
    uint numSets = numBlocks/getSettings().getCacheAssociativity();<br />
    <br />
    uint numBlocksPerSet = numBlocks / numSets;<br />
<br />
<br />
    CacheSet cs;<br />
    cs.setSettings(getSettings());<br />
    vector<CacheSet> v;<br />
<br />
    //create the correct number of sets and assign their set indexes<br />
    for(uint i = 0; i < numSets; i++)<br />
    {<br />
        cs.setSetIndex(i);<br />
        v.push_back(cs);<br />
    }<br />
<br />
    int k = v.size();<br />
    CacheBlock cb;<br />
<br />
    //assign each cache block to its set<br />
    uint setIx = 0;<br />
    for(uint i = 0; i < numBlocks; i++)<br />
    {<br />
        <br />
        setIx = getCacheBlocks()[i].getBlockAddress() << getSettings().getTagParity();<br />
        setIx >>= getSettings().getTagParity();<br />
        //cb = getCacheBlocks()[i];<br />
        cb = m_CacheBlock[i];<br />
        v[setIx].getCacheBlocks().push_back(cb);<br />
    }<br />
<br />
    #ifdef DEBUG<br />
    int j = v.size();<br />
<br />
    for(int i = 0; i < v.size(); i++)<br />
    {<br />
        j = v[i].getCacheBlocks().size();<br />
        cout << "V[i][j].size() =" << j << endl;<br />
    }<br />
    #endif<br />
<br />
    setCacheSet(v);<br />
<br />
    #ifdef DEBUG<br />
    /*<br />
    cout << "\n" << endl;<br />
    cout << "Number of Sets:\t\t" << getCacheSet().size() << endl;<br />
    cout << "Number of blocks in each set:\t" <<<br />
        getCacheSet()[0].getCacheBlocks().size() << endl;<br />
    cout << "\n" << endl;<br />
    for(int i = 0; i < getCacheSet().size(); i++)<br />
    {<br />
        cout << "Cache set:\t" << i << endl;<br />
        cout << "\n" << endl;<br />
        for(int j = 0; j < getCacheSet()[i].getCacheBlocks().size(); j++)<br />
        {<br />
            cout << "Block number " << j << "\tBlock Address: " << getCacheSet()[i].getCacheBlocks()[i].getBlockAddress();<br />
        }<br />
        cout << "\n" << endl;<br />
    }<br />
    */<br />
    cout << "\n" << endl;<br />
    cout << "Number of Sets:\t\t" << v.size() << endl;<br />
    cout << "Number of blocks in each set:\t" <<<br />
        v[0].getCacheBlocks().size() << endl;<br />
    cout << "\n" << endl;<br />
    for(int i = 0; i < v.size(); i++)<br />
    {<br />
        cout << "Cache set:\t" << i << endl;<br />
        cout << "\n" << endl;<br />
        for(int j = 0; j < v[i].getCacheBlocks().size(); j++)<br />
        {<br />
            cout << "Block number " << j << "\tBlock Address: " << v[i].getCacheBlocks()[i].getBlockAddress();<br />
        }<br />
        cout << "\n" << endl;<br />
    }<br />
    #endif<br />
<br />
}<br />
<br />
<br />
<br />
void Simulator::Splash()<br />
{<br />
    cout.clear();<br />
<br />
    cout << "Starting Cache Simulation by Steve Mustafa\n\n" << endl;<br />
    cout << "Supervisor: Dr. Mazen Kharbutli\nComputer Architecture II\n" << endl;<br />
<br />
    //wait for 5 seconds<br />
    sleep(5);<br />
}<br />
<br />
/*Methods for Properties*/<br />
<br />
/*Settings*/<br />
Settings Simulator::getSettings() const<br />
{<br />
    return m_Settings;<br />
}<br />
<br />
<br />
void Simulator::setSettings(Settings const &value)<br />
{<br />
    m_Settings = value;<br />
}<br />
<br />
/*Cache Set*/<br />
vector<CacheSet> Simulator::getCacheSet() const<br />
{<br />
    return m_CacheSet;<br />
}<br />
<br />
void Simulator::setCacheSet(vector<CacheSet>const &value)<br />
{<br />
    <br />
    int j = m_CacheSet.size();<br />
    m_CacheSet = value;<br />
    j = m_CacheSet.size();<br />
    j = getCacheSet().size();<br />
}<br />
<br />
/*Cache Blocks*/<br />
vector<CacheBlock> Simulator::getCacheBlocks() const<br />
{<br />
    return m_CacheBlock;<br />
}<br />
<br />
void Simulator::setCacheBlocks(vector<CacheBlock> const & value)<br />
{<br />
    m_CacheBlock = value;<br />
    int j = m_CacheBlock.size();<br />
    j = getCacheBlocks().size();<br />
}<br />
<br />
/* Data Buffer */<br />
vector<uint> Simulator::getDataBuffer() const<br />
{<br />
    return m_DataBuffer;<br />
}<br />
<br />
void Simulator::setDataBuffer(vector<uint> const &value)<br />
{<br />
    m_DataBuffer = value;<br />
}<br />


Don't forget to vote if the response was helpful


Sig history

"dad" Ishmail-Samuel Mustafa

Unix is a Four Letter Word, and Vi is a Two Letter Abbreviation

"There is no wealth like knowledge, no poverty like ignorance" Ali Ibn Abi Talib

GeneralRe: Dangling pointers and incorrect data being written Pin
Stuart Dootson15-Dec-08 13:37
professionalStuart Dootson15-Dec-08 13:37 
GeneralRe: Dangling pointers and incorrect data being written Pin
Mustafa Ismail Mustafa16-Dec-08 18:21
Mustafa Ismail Mustafa16-Dec-08 18:21 
GeneralRe: Dangling pointers and incorrect data being written Pin
Stuart Dootson16-Dec-08 19:40
professionalStuart Dootson16-Dec-08 19:40 
GeneralRe: Dangling pointers and incorrect data being written Pin
Mustafa Ismail Mustafa16-Dec-08 19:50
Mustafa Ismail Mustafa16-Dec-08 19:50 
GeneralRe: Dangling pointers and incorrect data being written Pin
Stuart Dootson16-Dec-08 21:52
professionalStuart Dootson16-Dec-08 21:52 
GeneralRe: Dangling pointers and incorrect data being written Pin
Mustafa Ismail Mustafa16-Dec-08 23:50
Mustafa Ismail Mustafa16-Dec-08 23:50 
GeneralRe: Dangling pointers and incorrect data being written Pin
Stuart Dootson17-Dec-08 2:34
professionalStuart Dootson17-Dec-08 2:34 
GeneralRe: Dangling pointers and incorrect data being written Pin
Mustafa Ismail Mustafa17-Dec-08 4:47
Mustafa Ismail Mustafa17-Dec-08 4:47 
GeneralRe: Dangling pointers and incorrect data being written Pin
Stuart Dootson17-Dec-08 5:18
professionalStuart Dootson17-Dec-08 5:18 
GeneralRe: Dangling pointers and incorrect data being written [modified] Pin
Mustafa Ismail Mustafa17-Dec-08 5:52
Mustafa Ismail Mustafa17-Dec-08 5:52 
GeneralRe: Dangling pointers and incorrect data being written Pin
Stuart Dootson17-Dec-08 6:25
professionalStuart Dootson17-Dec-08 6:25 
GeneralRe: Dangling pointers and incorrect data being written Pin
Mustafa Ismail Mustafa17-Dec-08 6:35
Mustafa Ismail Mustafa17-Dec-08 6:35 
GeneralRe: Dangling pointers and incorrect data being written Pin
Mustafa Ismail Mustafa14-Dec-08 21:12
Mustafa Ismail Mustafa14-Dec-08 21:12 
GeneralRe: Dangling pointers and incorrect data being written Pin
Mustafa Ismail Mustafa14-Dec-08 21:26
Mustafa Ismail Mustafa14-Dec-08 21:26 
QuestionDevelop CSP in XP Pin
izyani14-Dec-08 19:48
izyani14-Dec-08 19:48 
QuestionUsing Queue Timers...... Pin
shaina223114-Dec-08 19:22
shaina223114-Dec-08 19:22 
AnswerRe: Using Queue Timers...... Pin
Sarath C15-Dec-08 2:16
Sarath C15-Dec-08 2:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.