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 =----