Tag Archives: linux

Delete Dynamically Allocated Memory by Ctrl-C

Intro

In this post, I will share an example C/C++ code demonstrating a method to safely delete dynamically allocated memory when a user terminates the application by Ctrl-C.

Problem

First let’s review a code which includes a problem. The code dynamically allocate a memory for the variable a. The code has delete[] function, but the application cannot arrive there due to the infinite loop which is very common in may applications. If a user terminates the application with Ctrl-C, then the memory will be leaked.

#include <iostream>

int main(){
    signal(SIGINT, myInterruptHandler);

    double* a;
    a = new double [1000];

    std::cout << "press ctrl-c\n" ;
    while(1){  }

    delete[] a;
}

The figure below shows the execution results.

As you can see the, the free memory continuously is reduced every time after the termination of the application by Ctrl-C.

Solution by Using Signal

In Linux system, if a user press Ctrl-C, a signal is sent to the process from the OS. We can catch the signal and make a different routine. One example is as below:

#include <iostream>
#include <signal.h>

uint8_t loop_flag = 1;

void myInterruptHandler (int signum) {
    loop_flag = 0;
}

int main(){
    signal(SIGINT, myInterruptHandler);

    double* a;
    a = new double [1000];

    std::cout << "press ctrl-c\n" ;
    while(loop_flag){

    }

    delete[] a;
    std::cout << "\nelegantly terminated wihout memory leakage\n";
}

The result of the code above is shown in the figure below:

In contrast to the previous result, the memory is not leaked and elegantly terminated.

Download/Build of Source Code

You can download the source code at this link.

More topics to read

There is another function called “sigaction()”. It seems more robust and well -defined. But it seems to need more lines of codes. Thus, I decided to use signal. For the details, please read this post .

Reference

libstdc++.so.6 error when using cross compiler in debian (e.g. ubuntu)

When I compile a source code for Beaglebone black (arm processor) in my ubuntu 12.04 local computer, I met the following problem.

error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory

The solution is !!

Copy the below text

” deb http://www.emdebian.org/debian unstable main ”

and, go to the package source folder.  /etc/apt/sources.list.d

Then, paste the text in a source list file (e.g, lubuntu-desktop-ppa-precise.list)

Then update or install all necessary files like this.

# apt-get install build-essential
# apt-get install libc6-armhf-cross
# apt-get install libc6-dev-armhf-cross
# apt-get install binutils-arm-linux-gnueabihf
# apt-get install linux-libc-dev-armhf-cross
# apt-get install libstdc++6-armhf-cross
# apt-get install gcc-4.7-arm-linux-gnueabihf
# apt-get install g++-4.7-arm-linux-gnueabihf

 

I got this solution from a reply of this website.

http://derekmolloy.ie/beaglebone/setting-up-eclipse-on-the-beaglebone-for-c-development/

I really appreciate.

I hope this can help your project.

 

-Mok-

Terminator installation and setting as a default terminal program

I strongly recommend to use Terminator as a default terminal program in Linux.

It has many great functions such as horizontal/vertical tab split.

First. Installation.

$ sudo apt-get install terminator

Second, Setting as a default terminal program

gconftool --type string --set /desktop/gnome/applications/terminal/exec terminator

It is done!!!

Good luck

Very good source code for serial communication in QT Linux envirionment

< Source code for serial communication in QT Linux envirionment>

The below site has pretty good source code for serial communication in Linux QT environment.

You can download and learn how to use it.

http://www.embedded4fun.com/serial-port-interfacing-in-qt/

 

The below is how to install the code.  ( from the upper site)

  • cd <any folder (preferrably to the QtSDK installation folder)>
  • git clone https://code.google.com/p/qextserialport/
  • cd qextserialport
  • qmake
  • make
  • make install

<download>