473,661 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory leak in Vector

Hi,

The below code snippet shows memory issues in Vector's push_back
method.

#include <iostream.h>
#include <vector>

class Base
{
private:
std::vector<int > baseData;
public:
void addData(int t) { baseData.push_b ack(t);}
int getData(int index) { return baseData[index]; }
};

void main()
{
Base *bObj = new Base();
bObj->addData(1);
cout << bObj->getData(0) << endl;
}

But I tried my main() as
void main()
{
Base bObj;
bObj.addData(1) ;
cout << bObj.getData(0) << endl;
}
Did not shown any memory issue in this case.

Please explain me in understanding this issue.

Note: I have used Purify Plus to identify leaks.

Thanks in advance,
Thangamani

Jul 23 '05 #1
6 4454
th************* *****@gmail.com wrote:
void main()
{
Base *bObj = new Base();
bObj->addData(1);
cout << bObj->getData(0) << endl;
}

No 'delete'? I'd say the leak is yours -- not vector's.

Take care,

John Dibling

Jul 23 '05 #2
th************* *****@gmail.com schrieb:
void main()
{
Base *bObj = new Base();
bObj->addData(1);
cout << bObj->getData(0) << endl;
}

But I tried my main() as
void main()
{
Base bObj;
bObj.addData(1) ;
cout << bObj.getData(0) << endl;
}
Did not shown any memory issue in this case.

Please explain me in understanding this issue.

Note: I have used Purify Plus to identify leaks.


You should rather use a book containing the word "delete".

T.M.
Jul 23 '05 #3
Thanks for your suggestions.

I have tried delete as in:
Base *bObj = new Base();
bObj->addData(1);
cout << bObj->getData(0) << endl;
delete bObj;

still it shows memory leak as below.

Base::addData(i nt)
private:
std::vector<int > baseData;
public:
=> void addData(int t) { baseData.push_b ack(t);}
int getData(int index) { return baseData[index]; }
};

Thangamani

Jul 23 '05 #4
th************* *****@gmail.com wrote:
Hi,

The below code snippet shows memory issues in Vector's push_back
method.

#include <iostream.h>
should be "#include <iostream>"
#include <vector>

class Base
{
private:
std::vector<int > baseData;
public:
void addData(int t) { baseData.push_b ack(t);}
int getData(int index) { return baseData[index]; }
};

void main()
This is not a valid program, and will not compile.
main should return an int.
{
Base *bObj = new Base();
bObj->addData(1);
cout << bObj->getData(0) << endl;
delete bObj;

This is not java. You don't get free garbage collection.
i.e. If you use 'new', you also need to use 'delete'.
Java lets you get away with this, but C++ doesn't.
}

But I tried my main() as
void main()
This is not a valid program, and will not compile.
main should always return an int.
{
Base bObj;
bObj.addData(1) ;
cout << bObj.getData(0) << endl;
}
Did not shown any memory issue in this case.

Please explain me in understanding this issue.

Note: I have used Purify Plus to identify leaks.


Get a decent book on C++, that talks about using 'new' and
'delete'. And not anything written by Micro$oft or Schildt,
who both believe that main should return a 'void'.

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #5
th************* *****@gmail.com wrote:
Hi,

The below code snippet shows memory issues in Vector's push_back
method.

#include <iostream.h>
// use <iostream> instead of the old/obsolete <iostream.h>.
// using the old iostream.h on some systems can cause
// all kinds of subtle problems when it is mixed with the
// the standard headers/libs
#include <iostream>
#include <vector>

class Base
{
private:
std::vector<int > baseData;
public:
void addData(int t) { baseData.push_b ack(t);}
int getData(int index) { return baseData[index]; }
};

void main()
int main()
{
Base *bObj = new Base();
bObj->addData(1);
cout << bObj->getData(0) << endl;
// replace the line above with this line
std::cout << bObj->getData(0) << std::endl;

delete bObj;
return 0; }

But I tried my main() as
void main()
int main()
{
Base bObj;
bObj.addData(1) ;
cout << bObj.getData(0) << endl;
// replace the line above with this line
std::cout << bObj.getData(0) << std::endl;

return 0; }
Did not shown any memory issue in this case.

Please explain me in understanding this issue.

Note: I have used Purify Plus to identify leaks.

Thanks in advance,
Thangamani


How did you determine there is a memory leak (i.e. what
makes you think vector leaks memory)?

What compiler (vendor and version) and Operating System
(vendor and version) are you using?

Larry
Jul 23 '05 #6
It works fine. Thanks for all your valuable suggestions.

Jul 23 '05 #7

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

Similar topics

4
1390
by: deancoo | last post by:
I'm pretty sure that the program bellow contains a memory leak. After deleting the "hand_vector" instance, not all memory is released (only about 30%). It appears to me as though the instances of "hand" are still hanging around. Are they not on the heap? Shouldn't they be released along with the "hand_vector" instance? Thanks for any help. d #include <cstdlib> #include <iostream>
4
2294
by: NewToCPP | last post by:
Is there any debugging mechanism to find out which part of the code is causing memory leak?
16
510
by: KS | last post by:
Hello, I have a memory leak in my application and I have identified two lines of code that cause the leak. If I comment out these lines, the program runs fine. If left uncommented, the memory usages keeps increasing till the program terminates due to lack of available memory. I have marked the two lines that cause this problem in the code in the function main() (at the bottom of the file).
18
9068
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the pointer, the size is not fixed. I remember, all new statement should be followed by a delete statement, is there some memory leak here?
9
4205
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It starts off at a nice 4% of memory, then slowly grows up to 50% and beyond. This translates to around 2 gigs of physical memory, and that's really way more memory than this program should be taking up.
6
1541
by: Diwa | last post by:
// ----------------------------------- class Column { public: string name; vector<int values; }; // -----------------------------------
27
2945
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do not delete a. try { a = new int ;
5
505
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store pointers of type structure "SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create an instance of structure "SAMPLE_TABLE_STRUCT" using operator "new" and push back into the vector,this is done inside a for loop for
1
238
by: bingo | last post by:
Hi, All: I'm really new to C++, so please forgive If I asked stupid questions: I was trying to use the following Vector class to do some calculations: ============================= class Vector { public:
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8758
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7364
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.