[ROS/C++] Using smart pointers in containers

2014年8月17日 17:35

Problem Description:

The memory leak problem was found when I tested the ROS simulation of a large number of robots, say, 50. Basically I have one simulator node and 50 robot controller nodes. Occasionally after running the program for a while, the simulator node may throw a bad_alloc exception and then terminated.

Tags: c++ ROS
评论(49) 阅读(5913)

CMake File Example From Official Guide

2013年3月25日 08:48

The following example demonstrates some key ideas of CMake. Make sure that you have CMake installed prior to running this example (go here for instructions).

There are three directories involved. The top level directory has two subdirectories called ./Demo and ./Hello. In the directory ./Hello, a library is built. In the directory ./Demo, an executable is built by linking to the library. A total of three CMakeList.txt files are created: one for each directory.

The first, top-level directory contains the following CMakeLists.txt file.

# The name of our project is "HELLO". CMakeLists files in this project can
# refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and
# to the root binary directory of the project as ${HELLO_BINARY_DIR}.
cmake_minimum_required (VERSION 2.6)
project (HELLO)

# Recurse into the "Hello" and "Demo" subdirectories. This does not actually 
# cause another cmake executable to run. The same process will walk through 
# the project's entire directory structure. 
add_subdirectory (Hello)
add_subdirectory (Demo)

Then for each subdirectory specified, CMakeLists.txt files are created. In the ./Hello directory, the following CMakeLists.txt file is created:

# Create a library called "Hello" which includes the source file "hello.cxx"
# The extension is already found. Any number of sources could be listed here. 
add_library (Hello hello.cxx)

Finally, in the ./Demo directory, the third and final CMakeLists.txt file is created:

# Make sure the compiler can find include files from our Hello library. 
include_directories (${HELLO_SOURCE_DIR}/Hello) 

# Make sure the linker can find the Hello library once it is built. 
link_directories (${HELLO_BINARY_DIR}/Hello) 

# Add executable called "helloDemo" that is built from the source files 
# "demo.cxx" and "demo_b.cxx". The extensions are automatically found. 
add_executable (helloDemo demo.cxx demo_b.cxx) 

# Link the executable to the Hello library. 
target_link_libraries (helloDemo Hello) 

CMake when executed in the top-level directory will process the CMakeLists.txt file and then descend into the listed subdirectories. Variables, include paths, library paths, etc. are inherited. Depending on the system, makefiles (Unix) or workspaces/projects (MSVC) will be built. These can then be used in the usual way to build the code.

FROM:

http://www.cmake.org/cmake/help/examples.html

REFERENCE:

http://www.cnblogs.com/coderfenghc/archive/2012/06/16/CMake_ch_01.html#2625260

评论(7) 阅读(3923)

Using OpenCV with gcc and CMake

2013年3月25日 08:46

Create a program using OpenCV

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

int main( int argc, char** argv )
{
  Mat image;
  image = imread( argv[1], 1 );

  if( argc != 2 || !image.data )
    {
      printf( "No image data \n" );
      return -1;
    }

  namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
  imshow( "Display Image", image );

  waitKey(0);

  return 0;
}

Create a CMake file

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

Generate the executable<

cd <DisplayImage_directory>
cmake .
make

From http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

评论(5) 阅读(3299)

c++文件操作——输入文件包含矩阵形式的数据,输出为vector

2013年2月19日 14:20

方法一:来自网络,推荐此法,直接使用标准库。简洁好用

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
typedef vector<vector<int> > Mat;

Mat input();
int main (void)
{
       Mat a = input();
       for (int i = 0; i < a.size();i++)
      {
            for(int j = 0; j < a[i].size();j++)
           {
                 cout<<a[i][j]<<" "<<flush;
           }
           cout<<endl;
      }
     return 0;
}

Mat input()
{
       ifstream in("int.txt");
       Mat a;
       istringstream istr;
       string str;
       vector<int> tmpvec;
       while(getline(in,str))
      {
           istr.str(str);
           int tmp;
           while(istr>>tmp)
          {
               tmpvec.push_back(tmp);
          }
         a.push_back(tmpvec);
         tmpvec.clear();
         istr.clear();
      }
    in.close();
   return a;
}

方法二见原链接:http://hi.baidu.com/walanlixuming/item/63324ae80cdeccaece2d4ff4

稍作改动,写成两个函数如下方便调用:

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <fstream>
#include <stdlib.h>
//#include <boost/algorithm/string.hpp>

using namespace std;
//using namespace boost::algorithm;
typedef vector<vector<double> > Mat;

/* fileName should be of string type */
Mat inputMat(string & fileName){
  ifstream infile;
  infile.open(fileName.c_str());
  Mat a;
  istringstream istr;
  string str;
  vector<double> tempvec;
  while(getline(infile, str)){
    istr.str(str);
    double tmp;
    while(istr>>tmp){
      tempvec.push_back(tmp);
    }
    a.push_back(tempvec);
    tempvec.clear();
    istr.clear();
  }
  infile.close();
  return a;
}

void showMat(Mat M){
  for(int i=0; i<int(M.size()); i++){
    for(int j=0; j<int(M.at(i).size()); j++){
      cout<<M.at(i).at(j)<<" "<<flush;
    }
    cout<<endl;
  }
  
}

评论(7) 阅读(5007)

Best Ways to Trim a string in C++ [stackoverflow]

2013年2月19日 05:48

1. Using boost::algorithm

#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost::algorithm;

string str1(" hello world! ");
trim(str1);

2. Define inline function:

#include <algorithm> 
#include <functional> 
#include <cctype>
#include <locale>

// trim from start
static inline std::string <rim(std::string &s) {
        s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
        return s;
}

// trim from end
static inline std::string &rtrim(std::string &s) {
        s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
        return s;
}

// trim from both ends
static inline std::string &trim(std::string &s) {
        return ltrim(rtrim(s));
}

http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring

评论(8) 阅读(3840)