Posts

Showing posts from March, 2016

How to check the size of primitive data types in C++

The sizes of variables might be different depending on the compiler and the computer you are using. Following code will produce correct size of various data types on your system. #include <iostream> using namespace std; int main() {    cout << "Size of char : " << sizeof(char) << endl;    cout << "Size of int : " << sizeof(int) << endl;    cout << "Size of short int : " << sizeof(short int) << endl;    cout << "Size of long int : " << sizeof(long int) << endl;    cout << "Size of float : " << sizeof(float) << endl;    cout << "Size of double : " << sizeof(double) << endl;    cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;    return 0; } Following is the sample result. Results may be differ according to your system. Size of char : 1 Size of int : 4 Size of

Vector container in C++ STL

Following code demonstrates the vector container (a C++ Standard Template) in C++. #include <iostream> #include <vector> using namespace std; int main() {    // create a vector to store int    vector<int> vec;    int i;    // display the original size of vec    cout << "vector size = " << vec.size() << endl;    // push 5 values into the vector    for(i = 0; i < 5; i++){       vec.push_back(i);    }    // display extended size of vec    cout << "extended vector size = " << vec.size() << endl;    // access 5 values from the vector    for(i = 0; i < 5; i++){       cout << "value of vec [" << i << "] = " << vec[i] << endl;    }    // use iterator to access the values    vector<int>::iterator v = vec.begin();    while( v != vec.end()) {       cout << "value of v = " << *v << endl;       v++;    }

Enable Strict Mode in Android.

StrictMode works on a set of policies. There are presently two categories of policies: VM policies and thread policies. The former represent bad coding practices that pertain to your entire application, notably leaking SQLite Cursor objects and kin. The latter represent things that are bad when performed on the main application thread, notably flash I/O and network I/O. The simplest thing to do is call the static enableDefaults() method on StrictMode from onCreate() of your first activity. import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; public class FilesDemo extends Activity {     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         if (BuildConfig.DEBUG                 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {                        StrictMode.setThreadPolicy(buildPolicy())