473,508 Members | 2,357 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_back(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 4449
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(int)
private:
std::vector<int> baseData;
public:
=> void addData(int t) { baseData.push_back(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_back(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_back(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
1385
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...
4
2281
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...
18
9053
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...
9
4198
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...
6
1527
by: Diwa | last post by:
// ----------------------------------- class Column { public: string name; vector<int values; }; // -----------------------------------
27
2919
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...
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...
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...
0
7120
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...
0
7323
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
7380
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
7494
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
5626
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,...
1
5050
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...
0
4706
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...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
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 ...

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.