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

Will there be memory leak ?????

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.
Jul 22 '05 #1
1 1422
"Vijay" <sa****@yahoo.com> wrote in message
news:7c**************************@posting.google.c om...
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2

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

Similar topics

10
by: Debian User | last post by:
Hi, I'm trying to discover a memory leak on a program of mine. I've taken several approaches, but the leak still resists to appear. First of all, I've tried to use the garbage collector to...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
13
by: Boni | last post by:
I use 3-d party component. In this component I must pass a reference to my object. The problem is that this component has an ugly bug.When this component is disposed, it incorrectly don't delete...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
0
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. ...
9
by: Joakim Hove | last post by:
Hello, I have the following code: foo_ptr * alloc_foo(int size, ...) { /* This function allocates an instance of the foo_ptr type, and returns a pointer to the newly allocated storage. */ }
20
by: gNash | last post by:
Hi all, void main() { char *fp; fp=malloc(26); strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); fp='\0'; free(fp); }
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...

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.