473,398 Members | 2,088 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,398 software developers and data experts.

memory allocation issues?

Hi, here are the classes I have ...

File chedCache.h

#ifndef CHED_CACHE_INCLUDE
#define CHED_CACHE_INCLUDE

#include <map>
#include "chedView.h"

#define CHED_CACHE_MAP map <chedView, chedView>

class chedCache
{
public:
//--------------------------------------------------------------
// Constructor and Destructor

//--------------------------------------------------------------
chedCache() { };
~chedCache() { };
//--------------------------------------------------------------
// Accessor Methods

//--------------------------------------------------------------
const char *find(const char *key, const int keySize);
//--------------------------------------------------------------
// Mutator Methods

//--------------------------------------------------------------
void insert(const char *key, const int keySize, const
char *value, const int valueSize);

private :

CHED_CACHE_MAP _myCache;
CHED_CACHE_MAP::const_iterator _myIter;

};

file chedView.h

#ifndef CHED_VIEW_INCLUDE
#define CHED_VIEW_INCLUDE

#define MINIMUM(A,B) ((A) < (B) ? (A) : (B))

class chedView
{
public:
//--------------------------------------------------------------
// Constructor and Destructor

//--------------------------------------------------------------
chedView() { _mySize = 0; _myView = NULL; }
chedView(const char *myView, const int mySize) :
_mySize(mySize)
{
_myView = new char[mySize];
memcpy(_myView, myView, mySize);
cout << "constrcted " << _myView << " of size
: " << _mySize << endl;
}
~chedView() { if (_myView != NULL) delete [] _myView; }
//--------------------------------------------------------------
// Accessor Methods

//--------------------------------------------------------------
const char *getView() const { return _myView; }
const int getSize() const { return _mySize; }
//--------------------------------------------------------------
// Operators

//--------------------------------------------------------------
bool operator==(const chedView &a) const
{
cout << "operator == comparing the following"
<< endl;
cout << "my View = " << _myView << endl;
cout << "input View = " << a.getView() << endl;
return _mySize==a._mySize && memcmp(_myView,
a.getView(), _mySize);
}

bool operator<(const chedView &a) const
{
cout << "operator < comparing the following" <<
endl;
cout << "my View = " << _myView << endl;
cout << "input View = " << a.getView() << endl;
cout << "memcmp = " << memcmp(_myView,
a.getView(), MINIMUM(_mySize, a._mySize)) << endl;
return memcmp(_myView, a.getView(),
MINIMUM(_mySize, a._mySize))<0;
}

bool operator>(const chedView &a) const
{
return a < *this;
}
//--------------------------------------------------------------
// Mutator Methods

//--------------------------------------------------------------

private :

char *_myView;
int _mySize;

};

#endif
file chedCache.c
#include "chedCache.h"

/************************************************** *****************************
*
* Method Name: chedCache::insert
*
* Input Parameters: const char *key, const char *value
*
* Summary: insert the key, value pair in the cache
*
* Return Values: none
*
************************************************** *****************************/
void chedCache::insert(const char *key, const int keySize, const char
*value, const int valueSize)
{
chedView myKey(key, keySize), myValue(value, valueSize);
_myCache[myKey] = myValue;
_myIter = _myCache.begin();
while (_myIter != _myCache.end())
{
printf("insert -> found %s and %s in the cache\n",
(_myIter->first).getView(), (_myIter->second).getView());
++_myIter;
}

}
/************************************************** *****************************
*
* Method Name: chedCache::find
*
* Input Parameters: const char *key
*
* Summary: insert the key, value pair in the cache
*
* Return Values: NULL if no cache hit and cache stored object if
cache hit
*
************************************************** *****************************/
const char *chedCache::find(const char *key, const int keySize)
{
_myIter = _myCache.begin();
while (_myIter != _myCache.end())
{
printf("find first -> found %s and %s in the cache\n",
(_myIter->first).getView(), (_myIter->second).getView());
++_myIter;
}

cout << "constructing key to be searched " << endl;
chedView myView(key, keySize);
printf("chedView myView in find is %s \n", myView.getView());
cout << "constructed key to be searched " << endl;

_myIter = _myCache.begin();
while (_myIter != _myCache.end())
{
printf("find second -> found %s and %s in the cache\n",
(_myIter->first).getView(), (_myIter->second).getView());
++_myIter;
}

_myIter = _myCache.find(myView);
if (_myIter == _myCache.end()) printf("no dice\n"); else
printf("found\n");

return
((_myIter = _myCache.find(chedView(key, keySize)))
== _myCache.end()) ? NULL :
(_myIter->second).getView();
}
and finally the main program

#include "chedCache.h"
#define APP_SUCCESS 0
#define APP_FAILURE 1

void printTime(const int iter, const bool start);
typedef struct mySendView {
char racn_bsln_cd[2];
mySendView() {
initview();
}
void initview() {
memset(this, 0, sizeof(mySendView));
}
const string getName() const { return "chra_sel_RACNBaselineS";
}
} mySendView;

typedef struct myRetnView {
char racn_base_dt[31];
myRetnView() {
initview();
}
void initview() {
memset(this, 0, sizeof(myRetnView));
}
const string getName() const { return "chra_sel_RACNBaselineR";
}
} myRetnView;

int getData(mySendView &, myRetnView &);

/************************************************** *******************
* Function: eaAppInterface::bus
*
* Description: <description of function>
*
* Parameters:
*
* Error Messages:
************************************************** *******************/
int eaAppInterface::bus( int argc, const char* argv[], const char*
envp[] )
{
//-----------------------------------
// did we get proper input parameters
//-----------------------------------
if (argc < 2)
{
eaLogMsg << setid("INVALID_FUNCTION_USAGE")
<< "edCache::bus" << "$BIN/edCache
totalIter" << endl;
return APP_FAILURE;
}

//----------------------------
// declare necessary variables
//----------------------------
mySendView selRACNBslnS;
myRetnView selRACNBslnR;
int totalIter = atoi(argv[1]);
chedCache myCache;
const char *myEntry;

//------------------------
// now comes the real work
//------------------------
for (int i=1; i<=totalIter; i++)
{

//-----------------------
// populate the send view
//-----------------------
if (i%2 == 0)
strncpy(selRACNBslnS.racn_bsln_cd, "D",

sizeof(selRACNBslnS.racn_bsln_cd) - 1 );
else
strncpy(selRACNBslnS.racn_bsln_cd, "I",

sizeof(selRACNBslnS.racn_bsln_cd) - 1 );

//-----------------------
// is this data in cache?
//-----------------------
cout << "searching for " << selRACNBslnS.racn_bsln_cd
<< " in cache for iteration " << i <<
endl;
myEntry = myCache.find((const char *)&selRACNBslnS,
sizeof(selRACNBslnS));

//-----------
// yes, it is
//-----------
if (myEntry != NULL)
{
cout << " ... found " << endl;
memcpy(&selRACNBslnR, myEntry,
sizeof(selRACNBslnR));
}
//-------------------
// not found in cache
//-------------------
else
{
cout << " ... not found " << endl;

//-------------------------------------------------
// hopefully we can retrieve data from the
database

//-------------------------------------------------
if (getData(selRACNBslnS, selRACNBslnR) !=
APP_SUCCESS)
{
eaLogMsg << setid("WHATEVER") <<
"edCache::bus failed" << endl;
return APP_FAILURE;
}

//---------------------------------------
// store this in the cache for future use
//---------------------------------------
cout << "inserting " <<
selRACNBslnR.racn_base_dt
<< " in cache for iteration "
<< i << endl;
myCache.insert(
(const char *)&selRACNBslnS,
sizeof(selRACNBslnS),
(const char *)&selRACNBslnR,
sizeof(selRACNBslnR));
}

//------------------------------------
// let's make sure everything was okay
//------------------------------------
cout << "Iteration #" << i
<< " yields " <<
selRACNBslnR.racn_base_dt << endl;

}

//----------------
// declare victory
//----------------
return APP_SUCCESS;
}

int getData(mySendView &selRACNBslnS, myRetnView &selRACNBslnR)
{
//-------------
// do something
//-------------
if (strcmp(selRACNBslnS.racn_bsln_cd, "D"))
strncpy(selRACNBslnR.racn_base_dt, "02/06/1999
06:00:00", 30);
else
strncpy(selRACNBslnR.racn_base_dt, "02/09/1999
06:00:00", 30);

//-----------------------
// everything is okay now
//-----------------------
return APP_SUCCESS;
}

When I run this with an input parameter of 2, here is the output I get.

searching for I in cache for iteration 1
constructing key to be searched
constrcted I of size : 2
chedView myView in find is I
constructed key to be searched
no dice
constrcted I of size : 2
... not found
inserting 02/06/1999 06:00:00 in cache for iteration 1
constrcted I of size : 2
constrcted 02/06/1999 06:00:00 of size : 31
insert -> found I and 02/06/1999 06:00:00 in the cache
Iteration #1 yields 02/06/1999 06:00:00
searching for D in cache for iteration 2
find first -> found I and 02/06/1999 06:00:00 in the cache
constructing key to be searched
constrcted D of size : 2
chedView myView in find is D
constructed key to be searched
find second -> found D and in the cache
operator < comparing the following
my View = D
input View = D
memcmp = 0
operator < comparing the following
my View = D
input View = D
memcmp = 0
found
constrcted D of size : 2
operator < comparing the following
my View = D
input View = D
memcmp = 0
operator < comparing the following
my View = D
input View = D
memcmp = 0
... found
Iteration #2 yields
My problem is when I am going for the 2nd iteration through the main
program, after calling the find function for the chedCache class, it's
member variable gets garbled and I get unexpected results ie for the
2nd iteration, I am not supposed to find that particular entry in the
cache but I do and get incorrect results.

PLEASE HELP.

Dec 6 '05 #1
1 1673
su*******@hotmail.com wrote:
Hi, here are the classes I have ...


That's very nice.

First, read: http://www.catb.org/~esr/faqs/smart-questions.html

Then, cut your code to a better formatted (consider the medium), more
easily digestible, runnable snippet that exhibits your problem.

Doing so will greatly enhance your chances of getting meaningful help.
Which you clearly need.

And, if you do the above. you will get.

HTH,
--ag
[pages and pages of badly formatted rambling code snipped -- containing
obvious bad design decisions, ill thought out defines and bad data
structure choices...]

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 6 '05 #2

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

Similar topics

4
by: Alan Gifford | last post by:
I wrote a program to make sure that new would throw a bad_alloc exception if more memory was requested than was available. On my system, new allocates up to 2931 MBs of memory (I don't have that...
18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
6
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the...
11
by: William Buch | last post by:
I have a strange problem. The code isn't written by me, but uses the qsort function in stdlib. ALWAYS, the fourth time through, the memory location of variable list (i.e. mem location = 41813698)...
6
by: sangeetha_b | last post by:
Hello, I've writen one simple program to automate some manual process. I've written that in c program. It works fine so far no problem reported on this. Last week, i get chance to run my program...
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...
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
12
by: David Given | last post by:
I have a situation where I need to be able to allocate chunks on unmapped virtual memory. Because the memory I'm allocating isn't mapped, I can't use a normal memory allocator, as most of them...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.