Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > memory allocation issues?

Reply
Thread Tools

memory allocation issues?

 
 
sunilkher@hotmail.com
Guest
Posts: n/a
 
      12-06-2005
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.

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      12-06-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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!"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
memory allocation and freeing memory Rodrigo Dominguez C Programming 11 06-14-2005 11:54 PM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM



Advertisments