Memory Allocation Problem

U

ucfcpegirl06

Hello,

Maybe someone can help me with this. I believe I have a memory
allocation problem. The program crashes w/ a debug error.

cpp file:
#include <cstdio>
#include <cstring>
#include <iostream> // C++ I/O
#include <iomanip> // C++ I/O Manipulators
#include <fstream> // C++ File I/O
#include <cstdlib>
#include <new>


#include "CacheSimulator.h"

using namespace std;

int main(void)
{
CacheSimulator *ptr;
//CacheSimulator temp[7];

char cache_command[10], tracefile[20];
int size, assoc, blocksize, replacement_policy, write_policy;
long int num_traces = 0;
long int i = 0;

try{
ptr = new CacheSimulator[];
} catch (bad_alloc xa){
cout << endl << "Allocation Failure!!!" << endl;
return 1;
}

if(ptr == NULL)
cout << endl << "Error" << endl;

cout << "********************Cache Sim*************************" <<
endl;

cout << endl << "To use this simulator the following input format
must"
<< endl << "be followed:" << endl;

cout << endl << "Type input as follows (all on one line & w/o "
"arrows):" << endl
<< endl << "cache_sim <SIZE> <ASSOC> <BLOCKSIZE> <WRITE_POLICY> "
"<RP> <trace_file>" << endl;

cout << endl << "where SIZE = Cache size in bytes" << endl;
cout << " ASSOC = Set-associativity" << endl;
cout << " BLOCKSIZE = Block size in bytes (must be a power "
" of 2)" << endl;
cout << " WRITE POLICY = WTNA (1) or WBWA (0)" << endl;
cout << " REPLACEMENT POLICY = FIFO (0), True LRU (1), Random
(2), Optimal (3)"
<< endl;
cout << " trace_file = name of trace file" << endl;

cout << endl << endl << "Lets Begin!" << endl;
cout << endl << "Enter input here: ";
cin >> cache_command >> size >> assoc >> blocksize >> write_policy
>> replacement_policy >> tracefile;

//fstream file_op(tracefile, ios::in);
ifstream file_op(tracefile);
char file_string[20];

if(!file_op)
{
cout << endl << "File Not Found!!!" << endl;
return 1;
}

while( !file_op.eof() )
{
file_op.getline(file_string, 20);
num_traces++;

//strcpy(temp.file_data, file_string);

//memcpy(ptr.file_data, (const char *) file_string, 20);
strcpy(ptr.file_data, file_string);
i++;

}

/*for(int i = 0; i < num_traces; i++)
cout << endl << ptr.file_data << endl;*/

delete [] ptr;

cout << endl << "\t\tTotal Number of Traces: " << num_traces << endl;

file_op.close();

return 0;
}

header file
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>


using namespace std;

double log2(double var);

class CacheSimulator{
public:
char file_data[20];
CacheSimulator();
~CacheSimulator();
void process_data();
private:
long int num_reads;
long int num_rmisses;
long int num_writes;
long int num_wmisses;
};

CacheSimulator::CacheSimulator()
{
num_reads = 0;
num_rmisses = 0;
num_writes = 0;
num_wmisses = 0;
}

CacheSimulator::~CacheSimulator()
{
cout << endl << "\t\tSimulation Ended" << endl;
}
 
L

Larry Smith

ucfcpegirl06 said:
Hello,

Maybe someone can help me with this. I believe I have a memory
allocation problem. The program crashes w/ a debug error.

cpp file:
#include <cstdio>
#include <cstring>
#include <iostream> // C++ I/O
#include <iomanip> // C++ I/O Manipulators
#include <fstream> // C++ File I/O
#include <cstdlib>
#include <new>


#include "CacheSimulator.h"

using namespace std;

int main(void)
{
CacheSimulator *ptr;
//CacheSimulator temp[7];

char cache_command[10], tracefile[20];
int size, assoc, blocksize, replacement_policy, write_policy;
long int num_traces = 0;
long int i = 0;

try{
ptr = new CacheSimulator[];


There may be other problems in this code, but look at the
line above. How many entries do you want in
'CacheSimulator[]', 1, 20, 500? You must specify its
size, eg:

ptr = new CacheSimulator[20];

Otherwise code like

strcpy(ptr.file_data, file_string);

writes to who-knows-where, because 'i' is larger than
the size of CacheSimulator (which is un-specified in your
original code, but 20 in my example line above).

I'm not sure what the Standard says about empty arrays
( ie: '[]' ) passed to 'new', but I've got to believe
that it's at least 'undefined behaviour'.

Also, why not use C++ 'std::string' instead of C
nul-terminated strings (char arrays)? The you don't
have to worry about overflowing your 20 byte char buffers.


} catch (bad_alloc xa){
cout << endl << "Allocation Failure!!!" << endl;
return 1;
}

if(ptr == NULL)
cout << endl << "Error" << endl;

cout << "********************Cache Sim*************************" <<
endl;

cout << endl << "To use this simulator the following input format
must"
<< endl << "be followed:" << endl;

cout << endl << "Type input as follows (all on one line & w/o "
"arrows):" << endl
<< endl << "cache_sim <SIZE> <ASSOC> <BLOCKSIZE> <WRITE_POLICY> "
"<RP> <trace_file>" << endl;

cout << endl << "where SIZE = Cache size in bytes" << endl;
cout << " ASSOC = Set-associativity" << endl;
cout << " BLOCKSIZE = Block size in bytes (must be a power "
" of 2)" << endl;
cout << " WRITE POLICY = WTNA (1) or WBWA (0)" << endl;
cout << " REPLACEMENT POLICY = FIFO (0), True LRU (1), Random
(2), Optimal (3)"
<< endl;
cout << " trace_file = name of trace file" << endl;

cout << endl << endl << "Lets Begin!" << endl;
cout << endl << "Enter input here: ";
cin >> cache_command >> size >> assoc >> blocksize >> write_policy
>> replacement_policy >> tracefile;

//fstream file_op(tracefile, ios::in);
ifstream file_op(tracefile);
char file_string[20];

if(!file_op)
{
cout << endl << "File Not Found!!!" << endl;
return 1;
}

while( !file_op.eof() )
{
file_op.getline(file_string, 20);
num_traces++;

//strcpy(temp.file_data, file_string);

//memcpy(ptr.file_data, (const char *) file_string, 20);
strcpy(ptr.file_data, file_string);
i++;

}

/*for(int i = 0; i < num_traces; i++)
cout << endl << ptr.file_data << endl;*/

delete [] ptr;

cout << endl << "\t\tTotal Number of Traces: " << num_traces << endl;

file_op.close();

return 0;
}

header file
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>


using namespace std;

double log2(double var);

class CacheSimulator{
public:
char file_data[20];
CacheSimulator();
~CacheSimulator();
void process_data();
private:
long int num_reads;
long int num_rmisses;
long int num_writes;
long int num_wmisses;
};

CacheSimulator::CacheSimulator()
{
num_reads = 0;
num_rmisses = 0;
num_writes = 0;
num_wmisses = 0;
}

CacheSimulator::~CacheSimulator()
{
cout << endl << "\t\tSimulation Ended" << endl;
}
 
U

ucfcpegirl06

see thats the thing I was confused about.

I don't know how big CacheSimulator[] needs to be until I read every
line from the file.

The files have roughly 100,000 lines. Some more some less. I don't
know how to tell the program that I want to make room for this many
lines.
 
R

Rolf Magnus

ucfcpegirl06 said:
see thats the thing I was confused about.

I don't know how big CacheSimulator[] needs to be until I read every
line from the file.

The files have roughly 100,000 lines. Some more some less. I don't
know how to tell the program that I want to make room for this many
lines.

Best is not to. Instead use a vector and add the elements with its
push_back() member function. It will automatically grow when needed.
 
U

ucfcpegirl06

Rolf said:
ucfcpegirl06 said:
see thats the thing I was confused about.

I don't know how big CacheSimulator[] needs to be until I read every
line from the file.

The files have roughly 100,000 lines. Some more some less. I don't
know how to tell the program that I want to make room for this many
lines.

Best is not to. Instead use a vector and add the elements with its
push_back() member function. It will automatically grow when needed.

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top