Search This Blog

Monday, August 19, 2013

Stack Overrun

In this article I am trying to explain situation which causes stack overrun problem.
I was writing code for a program and was debugging the code. Everything was working properly. I was changing code very speedily. Suddenly my program stopped working due a strange CRASH stating that stack overrun. I was frustrating to find out the reason. I searched on Google but found that the only thing that causing stack overrun was recursive function call. In my case this was not a problem. Debugging also didn’t help. I got some clue because debugger was breaking at assembly code. I went through that assembly file and found that that machine code was probing for the page file. I got another clue after again debugging the code and the clue was that before entering into function the control was calling that assembly code. So here were all mess going on. This code reserve memory to store variable declared in function before entering into that function. In my function there was only one suspicious variable that was a array of bytes 10 MB long. I did this blindly but however I found the actual cause.
 So the conclusion is that don’t allocate very big amount of memory in Stack. Instead of that allocate them in Heap.
Whenever you get this kind of error do 2 things:
1.    Check you are looping in recursive function call.
2.    Check you are not allocating large memory on stack.
If you have more information on this topic please send me. I will update that here.

Sunday, September 9, 2012

Understanding Methods – A new Approch




Do you agree that a class member function cannot be called by without creating instance of that class???

If yes then think why these below lines of code is working


// ClassTest.cpp : Defines the entry point for the console application.//

#include "stdafx.h"
#include "iostream"

using namespace std;

class TestClass
{
public:

 int testFunc()
 {
  return 5;
 }

 void testFunc1()
 {
  cout << "testFunc1" << endl;
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 TestClass* pUninitialized = NULL;

 int i =  pUninitialized->testFunc();

 cout << i << endl;

 pUninitialized->testFunc1();

 return 0;
}


Here is the output

5
testFunc1
Press any key to continue . . .


Now you might wondering by observing the output. Let’s understand what is happening here
  1. First class member function does not occupy memory for each object. They get memory allocated once, and several objects call the same method. Here is the catch. Objects only gets memory allocated for their member variables not for member functions. But class member function can be called by only their object.

    Lets have a look on assembly code generated by visual studio 2010 compiler

    TestClass* pUninitialized = NULL;
    mov dword ptr [pUninitialized],0

    int i = pUninitialized->testFunc();
    // pushing "this" before function calling (NULL in our case)
    mov ecx,dword ptr [pUninitialized]
    // call to testFunc made by compiler
    call TestClass::testFunc (0D91203h)
    // getting value in var i returned from testFunc
    mov dword ptr [i],eax

    pUninitialized->testFunc1();
    // pushing "this" before function calling (NULL in our case)
    mov ecx,dword ptr [pUninitialized]
    // call to testFunc1 made by compiler
    call TestClass::testFunc1 (0D91168h)

  2. I added following code in main fucntion
    TestClass obj;

    int j = obj.testFunc();
    obj.testFunc1();

    Lets analyze its assembly code
    TestClass obj;
    lea ecx,[obj]
    // call to constructor
    call TestClass::TestClass (0D9124Eh)

    int j = obj.testFunc();
    // pushing "this" before function calling
    lea ecx,[obj]
    // call to testFunc made by compiler
    call TestClass::testFunc (0D91203h)
    mov dword ptr [j],eax
    obj.testFunc1();
    lea ecx,[obj]
    call TestClass::testFunc1 (0D91168h)

    It is clear that compiler makes the call to functions after pushing obj into stack. So function call can be made for valid instance or NULL instance.
  3. So now we understand that how it is possible to call member function without creating class instance as compiler insert code to call the function after pushing "this" in stack.


Monday, May 24, 2010

Simplest ThreadPool example Using C++

Hi guys,

I had very problem to find simple thread pooling program on net. So now I am presenting a simple example to explain ThreadPool in a very easy way. Hope you will like this.


What is ThreadPool?
Thread Pool is collection of Live, Reusable threads.

Why ThreadPool?
There are many situation where we can use ThreadPool. Consider a Client-Server application in which server has to respond to multiple client at same time. This means multitasking. Server will need a set of certain no. of threads which will do reply to client.
The big advantage of thread pool is that, it provides reusable thread. Though thread creation is very bulky process. It increases overhead. Suppose a new client send some data to server and Server has to respond. What will server do? It will create a new thread which will send data to client and then get killed or exited. Instead of creating thread on each client request we can keep collection of live thread. We will use any free thread which will send data to client. By that we will save a great overhead generated by creating threads multiple times.

ThreadPool Manager :
Hey we were talking about threadpool, now from where manager has come.
Hmmm, lets suppose I need some worker to do my work. I will go to a place(Array or Pool of threads) where I can find all workers. What I will do next? I will ask to each worker that are you free? Can you do work for me? Suppose a worst case when no workers are free then my effort for asking each worker will go waste.
Consider a person get hired to manage all information about worker like that who is free, ID of worker etc etc. I will directly go to Manager and will ask can u give me x worker for some work. Manager will check which worker is free now and if available he will assign worker to me.