Tuesday, September 13, 2011

Weak Pointers

Recently, I was looking at using weak pointers for a project. I have the following code to test locking a shared_ptr via a weak_ptr, both when the shared_ptr is in scope and when it gets out of scope. Initial attempts at using it failed due to a compiler issue.

1:  #include <iostream>
2: #include <boost/make_shared.hpp>
3:
4: using namespace boost;
5: using namespace std;
6:
7: int main()
8: {
9: weak_ptr<int> q;
10: {
11: shared_ptr<int> p = make_shared<int>(9);
12: q = p;
13: }
14: shared_ptr<int> r = q.lock();
15: if (r)
16: cout << "r = " << *r << endl;
17:
18: return 0;
19: }

The Intel compiler issued an error: incomplete type is not allowed; VS compiler along the same lines. It was a header problem; and indeed it was.
This is strange at first though as make_shared includes declaration to shared_ptr class which in turn uses weak_ptr. But WTH, if a type is incomplete it is not declared; inclusion of the proper headers solve the problem.
3:  #include <boost/weak_ptr.hpp>  
Going back to the code, the use of a block after creation of q (line 10, 13) causes p to get out of scope before attempting to lock a shared_ptr, causing an empty r; on the other hand, commenting out the block validates q.lock(), increments p's reference count, and causes the program to display the value of r. These behaviors are expected.


Wednesday, April 13, 2011

C++ Project Porting Nightmare

Recently I had the task of porting two c++ projects written in the good old vc6 to vs2005. After making a couple of changes to the code and compiler options(Zm), compiling and linking was easy, in fact a no-brainer.
However, registration proved to be a nightmare. Registration is usually part of the post build process. But vs2005 seemed to hang in there and looked like it had no intention of proceeding. Googling suggested killing the process. Killing it caused vs to continue but this was wrong - there was a problem lurking somewhere.
Adding logs in the entrypoint did not help, as it seemed like main() was not getting called.
This was where I checked the binary using the dependency viewer in vs. DV indicated problematic dependencies. So it turned out libraries being used by this project had to be built in vs2005. Right!
So I built them libraries and, voila, projects compiled, built and got registered!
If only there was an indication of such issue provided by vs2005, it would have been easier. :)

Sunday, January 31, 2010

Using Boost Regex in XCode

I have the following configuration:
- XCode 3.2
- Boost 1.41.0
- GCC 4.2.1

Not sure if it is necessary to mention that I am developing under Snow Leopard, but there you go.
It took me awhile before I was even able to build the thing in XCode.
It appears that referencing a library is different in XCode than in the other IDEs. In XCode, I have to drag the library, libboost_regex.dylib, from the Finder window to the XCode window in the Groups and Files pane under the Products node. I do not know of any other easier way; using the Project properties window did not seem to work. Usually, as is the practice in other IDEs, the references to the library are set under the properties window.
Also, I make sure that I have copied the library under the target build directory; otherwise, the library will not be loaded at runtime. This is strange and can be wrong, though.
Now, building and running the application seem successful. However, executing regex functions (such as regex_match) crashes. See code below. What is necessary is to remove _GLIBCXX_DEBUG from the preprocessor macros. I did not remove the other one, though -- _GLIBCXX_DEBUG_PEDANTIC.

There you go...

Saturday, March 14, 2009

Building Boost Is Easy

Building the Boost library in Windows is easy. After downloading the Boost source and the appropriate bjam binary, I extracted the Boost source under c:\boost_1_38_0 and then copy the bjam binary under the same directory. The documentation points the appropriate bjam binary to download.
Once that is done, I fire up the command line, change the directory to the extraction directory, and then type in bjam. That's it, it started building the libraries without any problem. By the way, I have Visual Studio 2008 installed.

Thursday, March 12, 2009

Asynchronous IO

At last I can get my hands on the Boost ASIO library. I have been trying to for the last couple of months but could not due to work reasons.

I develop applications in C++ for the last 10 years, some of them socket applications, but mostly target Windows. I purchased a Unix book on sockets by the late Richard Stevens so I can learn how to do it in Unix. Honestly, it may have everything one needs to be proficient in sockets development; however, learning and getting familiar with a different set of APIs takes time.

So I start looking at the ASIO library. It supports most commonly used operating systems. One good thing about it is it uses IO completion ports which is a necessary requirement for server-side applications, in Windows of course, but uses its counterparts in other OSes. Read the Boost ASIO documentation for more information on what the library offers.

I will be creating a simple sockets server application that encrypts data passed to it and writes it back to the client. Of course I will be using the Boost ASIO library. Nothing much to do this time. I just want to get a feeling of what it is like to use it.

Once I am done, I will post my experience, difficulties and hopefully, joys in using the library!!!

Sunday, June 22, 2008

Creating An Immutable Object

An immutable object is an object that once gets created can no longer changed its state afterwards.
For example, to make a class immutable we can do the following:

class A
{
//1. we make its attributes private.
int x,y;
public:
//2. only create getters if needed, no setters of course
int getx() const {return x;} //same for y
public:
//3. declare an explicit constructor
explicit A(int _x, int _y) : x(_x), y(_y){}
};

We make the attributes private so derived classes cannot change the state. Of course, getters should return copies not references to the attribute.

Friday, June 20, 2008

Const Pointers, Pointers to Const, Volatile

Constant Pointers and Pointers to Const

Honestly, the first time I saw these statements, I got a bit confused.
const int * p;
int * const q;
const int * const r;

It is easy actually. The technique is to read from right to left. But note that const type  and type const are the same. So const int and int const are equivalent as well as const Object and Object const.

p is a pointer to a constant integer. This means that the following is valid:
const int xx = 10;
int xy = 11;
p = &xx; //okay
p = &xy; //okay
But p cannot be used to modify the value at the address it dereferences because it is constant.
*p = 120; //error

q is a constant pointer to an integer. This means that once initialized, q can never be set again.
q = &xx; //okay
q = &xy; //error

r is a constant pointer to a constant integer. This is a combination of the two rules above.

Volatile

The volatile keyword is used to denote that the compiler cannot make assumptions on its value and not optimize.