This is my python function:
if __name__ == "__main__":
file = open("data_input.txt", "r")
contents = file.read()
e = ast.literal_eval(contents)
neighbour_num= 4
new_data = 300
result = np.ndarray((4,4),dtype='int32',buffer=np.zeros((4,4),dtype='int32'))
c_module.mmult_wrapper(e, new_data, neighbour_num, result)
This is my C++ function, where i want to use dict as array
void mmult(double new_data, double neighbour_num, double e[100], int32_t result[16]) {
double distances[100];
unsigned distances_front = 0;
unsigned distances_back = 0;
double keys[100];
unsigned keys_front = 0;
unsigned keys_back = 0;
for(int i=0;i<(int)100; i=i+1) {
double temp_distance=(new_data-e[i]);
double sq_tmp=(temp_distance*temp_distance);
double sqrt=(sq_tmp/2);
double temp=0;
while(sqrt!=temp) {
temp = sqrt;
sqrt = (((sq_tmp/temp)+temp)/2);
}
distances[distances_front++] = (sqrt);
keys[keys_front++] = (e[i-1]);
}
for(int i=0; i<(int)(keys_front - keys_back); i=i+1) {
for(int j=(i+1); j<(int)(distances_front - distances_back); j=j+1) {
if(distances[i]>distances[j]) {
distances[i] = distances[j];
distances[j] = distances[i];
keys[i] = keys[j];
keys[j] = keys[i];
}
}
}
for(int i=0; i<(int)neighbour_num; i=i+1) {
result[i] = keys[i];
}
}
this is the wrapper function i have
#include <Python.h>
#include <numpy/arrayobject.h>
#include "spyc.h"
#include "caller.h"
static PyObject* mmult_wrapper(PyObject* self, PyObject* args) {
int32_t e;
int32_t new_data;
int32_t neighbour_num;
int32_t result;
int res = PyArg_ParseTuple(args, "Oi", &e_obj, &d);
if (!res)
return NULL;
/* call function */
mmult_caller(e,d);
}
My objective to accept the dict values from python function and convert it to array values suitable for C in the wrapper. I have no prior knowledge of C\C++ and i am stuck. Any help will be greatly helpful. thanks
you will likely need to include dictobject.h, floatobject.h, , if not already pulled in via other headers. Here is a way to grab all the values from a dict of floating point numbera to a vector. Depending on what's actually in the dict and how that's structured you may want to look at keys and other pieces.
Note that this is C++ and thus should be compiled as such.
//returns true on success
bool dictToVector(PyObject * srcDict, std::vector<double> & destVector) {
destVector.clear();
if(PyDict_Check(srcDict)) {
Py_ssize_t numItems = PyDict_Size(srcDict);
destVector.reserve(numItems);
Py_ssize_t iteratorPPOS = 0;
PyObject * currentVal;
while(PyDict_Next(srcDict, &iteratorPPOS, NULL, ¤tVal) {
// might be worth checking PyFloat_Check...
destVector.push_back(PyFloat_AsDouble(currentVal));
}
return (numItems == destVector.size());
}
else { // not a dict return with failure
return false;
}
}
Your wrapper will be similar at the end in that it will need to copy the result of mmult into the of result type numpy.ndarray.
Usage of the above something like:
PyObject* mmult_wrapper(PyObject * e, PyObject * new_data, PyObject * neighbour_num, PyObject * result) {
int32_t Cresult[16];
std::vector<double> Ce;
bool noErrorSoFar = dictToVector(e,Ce);
if(Ce.size() == 100) {
mmult(PyFloat_AsDouble(new_data) , PyFloat_AsDouble( neighbour_num), Ce.data(), Cresult);
}
else { // not 100 doubles in the data read!
noErrorSoFar = false;
}
... //some stuff to copy Cresult to the python, and return something meaningful?
}
Related
I find myself in a difficult situation. I have a program which is supposed to delete any memory that is dynamically allocated, but whenever I try to call the relevant methods, it comes up with a memory heap corruption.
It seems to work when I don't call the methods, but then I've probably caused a ton of memory leaks. Would anyone have any idea what is going on?
The code is below:
CSVFile.h:
#pragma once
class InputPattern;
class OutputPattern;
class CSVFile
{
private:
const int NAME_MAX = 100;
char* name;
char** buffer;
bool loadedFlag;
int patternCount;
InputPattern** inputs;
OutputPattern** outputs;
void setLoadedFlagTrue();
void setLoadedFlagFalse();
public:
CSVFile();
~CSVFile();
CSVFile(const char*);
void setName(const char*);
char* getFilename(char*, int);
bool getLoadedFlag();
int loadFile();
InputPattern* getInputPattern(int);
OutputPattern* getOutputPattern(int);
void addInputPattern(InputPattern*);
void addOutputPattern(OutputPattern*);
void deleteInputPattern();
void deleteOutputPattern();
void printMetaData();
void printPatterns();
void deleteBuffer();
};
CSVFile.cpp:
#include "CSVFile.h"
#include "InputPattern.h"
#include "OutputPattern.h"
#include <stdio.h>
#include <string.h>
void CSVFile::setLoadedFlagTrue()
{
loadedFlag = true;
}
void CSVFile::setLoadedFlagFalse()
{
loadedFlag = false;
}
CSVFile::CSVFile()
{
name = NULL;
buffer = NULL;
inputs = NULL;
outputs = NULL;
patternCount = 0;
inputs = new InputPattern*[10];
outputs = new OutputPattern*[10];
buffer = new char*[4];
int i;
for (i = 0; i < 10; i++)
{
inputs[i] = new InputPattern();
outputs[i] = new OutputPattern();
buffer[i] = new char[NAME_MAX];
}
}
CSVFile::~CSVFile()
{
delete name;
name = NULL;
}
CSVFile::CSVFile(const char * filename)
{
name = NULL;
buffer = NULL;
inputs = NULL;
outputs = NULL;
patternCount = 0;
inputs = new InputPattern*[10];
outputs = new OutputPattern*[10];
int i;
for (i = 0; i < 10; i++)
{
inputs[i] = new InputPattern();
outputs[i] = new OutputPattern();
}
name = new char[NAME_MAX];
strcpy(name, filename);
}
void CSVFile::setName(const char * filename)
{
name = new char[NAME_MAX];
strcpy(name, filename);
}
char* CSVFile::getFilename(char * outBuff, int outBuffSize)
{
outBuff = new char[outBuffSize + 1];
strncpy(outBuff, name, outBuffSize);
return outBuff;
}
bool CSVFile::getLoadedFlag()
{
if (name == NULL)
{
setLoadedFlagFalse();
return loadedFlag;
}
if (patternCount == 10)
setLoadedFlagTrue();
else
setLoadedFlagFalse();
return loadedFlag;
}
int CSVFile::loadFile()
{
FILE* f;
if ((f = fopen(name, "r")) == NULL)
{
printf("File failed to open\n");
return 0;
}
for (patternCount = 0; patternCount < 4; patternCount++)
{
fgets(buffer[patternCount], 100, f);
}
patternCount = 0;
/*ask about input interaction; potentially remove these variables afterwards*/
float tIn, rIn, gIn, bIn, tOut, oOut;
/*might change this to make it more flexible*/
while (patternCount < 10)
{
fscanf(f, "%f,%f,%f,%f,%f,%f", &tIn, &rIn, &gIn, &bIn, &tOut, &oOut);
printf("%f,%f,%f,%f,%f,%f\n", tIn, rIn, gIn, bIn, tOut, oOut);
inputs[patternCount]->setT(tIn);
inputs[patternCount]->setR(rIn);
inputs[patternCount]->setG(gIn);
inputs[patternCount]->setB(bIn);
outputs[patternCount]->setT(tOut);
outputs[patternCount]->setO(oOut);
patternCount++;
}
fclose(f);
return patternCount;
}
InputPattern * CSVFile::getInputPattern(int index)
{
if (index >= 0 && index < 10)
return inputs[index];
else
return 0;
}
OutputPattern * CSVFile::getOutputPattern(int index)
{
if (index >= 0 && index < 10)
return outputs[index];
else
return 0;
}
void CSVFile::addInputPattern(InputPattern * in)
{
inputs[patternCount] = in;
patternCount++;
}
void CSVFile::addOutputPattern(OutputPattern * out)
{
outputs[patternCount] = out;
patternCount++;
}
void CSVFile::deleteInputPattern()
{
int i;
for (i = 0; i < patternCount; i++)
{
delete inputs[i];
}
delete inputs;
inputs = NULL;
}
void CSVFile::deleteOutputPattern()
{
int i;
for (i = 0; i < patternCount; i++)
{
delete outputs[i];
}
delete outputs;
outputs = NULL;
}
void CSVFile::printMetaData()
{
int i;
for (i = 0; i < 4; i++)
{
printf("%s", buffer[i]);
}
}
void CSVFile::printPatterns()
{
/*to be completed*/
int i;
for (i = 0; i < patternCount; i++)
{
printf("Class number %d\n", i + 1);
printf("T in = %f\n", inputs[i]->getT());
printf("R in = %f\n", inputs[i]->getR());
printf("G in = %f\n", inputs[i]->getG());
printf("B in = %f\n", inputs[i]->getB());
printf("T out = %f\n", outputs[i]->getT());
printf("O out = %f\n", outputs[i]->getO());
}
}
void CSVFile::deleteBuffer()
{
int i;
for (i = 0; i < patternCount; i++)
{
delete buffer[i];
}
delete buffer;
buffer = NULL;
}
TestHarness.cpp sample (this is executed in the main function)
bool TestHarness::testCSVFileSetFilepath() /*this works fine*/
{
bool testResult = false;
CSVFile* test = NULL;
test = new CSVFile();
char *testName = NULL;
test->setName("test.txt");
testName = test->getFilename(testName, 10);
if (strcmp("test.txt", testName) == 0)
testResult = true;
delete test;
delete testName;
test = NULL;
testName = NULL;
return testResult;
}
...........................
bool TestHarness::testCSVFileLoadFile() /*this causes the corruption*/
{
bool testResult = false;
CSVFile* test = NULL;
test = new CSVFile();
test->setName("C:/Users/user/Documents/AssignmentsSem2/ExampleFile.csv");
if (test->loadFile() == 10)
testResult = true;
test->deleteInputPattern();
test->deleteOutputPattern();
test->deleteBuffer(); /*these three above methods are the ones I'm talking about*/
delete test;
test = NULL;
return testResult;
}
You can check for memory leaks with
#define _CRTDBG_MAP_ALLOC
#include<crtdbg.h>
struct AtExit
{
~AtExit()
{
_CrtDumpMemoryLeaks();
}
}doAtExit;
just outside the main method.
This runs whenever your program ends. All it really does is display whether you have a memory leak or not. Doesn't help with actually finding them.
You might need Visual Studio for this.
This is how it looks when a memory leak is found
Why does this code work:
#include <cstdio>
#include <cstdlib>
#define N n *
#define Q q *
#define S s *
typedef struct n
{
int data;
struct N nxt;
}n;
typedef struct q
{
N f;
N r;
}q;
typedef struct stack
{
Q q1;
Q q2;
}s;
Q Createq()
{
Q qq = (Q)malloc(sizeof(q));
qq->f = qq->r = 0;
return qq;
}
S CreateStk()
{
S stk = (S)malloc(sizeof(s));
stk->q1 = Createq();
stk->q2 = Createq();
return stk;
}
int Deq(Q qq)
{
if(qq->f == 0 && qq->r == 0) return -1;
N nn = qq->r;
int data = nn->data;
qq->r = qq->r->nxt;
free(nn);
if(!qq->r)
qq->f = 0;
return data;
}
void Enq(Q qq, int data)
{
if(!qq->f)
{
N nn = (N)malloc(sizeof(n));
nn->data = data;
nn->nxt = 0;
qq->f = qq->r = nn;
}
else
{
N nn = (N)malloc(sizeof(n));
nn->data = data;
nn->nxt = 0;
qq->f->nxt = nn;
qq->f = nn;
}
}
void Push(S stk, int data)
{
Enq(stk->q2,data);
while(stk->q1->f)
{
Enq(stk->q2,Deq(stk->q1));
}
Q t = stk->q1;
stk->q1 = stk->q2;
stk->q2 = t;
}
int Pop(S stk)
{
return Deq(stk->q1);
}
int main()
{
S stk = CreateStk();
Push(stk,10);
Push(stk,30);
Push(stk,40);
Push(stk,50);
printf("\nPopped: %d.", Pop(stk));
printf("\nPopped: %d.", Pop(stk));
printf("\nPopped: %d.", Pop(stk));
printf("\nPopped: %d.", Pop(stk));
return 0;
return 0;
}
Output:
Popped: 50.
Popped: 40.
Popped: 30.
Popped: 10.
While this does not:
#include <cstdio>
#include <cstdlib>
#define N n *
#define Q q *
typedef struct n
{
int data;
struct N nxt;
}n;
typedef struct q
{
N f;
N r;
}q;
Q Createq()
{
Q qq = (Q)malloc(sizeof(q));
qq->f = qq->r = 0;
return qq;
}
int Deq(Q qq)
{
if(qq->f == 0 && qq->r == 0) return -1;
N nn = qq->r;
int data = nn->data;
qq->r = qq->r->nxt;
free(nn);
if(!qq->r)
qq->f = 0;
return data;
}
void Enq(Q qq, int data)
{
if(!qq->f)
{
N nn = (N)malloc(sizeof(n));
nn->data = data;
nn->nxt = 0;
qq->f = qq->r = nn;
}
else
{
N nn = (N)malloc(sizeof(n));
nn->data = data;
nn->nxt = 0;
qq->f->nxt = nn;
qq->f = nn;
}
}
void Push(Q qq1, Q qq2, int data)
{
Enq(qq2,data);
while(qq1->f)
{
Enq(qq2,Deq(qq1));
}
Q t = qq1;
qq1 = qq2;
qq2 = t;
}
int Pop(Q qq1)
{
return Deq(qq1);
}
int main() {
// your code goes here
Q qq1 = Createq();
Q qq2 = Createq();
Push(qq1,qq2,10);
Push(qq1,qq2,30);
Push(qq1,qq2,40);
Push(qq1,qq2,50);
printf("\nPopped: %d.", Pop(qq1));
printf("\nPopped: %d.", Pop(qq1));
printf("\nPopped: %d.", Pop(qq1));
printf("\nPopped: %d.", Pop(qq1));
return 0;
}
Output:
Popped: -1.
Popped: -1.
Popped: -1.
Popped: -1.
The expected output is the first one as its obvious by the question heading. However i do not understand the gory details of why the code did not work when i did not encapsulate the 2 queues in a struct in my second example.
PS: I think the problem is with the Push method - but not sure what went wrong.
Aside from being horrendously hard to read, the problem is that your second Push function changes the values of its parameters in the following lines of code.
Q t = qq1;
qq1 = qq2;
qq2 = t;
qq1 and qq2 are parameters of the function, so the new values are not updated in the calling function (main).
One way to fix this is to make the parameters pass by reference:
void Push(Q &qq1, Q &qq2, int data)
That way, changes to qq1 and qq2 will also change the values in the calling function.
I have been trying to fully implement my hashing table, but I don't really understand exactly how the code works. I have my header file setup fine, I am just missing implementing the following:
V * find(K key) - which I want to return a pointer to the value I am looking for, or null obviously.
bool addOrMod(K key, V val) - which adds a new element, returning the status (true or false) for if the value is added.
int size() - which simply returns how many items are in my Dictionary.
I also think I didn't initialize SZ correctly in int hash(string s)
I appreciate your help.
Here is my class:
#include "Dic.h"
#include <string>
int Dic::hash(K key){ //DEPENDS ON K. This one assumes K is string
return -1; //stub code
}
void Dic::deallocate(){ //separate member called by destruc and op=
for(int i=0; i<SZ; i++){
//get rid of chain i
DicNode * p = table[i];
while(p!=0){
DicNode * kill = p;
p = p->nxt;
delete kill;
}
}
delete [] table;
}
V * find(K) {
return *location;
}
bool addOrMod(K, V){
return added;
}
int size(){
return size;
}
//-----------------------------------------------------------------
//BIG 3
Dic::Dic() {
}
int hash(string s) {
int ret=0;
int SZ = s.length();
for (int i=0; i<s.size(); i++) {
ret+=s[i]-'A';
}
return ret%SZ;
}
Dic::~Dic(){this->deallocate();}
Dic::Dic( const Dic & src ){ //copy con
*this = src; //Uses operator= defined for Dic
}
Dic & Dic::operator=( const Dic & rhs ){ //assignment op
if(this == &rhs){ cout<<"goofy"<<endl; return *this; }
// clean up any memory allocated by this
this->deallocate();
// initialize this n,SZ,table to be like rhs
this->n=rhs.n; this->SZ = rhs.SZ; this->table=new DicNode*[SZ];
// duplicate the DicNode chains
for(int i=0;i<SZ;i++){
DicNode * q = rhs.table[i];
if(q==0){
this->table[i]=0;
}else{
this->table[i]=new DicNode; //note: NOT DicNode()
DicNode * p = this->table[i];
while(true){ //loop inv: *p is blank node corresp to *q
p->key = q->key; p->val = q->val;
if(q->nxt==0)break;
q=q->nxt; p->nxt=new DicNode; p=p->nxt;
}
p->nxt=0;
}
}
return *this;
}
This question already has answers here:
How to replicate vector in c?
(6 answers)
Closed 2 years ago.
I have a code (C++) that looks like this
vector<int> values[10000];
int i, j;
while (.....) {
scanf("%d%d", &i, &j);
values[i].push_back(j);
values[j].push_back(i);
}
but I want to rewrite this code to C. How can I do this?
I researched the opportunity to make the own stack, but maybe have more lightweight way to rewrite this code, maybe two-dimensional arrays. So far I can not think how this remake, I hope that someone more experienced tell me how to do it :)
Sorry guys, added a more advanced example...
Instead of rolling your own, you may want to try a C container library, e.g. http://code.google.com/p/ccl/
You can use Gena library. It closely resembles stl::vector in pure C89.
You can check it out here:
https://github.com/cher-nov/Gena
Something like this:
#include <stdio.h>
#include <stdlib.h>
typedef struct _darray
{
size_t size;
size_t actual_size;
int *content;
} darray;
void darray_create(darray *d)
{
d->actual_size = d->size = 0;
d->content = NULL;
}
void darray_append(darray *d, int v)
{
if (d->size+1 > d->actual_size)
{
size_t new_size;
if (!d->actual_size)
{
new_size = 1;
}
else
{
new_size = d->actual_size * 2;
}
int *temp = realloc(d->content, sizeof(int) * new_size);
if (!temp)
{
fprintf(stderr, "Failed to extend array (new_size=%zu)\n", new_size);
exit(EXIT_FAILURE);
}
d->actual_size = new_size;
d->content = temp;
}
d->content[d->size] = v;
d->size++;
}
const int* darray_data(darray *d)
{
return d->content;
}
void darray_destroy(darray *d)
{
free(d->content);
d->content = NULL;
d->size = d->actual_size = 0;
}
size_t darray_size(darray *d)
{
return d->size;
}
int main()
{
int i;
darray myarray;
const int *a;
darray_create(&myarray);
for(i = 0; i < 100; i++)
{
darray_append(&myarray, i);
}
a = darray_data(&myarray);
for(i = 0; i < darray_size(&myarray); i++)
{
printf("i=%d, value=%d\n", i, a[i]);
}
darray_destroy(&myarray);
}
You can try something like this:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct vector
{
int len;
int allocated;
int step;
int *data;
};
#define INIT_SIZE 1
void init_vector(struct vector *v)
{
v->len = 0;
v->allocated = 0;
v->step = 2;
v->data = NULL;
}
int append(struct vector *v, int item)
{
if (!v->data)
{
v->data = malloc(INIT_SIZE * sizeof(int));
if (!v->data)
return -1;
v->allocated = INIT_SIZE;
}
else
if (v->len >= v-vallocated)
{
int *tmp = realloc(v->data,
v->allocated * v->step * sizeof(int));
if (!tmp)
return -1;
v->data = tmp;
v->allocated *= v->step;
}
v->data[v->len] = item;
v->len++;
return 0;
}
int delete(struct vector *v, int index)
{
if (index < 0 || index >= v->len)
return -1;
memmove(v->data + index, v->data + index + 1,
(v->len - index - 1) * sizeof(int));
v->len--;
return 0;
}
void print(const struct vector *v)
{
printf("Array:\n");
for (int i = 0; i < v->len; i++)
printf("%d ", v->data[i]);
printf("\n");
}
int main(void)
{
struct vector v;
int rc;
init_vector(&v);
rc = append(&v, 1);
assert(rc == 0);
rc = append(&v, 2);
assert(rc == 0);
rc = append(&v, 3);
assert(rc == 0);
rc = append(&v, 4);
assert(rc == 0);
rc = append(&v, 5);
assert(rc == 0);
print(&v);
rc = delete(&v, 2);
assert(rc == 0);
print(&v);
free(v.data);
return 0;
}
A rough equivalent of a C++ vector would be a resizing C array (to account for more elements than available).
Ergo, the equivalent of an array of vectors would be an array of pointers (an array of arrays wouldn't cut it because of the resizing constraint).
int* values[1000];
You'll need to account for the sizes though, so you could either do that externally or wrap the logic inside a structure.
int sizes[1000];
int noElements[1000];
// all sizes and noElements initially 0
for (int i = 0; i < 10; i++) {
if ( noElements[i] >= sizes[i] )
{
// allocate more memory for values[i];
// copy old contents into the new memory
// update sizes[i]
}
values[i][noElements] = 10;
noElements++;
}
There is no C standard equivalent to the c++ vector, though you could create a struct based off of the vector in c++. The struct would
Resize itself if the array bounds are passed the max size
perform the operations similar to that of a vector
OR
Create a linked list stack struct that simulates that of a c++ vector
I'm affraid you'll have to work with heap memory in 80's fashion in the plain C.
typedef struct tagArrayDesc {
int* arr;
size_t top;
size_t reserved;
} ArrayDesc;
#define EC(NAME, T) size_t ensure_capacity##NAME##(size_t size, \
T** vec, \
size_t reserved) \
{ \
size_t new_reserved; \
new_reserved = reserved; \
if (reserved < size) { \
if (reserved != 0) { \
new_reserved *= 2; \
} else { \
new_reserved = 0x10; \
} \
} \
if (new_reserved < size) { \
new_reserved = (size * 4) / 3; \
} \
if (new_reserved > reserved) { \
*vec = realloc(*vec, sizeof(**vec) * new_reserved); \
memset((*vec) + reserved, 0, sizeof(T) * (new_reserved - reserved)); \
} \
return new_reserved; \
}
EC(_int, int)
EC(_array_desc, ArrayDesc)
int main()
{
ArrayDesc* rows = NULL;
size_t rows_size = 0;
size_t rows_reserved = 0;
while (true) {
int i, j;
scanf("%d%d", &i, &j);
rows_reserved = ensure_capacity_array_desc(i + 1, &rows, rows_reserved);
rows[i].reserved = ensure_capacity_int(j + 1, &rows[i].arr, rows[i].reserved);
rows[i].arr[j] = 42;
}
return 0;
}
You have to work with dynamic memory allocation. It's not hard. Every time when a new item must be inserted just use realloc. Somethink that looks like this:
#include <cstdlib>
typedef struct { } UserType;
int currentSize = 0;
UserType* values;
/// Add new value to values method
void addValue(const UserType& newValue)
{
++currentSize;
values = static_cast<UserType*>(realloc(values, currentSize));
if (values == NULL)
// memory allocation filed, place fix code here
*(values + currentSize) = newValue;
}
Remember, u have to use free for free memory of the values. Also, you may don't free allocated memory if will end work right now.
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:
int min = treeArray[set]->root->findPLRU();
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?
#include "stdafx.h"
#include "stdlib.h"
#include <conio.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <io.h>
#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 >> 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 < 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 < 0 || rightCacheLine < 0)
{
if (goRight)
{
goRight = false;
return right->findPLRU();
}
else
{
goRight = true;
return left->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 < 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 < n; i++)
{
treeArray[i] = new Tree();
}
}
access = -1;
}
//destructor
~Cache()
{
free(cache);
}
//test for hit
void hit(int a)
{
access++;
int set = (a >> log2l) & (n - 1);
int tag = a >> (log2n + log2l);
CacheLine* c = &cache[set*k];
for (int i = 0; i < 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 < k; i++)
{
if (c[i].access < minv)
{
minv = c[i].access;
min = i;
}
}
c[min].tag = tag;
c[min].access = access;
}
else if(replacementPolicy == 1) // pseudoLRU
{
int min = treeArray[set]->root->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 < bytes / (int) sizeof(trace); i++, tcount++)
{
word0 = buf[i].word0;
a = (word0 & ADDRESSMASK) << 2;
type = (word0 >> TYPESHIFT) & TYPEMASK;
burstcount = ((word0 >> BURSTSHIFT) & BURSTMASK) + 1;
cache0->hit(a);
cache1->hit(a);
cache2->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;
}
Your question hasn't yet been pounced upon, probably because your code still doesn't compile since you've not provided main.h.
And even then it would annoy most folks trying to help you because you make no mention of the ico0.trace file that is required to prevent the code from immediately exiting.
You say int min = treeArray[set]->root->findPLRU(); access violates.
1) the value of set can never exceed the size n of your treeArray since you & n-1 the range of input values.
2) since your ~Tree() destructor is never called there will always be a treeArray[set]->root
3) since you *always create new left & right nodes whenever leftCacheLine = -1 or rightCacheLine = -1 it cannot be due to recursive findPLRUs
So, the pointer to the node is not being "lost" somewhere; it is being stomped on.
Try replacing:
int min = treeArray[set]->root->findPLRU();
c[min].tag = tag;
c[min].access = access;
with:
int min = treeArray[set]->root->findPLRU();
if (min >= k*n)
{
printf("ook\n");
}
else
{
c[min].tag = tag;
c[min].access = access;
}
and I think you will discover what's doing the stomping. ;)