473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Memory Allocation Problem

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[i].file_data, file_string);

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

}

/*for(int i = 0; i < num_traces; i++)
cout << endl << ptr[i].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;
}

Oct 8 '06 #1
7 1884
try{
ptr = new CacheSimulator[];
The above line shouldn't even compile.
} catch (bad_alloc xa){
cout << endl << "Allocation Failure!!!" << endl;
return 1;
}
Give us the real code that compiles.

Ben
Oct 8 '06 #2

benben wrote:
try{
ptr = new CacheSimulator[];

The above line shouldn't even compile.
} catch (bad_alloc xa){
cout << endl << "Allocation Failure!!!" << endl;
return 1;
}

Give us the real code that compiles.

Ben
The code I posted does compile.

Oct 8 '06 #3
ucfcpegirl06 wrote:
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[i].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[i].file_data, file_string);

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

}

/*for(int i = 0; i < num_traces; i++)
cout << endl << ptr[i].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;
}
Oct 8 '06 #4
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.

Oct 8 '06 #5
ucfcpegirl06 wrote:
>
benben wrote:
try{
ptr = new CacheSimulator[];

The above line shouldn't even compile.
} catch (bad_alloc xa){
cout << endl << "Allocation Failure!!!" << endl;
return 1;
}

Give us the real code that compiles.

Ben

The code I posted does compile.
Then your compiler is broken. An array must always have a size.

Oct 8 '06 #6
ucfcpegirl06 wrote:
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.

Oct 8 '06 #7

Rolf Magnus wrote:
ucfcpegirl06 wrote:
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.

Oct 8 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
4
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a =...
15
by: berthelot samuel | last post by:
Hi, I'm trying to develop an application for modeling 3D objects from Bezier patches, but I have a memory allocation problem. Here are my structures: typedef struct _vector3 { union { struct...
7
by: Dan Nilsen | last post by:
Hi! I'm writing a small piece of software that basically runs on an embedded system with a Power-PC cpu. This runs on a stripped down version of Linux - Busybox. As I'm writing a piece of...
3
by: Florin | last post by:
Hi all, I have a problem related to memory grow on a server application which is basically stateless (I have some static info loaded). The client accesses this server using remoting and it has...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
9
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.