Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ pointer "losing" its value
    text
    copied!<p>As an exercise (largely an exercise in trying to write something using pointers), I'm writing a cache simulation, specifically of the pseudo least recently used system from the old 486. I'm getting an "Access violation reading location" error on the line:</p> <pre><code>int min = treeArray[set]-&gt;root-&gt;findPLRU(); </code></pre> <p>Initially the treeArray seems to be initialised properly (if I pause the program at the start and take a look, it's all as should be), but when the programme breaks and I delve in to examine things the root of the tree in question isn't defined. I feel it's quite probable that I'm making some sort of very elementary pointer mistake, which is causing the pointer to the node to be "lost" somewhere, but I've no clue what it might be. Is there something in particular I need to do to "hold on" to a pointer value?</p> <p>Thanks in advance for any help you can give!</p> <pre><code>#include "stdafx.h" #include "stdlib.h" #include &lt;conio.h&gt; #include &lt;stdio.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #include &lt;time.h&gt; #include &lt;string.h&gt; #include &lt;io.h&gt; #include "main.h" //char fn[80]; // trace filename int tf; // trace file trace buf[BUFSZ / sizeof(trace)]; // buffer SIZE int LRUHits = 0; int pLRUHits = 0; int randomHits = 0; int height; int cachelinenumber; //log2 helper function int log2(int n) { int i = 0; while (n) { n = n &gt;&gt; 1; i++; } return i - 1; } class CacheLine{ public: int tag; int access; CacheLine(); }; class Cache; class Node{ public: bool goRight; Node* left; Node* right; int leftCacheLine; int rightCacheLine; Node(int depth) // constructor { goRight = false; if (depth &lt; height - 1) { left = new Node(depth + 1); right = new Node(depth + 1); leftCacheLine = -1; rightCacheLine = -1; } else { leftCacheLine = cachelinenumber; cachelinenumber++; rightCacheLine = cachelinenumber; cachelinenumber++; } //printf("Depth: %d, Height: %d, Left: %d, Right: %d\n", depth, height, leftCacheLine, rightCacheLine); } ~Node() { delete left; delete right; } int findPLRU() { if (leftCacheLine &lt; 0 || rightCacheLine &lt; 0) { if (goRight) { goRight = false; return right-&gt;findPLRU(); } else { goRight = true; return left-&gt;findPLRU(); } } else { if (goRight) { goRight = false; return rightCacheLine; } else { goRight = true; return leftCacheLine; } } } }; class Tree{ public: Node* root; Tree() { root = new Node(0); } ~Tree() { delete root; } }; //cache class class Cache { public: CacheLine *cache; int l, k, n, replacementPolicy; int log2l, log2n; int access; Tree** treeArray; //constructor Cache(int ll, int kk, int nn, int _replacementPolicy) { l = ll; k = kk; n = nn; replacementPolicy = _replacementPolicy; log2l = log2(l); log2n = log2(n); cache = (CacheLine*)malloc(sizeof(CacheLine)*k*n); for (int i = 0; i &lt; k*n; i++) { cache[i].tag = 0x80000000; cache[i].access = 0; } if (replacementPolicy == 1) { cachelinenumber = 0; treeArray = new Tree*[n]; for (int i = 0; i &lt; n; i++) { treeArray[i] = new Tree(); } } access = -1; } //destructor ~Cache() { free(cache); } //test for hit void hit(int a) { access++; int set = (a &gt;&gt; log2l) &amp; (n - 1); int tag = a &gt;&gt; (log2n + log2l); CacheLine* c = &amp;cache[set*k]; for (int i = 0; i &lt; k; i++) { if (c[i].tag == tag) { c[i].access = access; if (replacementPolicy == 0) LRUHits++; else if (replacementPolicy == 1) pLRUHits++; else if (replacementPolicy == 2) randomHits++; break; } } if (replacementPolicy == 0) //LRU { int min = 0; int minv = c[0].access; for (int i = 1; i &lt; k; i++) { if (c[i].access &lt; minv) { minv = c[i].access; min = i; } } c[min].tag = tag; c[min].access = access; } else if(replacementPolicy == 1) // pseudoLRU { int min = treeArray[set]-&gt;root-&gt;findPLRU(); c[min].tag = tag; c[min].access = access; } else // random { srand(clock()); int randomNumber = rand()%k; c[randomNumber].tag = tag; c[randomNumber].access = access; } return; } }; void analyse (int l, int k, int n) { height = log2(k) + 1; char fn[] = "ico0.trace"; if ((tf = open(fn, _O_RDONLY | _O_BINARY )) == -1) { printf("unable to open file %s\n", fn); exit(0); } LRUHits = 0; pLRUHits = 0; randomHits = 0; Cache *cache0 = new Cache(l, k, n, 0); // LRU Cache *cache1 = new Cache(l, k, n, 1); // pseudoLRU Cache *cache2 = new Cache(l, k, n, 2); // random int bytes, word0, a, type, burstcount; int hits = 0; int tcount = 0; while (bytes = read(tf, buf, sizeof(buf))) { for (int i = 0; i &lt; bytes / (int) sizeof(trace); i++, tcount++) { word0 = buf[i].word0; a = (word0 &amp; ADDRESSMASK) &lt;&lt; 2; type = (word0 &gt;&gt; TYPESHIFT) &amp; TYPEMASK; burstcount = ((word0 &gt;&gt; BURSTSHIFT) &amp; BURSTMASK) + 1; cache0-&gt;hit(a); cache1-&gt;hit(a); cache2-&gt;hit(a); } } printf("Hits: %d Total: %d\n", LRUHits, tcount); printf("Hits: %d Total: %d\n", pLRUHits, tcount); printf("Hits: %d Total: %d\n\n\n", randomHits, tcount); delete cache0; delete cache1; delete cache2; } int _tmain(int argc, _TCHAR* argv[]) { //analyse(16, 1, 8); analyse(16, 2, 512); //analyse(16, 4, 256); //analyse(16, 8, 128); //analyse(16, 1024, 1); _getch(); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload