Tuesday, September 12, 2017

Difference between *p++, *++p and ++*p

One of the most common question is which version of *p++, *++p or *p++ to use while programming.

There is no right answer and the answer varies from the usage point of view. Understanding the output of each form would help you in deciding which one should be used.

There are two simple rules to remember related to postfix, prefix and de-reference operator.

1. Precedence of postfix is higher than both prefix and de-reference operator. Associativity of postfix operator is left to right.

2. Precedence of prefix and de-reference operator is same. Associativity is right to left.

Let's try to understand the results based on these rules by below examples -

int a[] = {5, 10};
int *p = a;

*p++
Result - 10
The precedence of p++ is higher so pointer is incremented first and then de-referenced.

*++p
Result - 10
The associativity is right to left so pointer is incremented first and then de-referenced.

++*p
Result -6
The associativity is right to left so pointer is de-referenced first then incremented.

Please leave your comments with suggestions/feedback.

Thursday, April 27, 2017

memcpy vs strcpy

The functions which begins with "mem" prefix also operate on characters but, they do not use the null terminator convention.

This is the reason they will always need the size of characters to operate on.

For example, when using memcpy it requires size as well, though, strcpy only needs dest and src pointers. It can automatically detect end of string using null character.

void *memcpy(void *dest, const void *src, size_t n);
 
char *strcpy(char *dest, const char *src); 

Monday, April 24, 2017

What is Smart Pointer

Smart pointer is a class which wraps raw C++ pointers with some additional functionality, e.g. automatic memory de-allocation, reference counting etc.

Smart pointers should be preferred over raw pointers because it could help in overcoming some limitation of raw pointers such as memory leak, dangling pointers etc.

Saturday, January 25, 2014

Count number of folders/files in a directory

The "dir" command is used for listing the content of a directory in Windows.

To count number of folders in a directory -

dir /b/ad | find /v /c "::"

To count only files in a directory (excludes folders) -

dir /b/a-d | find /v /c "::"

Saturday, October 27, 2012

Wifi not working on ubuntu 12.04

I upgraded my kubuntu 10 to 12.04 LTS. After the upgrade, wifi did not work. I could connect to internet using wired network.

My problem was resolved after I installed the system updates.

However, I also found one more solution (did not try though) which can be found here.

Also, do not forget to remove the already stored wireless connection and add it again.

Wednesday, October 19, 2011

No shutdown option in kubuntu

Since, I upgraded to Kubuntu version 11.10, I could not see shutdown option. However, following fix works very well and shutdown option is shown. sudo dpkg-reconfigure kdm Chose kdm from the list, then log out, reboot, and now you have the shutdown option on the Leave menu.

Tuesday, August 2, 2011

Disable compilation warning using pragma

Compiler warning can be disabled for a part of the code using #pragma.

#pragma warning(disable:4700)
//your code here
.
.
#pragma warning(default:4700) // restores the default behavior.