Will there be memory leak ?????

V

Vijay

Hi ,
I have created a program using CMapStrintToPtr where I would be
mapping
structure ptr to map string. This is just a sample program which I
thought
to avoid linear search in link list since my actual data is so huge.

I created a Windows console program. Since I am assigning a memory
block to
the structure node and I am not freeing it anyway in the program. Will
there be memory leak. Is it necessary to free the memory in my code.
If I want to free
how should I free it. I am thinking of get cmP.GetCount then get 1st
strcuture ptr by cmP.GetStartPosition then go through each by
cmP.GetNextAssoc and free
each structure ptr we get. Is this correct. Please tell more.

My code is

#include <iostream.h>
#include <afxcoll.h>
#include <conio.h>

CMapStringToPtr cmP;

struct vj
{
int type;
char name_type[100];
};

void mapFunction(char *mapStr, int typ, char *typeName)
{

struct vj *vijay;
vijay = (struct vj *) malloc (sizeof (struct vj));
strcpy(vijay->name_type, typeName);
vijay->type = typ;

cmP.SetAt ( mapStr, vijay);
}

void main()
{

mapFunction("li", 3, "line");
mapFunction("ls", 4, "linestring");
mapFunction("sh", 5, "shape");

struct vj *temp;
cmP.Lookup ("sh", (void*&)temp);
cout<< temp->type <<" " << temp->name_type << endl;
getch();

}
Thanks vijay.
 
I

Ivan Vecerina

Vijay said:
I have created a program using CMapStrintToPtr where I would be
mapping
structure ptr to map string. This is just a sample program which I
thought
to avoid linear search in link list since my actual data is so huge.
CMapStringToPtr is not a standard C++ class. Most people here do
not know (or want to know) what it does.
I created a Windows console program. Since I am assigning a memory
block to
the structure node and I am not freeing it anyway in the program. Will
there be memory leak. Is it necessary to free the memory in my code.
Yes, Yes.
If I want to free how should I free it.
Really, you should use better designed classes, such as those
available in the standard C++ library, that will save you from
doing manual memory management.
See below how leaner and cleaner your program gets -- not to mention
safer and more portable:

#include <iostream> //NB: iostream.h is non-standard.
#include <map>
#include <string>
//using namespace std; // put this to not repeat std:: below

struct vj // could eventually be replaced by a std::pair ...
{
int type;
std::string name_type;
vj(int t, std::string n) : type(t), name_type(n) {} //added
};

//nb: no mapFunction needed, the constructor replaces it

int main() // void main() is NOT allowed in standard C++
{
std::map< std::string, vj > cmP;

cmP["li"] = vj(3,"line");
cmP["ls"] = vj(4,"linestring");
cmP["sh"] = vj(5,"shape");

//NB: cmP.Lookup ("sh", (void*&)temp) --> was undefined behavior
vj& temp = cmP["sh"];
std::cout << temp.type <<" "<<temp.name_type<<std::endl;

return 0;
}


I would recommend getting a real C++ book (e.g. "Accelerated C++"),
and (or else) posting to a windows-specific forum when using
platform-specific libraries.

Ivan
 

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

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top