Wednesday, February 17, 2010

Core dumping of runtime allocated memory

Hello Folks , today we are going to demonstrate a simple application , which can generate the core dump of the memory allocated to objects at runtime.

Below is illustrated a simple c++ program , along with a trial run of the output try it and a fair analysis yields the fact about allocation of memory to the objects at runtime .

As an enhancement  , we can compile the program twice into 2 object files , and then run them simultaneously on two terminals , with considerable delay to affix the illustrated concept .

// The program is to be tested in any Linux environment
// We have tested the program in Fedora core 12

#define MARKERSIZE 5
#define BEGMARKER "" set it to
#define ENDMARKER "
" set it to
#include
#include
#include
#include
#include
#include


using namespace std;

class A
{
    char beginA[MARKERSIZE];
    int i;
    char b;
    char endA[MARKERSIZE];

public:
    A() : i(0xabcd), b('X')
    {
        std::cout << "CTOR" << std::endl;
        strcpy(beginA,BEGMARKER);
        strcpy(endA,ENDMARKER);
    }

    ~A()
    {
        std::cout << "DTOR" << std::endl;
    }

    void displayme()
    {
        printf("ADDR BEG = %p\n",beginA);
        printf("ADDR I = %p\n", &i);
        printf("ADDR B = %p\n", &b);
        printf("ADDR END = %p\n",endA);
    }
};


int main()
{
    for(int i=0 ; i<10 ; ++i)
    {
        A *a = new A();
        a->displayme();
        //delete a; remove this comment in the second run .
    }
        
       //send a SIGSEGV signal to generate core
    pid_t pid = getpid();
    kill(pid,11);
}

Trial Run :

ADDR BEG = 0x8fca008
ADDR I = 0x8fca010
ADDR B = 0x8fca014
ADDR END = 0x8fca015
CTOR
ADDR BEG = 0x8fca020
ADDR I = 0x8fca028
ADDR B = 0x8fca02c
ADDR END = 0x8fca02d
CTOR
ADDR BEG = 0x8fca038
ADDR I = 0x8fca040
ADDR B = 0x8fca044
ADDR END = 0x8fca045
CTOR
ADDR BEG = 0x8fca050
ADDR I = 0x8fca058
ADDR B = 0x8fca05c
ADDR END = 0x8fca05d
CTOR
ADDR BEG = 0x8fca068
ADDR I = 0x8fca070
ADDR B = 0x8fca074
ADDR END = 0x8fca075
CTOR
ADDR BEG = 0x8fca080
ADDR I = 0x8fca088
ADDR B = 0x8fca08c
ADDR END = 0x8fca08d
CTOR
ADDR BEG = 0x8fca098
ADDR I = 0x8fca0a0
ADDR B = 0x8fca0a4
ADDR END = 0x8fca0a5
CTOR
ADDR BEG = 0x8fca0b0
ADDR I = 0x8fca0b8
ADDR B = 0x8fca0bc
ADDR END = 0x8fca0bd
CTOR
ADDR BEG = 0x8fca0c8
ADDR I = 0x8fca0d0
ADDR B = 0x8fca0d4
ADDR END = 0x8fca0d5
CTOR
ADDR BEG = 0x8fca0e0
ADDR I = 0x8fca0e8
ADDR B = 0x8fca0ec
ADDR END = 0x8fca0ed
ADDR BEG = 0x8fca008
ADDR I = 0x8fca010
ADDR B = 0x8fca014
ADDR END = 0x8fca015
CTOR
ADDR BEG = 0x8fca020
ADDR I = 0x8fca028
ADDR B = 0x8fca02c
ADDR END = 0x8fca02d
CTOR
ADDR BEG = 0x8fca038
ADDR I = 0x8fca040
ADDR B = 0x8fca044
ADDR END = 0x8fca045
CTOR
ADDR BEG = 0x8fca050
ADDR I = 0x8fca058
ADDR B = 0x8fca05c
ADDR END = 0x8fca05d
CTOR
ADDR BEG = 0x8fca068
ADDR I = 0x8fca070
ADDR B = 0x8fca074
ADDR END = 0x8fca075
CTOR
ADDR BEG = 0x8fca080
ADDR I = 0x8fca088
ADDR B = 0x8fca08c
ADDR END = 0x8fca08d
CTOR
ADDR BEG = 0x8fca098
ADDR I = 0x8fca0a0
ADDR B = 0x8fca0a4
ADDR END = 0x8fca0a5
CTOR
ADDR BEG = 0x8fca0b0
ADDR I = 0x8fca0b8
ADDR B = 0x8fca0bc
ADDR END = 0x8fca0bd
CTOR
ADDR BEG = 0x8fca0c8
ADDR I = 0x8fca0d0
ADDR B = 0x8fca0d4
ADDR END = 0x8fca0d5
CTOR
ADDR BEG = 0x8fca0e0
ADDR I = 0x8fca0e8
ADDR B = 0x8fca0ec
ADDR END = 0x8fca0ed

A prerequisite b4 we run the program ,
run the following command:

[]#ulimit -l unlimited

to set the core dump size to unlimited .

Original Article at :http://cprogrammers.blogspot.com/2009/02/c-heap-marker-to-detect-memory-leaks.html

Authored by :

Jayanthi GM ( Course Instructor for Java ,PESIT )
Prashanth Raghu ( Student Assitant )

No comments:

Post a Comment