lundi 7 avril 2014

Class/Struct definition local to a function.


You may know or not that defining a class or struct inside a function is possible but you may not be aware of all the issue you will meet when using that with the old C++98.
First thing, if you are using C++11 you can pass away, because in all case that feature will be correctly handle by recent compiler and debugger.
Now if your are still using a C++98 compiler or an old GCC, stay here and read the following:)

Definition in a function

In most case the following code will be Ok.

int function() { 
   typedef struct myStruct { 
       int num; 
       myStruct (int n): num(n){} 
       myStruct(const myStruct& ro):num(ro.num){} 
    } mystruct_t; 

  mystruct_t t(2);

  return t.num;
} 


But want will happened if you try to use that struct with an STL’s container, like vector, deque, etc….. Example

int function() { 
   typedef struct myStruct { 
       int num; 
       myStruct (int n): num(n){} 
       myStruct(const myStruct& ro):num(ro.num){} 
    } mystruct_t; 

  std::deque<mystruct_t> dq(10, mystruct_t(2));
  
   return dq[0].num;
} 

In fact here we have 2 different scenario:
  1. you are using GCC and it will not compile, because it’s not a C++98 compliant code
  2. you are using VC++2008 and it will compile, but you will meet weird debugger behavior on that piece of code.

Explanation

In C++98, you cannot use local class or struct with template type. This restriction is part of the standard. The reasoning was originally that local structures don't really have a names and thus it would be impossible to instantiate a template with them.

So as said if VC++2008 allow that code to compile, you will not be able to see what you have in your deque… Like in the snapshot below.

DebuggerError

My conclusion

Try to avoid that kind of code until you get the new C++11, because the portability across platform is not guarantee by the standard and that debugging and maintaining them is really hard. Define your class or struct outside of the function.

jeudi 13 mars 2014

Step-by-step through C++ Template

As some of my teammate always complain that template-based code are really hard to understand and that they would prefer coding without, I will try here to explain how it works and propose some complexity-growing example.

NOTE1: that post will be periodically update....
NOTE2: that post/tutorial is design in a way to can do all exercise without local compiler, I wrote a solution using Ideone, so create your account and write your own solution for all exercise.

A first look at the template syntax <> and their usage:

We using template and/or writing template you "can" meet the < CONTENT > syntax
Where Content can be:
  • class T / typename T
  • A data type, which maps to T
  • An integral specification
  • An integral constant/pointer/reference which maps to specification mentioned above.

1st Example

Imagine we want a function to multiply by 2 an integer

int mulByTwo(int v) { return v*2;}

And we can write int I = mulByTwo(2);

Now we also want mulByTwo but for float and we avoid code duplication.
Question: Write only one function MulByTwo to deal with:

int I = mulByTwo(2);
double D = mulByTwo(2.0);

Solution: here

Observe basic/simple "type deduction" error message

Now using the 1st example, what's happen if you call:
string S = mulByTwo("blahblah....");

Now build a function Sum, range-based to deal with iterator and pointer of any kind !

Tips: an iterator is just a simple base class implementing some operator like: ++,--,+=,-=,*, ->, [], etc...

Add the end with only 1 function, you should be able to execute:

int main() {
    int testArray[5] = {0,1,2,3,4};
    vector v = {0, 1, 2, 3, 4};
    vector s = {"hello", " ", "world"};
  
    cout << Sum(v.begin(), v.end()), cout << endl;
    cout << Sum(testArray, testArray + sizeof(testArray)/sizeof(testArray[0])), cout << endl;
    cout << Sum(s.begin(), s.end()), cout << endl;
  
    return 0;
}


Solution: here

How to use integral template

as for type deduction, the compiler will prepare everything at the compilation time.

Example: template&ltint&gt struct example { static double value = N * 2.0; }
If we write: cout << example&lt5&gt::value;

Exercise: Use that to compute factoriel&lt5&gt at compile time
Solution: here.

------ > followings section are under construction <------------------------ br="">

Solving ambiguity / Explicit Template Arg Specification

 

Default template  argument


Template and Specialization




vendredi 7 février 2014

char ** conversion to string

First, I would say it was a long time without posting anything, but I’m a bit in trouble in my personal life. And finally today as I was working on a command line implementation I realize that it’s a good practice to log (wherever you want …) the command line argument used to run a command line executable.

And as I was writing the code to do that, I was thinking that some developer may have create some complex code with loop display the command line argument that we have in argv. But in fact if you can use C++ STL and also the powerful BOOST Framework, it use only 2 lines of code (or even less).

So let start, first cmd line arguments are from the main function point of view an array of char*. So the 1st thing to do convert that in the C++ world.

vector<string> cmdline(argv+1, argv+argc);

Ok now we have STL object we all know on which we can easily iterate to concatenate each string and insert a delimiter (a simple space “ ”). But instead of using a complex loop I prefer using an efficient BOOST string algorithm call join.

#include "boost/algorithm/string.hpp"

….

string cl = boost::algorithm::join(cmdline, " ")

now you can dump the argument passed to your command line with a cout or any logging system you want.

And have a nice weekend.

vendredi 27 décembre 2013

C++: How to handle exception in CTOR

Yesterday I was reading that thread on Linkedin, and I realized once again that the exception handling in/or around a constructor(CTOR) is still an obscure topic as only one commenter mentioned the correct try-catch form for CTOR that can handle every exception even those coming from the initialization list.
The main problem when an exception occur during a CTOR is what have we to do for cleaning memory allocated on the heap.
To summarize the easiest way to handle exception, I produced the following example.

The output will be (I added some comments:
  • object_t CTOR ==> object_t allocation in our initializer list put in a smart_ptr (1)
  •  test_t CTOR ==> here we enter in the test_t CTOR Impl
  • object_t CTOR ==> we allocated a new object (2)
  • … here we have thrown the exception
  • object_t DTOR ==> the smart pointer object is on the stack, so the stack unwinding run and the memory allocation (1) is cleaned
  • here we handle our dynamic allocation ==> we are now in the CTOR catch section, as we have correctly ordered our data member we can check `if (ptr != nullptr)` and delete it if needed.
  • object_t DTOR ==> now the memory allocation (2) is released.
  • exception just to test... ==> as we have and should always RETHROW the exception, we catch it!

Conclusion: If we based all our class on that model, there is no reason anymore to fear about memory leaks.

mardi 24 décembre 2013

Use Beyond Compare as SVN diff tool under Linux

Today a quick note on that topic as I extended my own configuration with that tricks. BeyondCompare is a well known tool to compare and merge different  version file.

I used it a lot combined with ToirtoiseSVN, but as I worked more and more under linux using Remote Workstation, I would also use it. But I mainly work on remote workstation using a putty prompt. I will not describe here how to setup bcompare on a linux workstation, but it’s the 1st thing todo.

Next you have to follow my previous post to redirect the all X application to your local (I mean Windows 7 in my case) X11 server.

Now when you run “svn diff” on a file, you expect to use bcompare. To achieve that goal, follow me step by step.

cd ~

touch .subversion/bcompare_svn.sh

nano .subversion/bcompare_svn.sh

COPY/PASTE the folllowing in that script:

#!/bin/bash

/usr/bin/bcompare $6 $7 &

exit 0

Set execution permission

chmod 755 .subversion/bcompare_svn.sh

nano .subversion/config

Add that line just after “# diff-cmd = diff_program (diff, gdiff, etc.)”


diff-cmd=/home/YOURFOLDER/.subversion/bcompare_svn.sh

And done !

Now at each svn diff  command you start, BeyondCompare will start and display on the own PC.

vendredi 13 décembre 2013

STL String to/from wide string

Yesterday I spend sometimes to help one of my teammate with a piece of code he wrote to convert a std::string  into a std::wstring. At the beginning it seems to be a complex issue for him and he used a complex C based code managing wchar buffer convert char to wchar with mbstowcs.

But finally as often the STL provide everything we need to convert one string type into the other and it’s quite simple.

I quickly use Ideone to demonstrate how to deal with:

As you see we can just use the iterator (old-style or new style like below)

wstring ws(s.begin(), s.end()); or wstring ws(begin(s), end(s));

lundi 2 décembre 2013

Redirect X from remote linux machine on your windows host.

When you use a remote linux machine from your own Windows Desktop machine using a putty connection, you can easily redirect X application on your desktop.

To do that, there is several steps:

1) You need to have a cygwin setup on your windows host, and follow the following instruction to setup all package required to run a X server.

2) Setup your putty connection to allow X11 forwarding.

Putty-Enable-X11Forwarding

To check if everything is OK, restart a connection and from linux prompt, check the value of “DISPLAY” (echo $DISPLAY). It should return something similar to:

localhost:13.0

3) To start your windows X11 server, create a shortcut file in your cygwin folder.

the target will be: C:\ cygwin\bin\run.exe /usr/bin/bash.exe -l -c /bin/startxwin.exe

And for its name, “StartX” is a good choice!

Double-click on that shortcut and check that you now have the X11-server icon in your Windows systray.

4) To run that server every time you start your window session, save a copy of that shortcut in:

%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

5) Final check, start xclock from your Linux prompt, the xclock windows will appear on your Windows Desktop !