Wednesday, 31 March 2010

Deleting of Vectors content to avoid memory leaks

Its interesting how deleting of vectors can sometimes be a problem as people tend to make simple mistakes that can cause crash and waste useful hours to debug.

The following code shows my approach to deleting of vectors. I am sure there are better approaches. In case you know one please share.




//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Program to demonstrate a simple way to delete vector and memory
//associated with it.

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include<iostream>
#include <crtdbg.h>
#include<vector>

#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

using namespace
std;

int
main()
{

_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF _CRTDBG_LEAK_CHECK_DF );
vector<int *> someVector;
for
(int i = 0; i < 5; i++)
{

int
* x = new int(i * 15);
someVector.push_back(x);
}


//now we need to delete the elements of the vector else it will generate memory leaks
//This is one simple and safe approach
while (!someVector.empty())
{

vector<int *>::iterator it = someVector.begin();
if
(*it) //Additional safety in case a NULL pointer was stored
delete (*it); //because *it = *int - Only the contents are deleted
someVector.erase(it);
}


return
0;
}





You can check my old post on how to see if memory leaks are there. This program shows another approach but the results are the same

No output needed.

You can read more about this approach of detecting memory leaks on the MSDN site here.

Wednesday, 24 March 2010

Case-Insensitive String comparison

It is often a problem that we ask someone to input some string for comparison but they often put different case and the comparison fails. For example if we are expecting a string, say, 'true'. The user may input 'True' or 'TRUE'. A simple comparison will fail in this case.

The following is a simple program to convert the input string to lower case. What this would allow is to do a case-insensitive search.



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>
#include<string>

using namespace
std;

//First approach for converting into lower case
string toLowerString(string& input)
{

int
length = input.length();
string output;
for
(int i = 0; i < length; i++)
{

output += tolower(input[i]);
}

return
output;
}


//Second approach for converting into lower case
string toLowerString(string& input, bool approach2) //Overloaded method
{
int
length = input.length();
string output;
for
(int i = 0; i < length; i++)
{

if
(input[i] >= 0x41 && input[i] <= 0x5A) //Ascii for A-Z
output += (input[i] + 0x20); //ascii for a-z
else
output += input[i];
}

return
output;
}


int
main()
{

string s1="true";
string s2("TRUE");
string s3;
s3 = "True";

//Check if s1 = s2
if(s1 == s2)
cout<<"s1 == s2"<<endl;
else

cout<<"s1 != s2"<<endl;

//Check if s1 = s2 with first approach
if(s1 == toLowerString(s2))
cout<<"s1 == Approach1::toLowerString(s2)"<<endl;
else

cout<<"s1 != Approach1::toLowerString(s2)"<<endl;

//Check if s1 = s2 with second approach
if(s1 == toLowerString(s2,true))
cout<<"s1 == Approach2::toLowerString(s2)"<<endl;
else

cout<<"s1 != Approach2::toLowerString(s2)"<<endl;


//Check if s1 = s3 with second approach
if(s1 == s3)
cout<<"s1 == s3"<<endl;
else

cout<<"s1 != s3"<<endl;

if
(s1 == toLowerString(s3,true))
cout<<"s1 == Approach2::toLowerString(s3)"<<endl;
else

cout<<"s1 != Approach2::toLowerString(s3)"<<endl;

//Check if s1 = s3 with second approach
if(s1 == toLowerString(s3))
cout<<"s1 == Approach1::toLowerString(s3)"<<endl;
else

cout<<"s1 != Approach1::toLowerString(s3)"<<endl;

return
0;
}





The output is as follows:


I am aure much better approaches are possible. If you have a better example please share.

Wednesday, 17 March 2010

Example of Stringstreams

Here is an example of stringstream that I picked up from here and modified it slightly.




//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Simple example of C++ stringstreams

#include<iostream>
#include<sstream>
#include<string>

using namespace
std;

int
main()
{

string input;

cout<< "Please enter some numbers speretaed by space and press enter once done: ";
getline(cin, input);

cout<< "input = " << input << endl;

stringstream memString(input, ios_base::in); //stringstream( string s, openmode mode )

cout<<"memString = " << memString << endl;
cout<<"memString (contents) = " << memString.str() << endl;

double
summ = 0;
double
temp;
while
(memString >> temp)
{

summ+= temp;
}


cout<<"summ = " << summ << endl;

return
0;
}








The output is as follows:


See Also:
C++ Reference on stringstreams
Using Stringstreams in C++
String Stream example
String Streams Constructors

Wednesday, 10 March 2010

Fixed form, Scientific form and Precision setting for floating point numbers

Sometimes we want to output the floating point numbers in fixed or scientific form and sometimes with fixed precision so as to align all the output. The following program is an example to demonstrate that:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This example shows how to output floating point numbersin fixed and
#include<iostream>

using namespace
std;

int
main()
{

float
number = 123.456;

cout << "number in fixed form = " << number << endl; //default is fixed
cout << "number in scientific form = " << scientific << number << endl;
cout.precision(2);
cout << "number in fixed form with precision 2 = " << fixed << number << endl; //here the format was scientific
cout.precision(3);
cout << "number in fixed form with precision 3 = " << number << endl;
cout.precision(4);
cout << "number in fixed form with precision 4 = " << number << endl;
cout.precision(5);
cout << "number in fixed form with precision 5 = " << number << endl;

return
0;
}






The output is as follows:

Wednesday, 3 March 2010

Printing out leading 0's (or any other charachters)

This is a simple example to print out leading zeroes or any charachters when printing out numbers. The main manipulator function needed is the set field width that will set the width of the field.



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This example shows how to print leading 0's in the output

#include<iostream>
#include<iomanip>

using namespace
std;

int
main()
{

int
number = 123;

cout<< "number = "<< number << endl;

cout<< "number = "<< setw(2) << setfill('0') << number << endl;
cout<< "number = "<< setw(3) << setfill('0') << number << endl;
cout<< "number = "<< setw(4) << setfill('0') << number << endl;
cout<< "number = "<< setw(5) << setfill('0') << number << endl;
cout<< "number = "<< setw(10) << setfill('0') << number << endl;

cout<< endl;
cout<< "number = "<< setw(5) << setfill('x') << number << endl;

cout<< endl;
number = 0;
cout<< "number = "<< setw(5) << setfill('x') << number << endl;

return
0;
}







The output is as follows: