Wednesday, July 25, 2012

Double Pointer (Pointer to Pointer) some input


 A double pointer has two basic meanings.
        One is of a pointer to a pointer, where changing the value of double pointer will result in the original pointer being changed.
        Another is that of a two-dimensional array, such as a matrix

Example for Two dimensional array
int main( int argc, char **argv (or) char* argv[] )
{
// print the first string
printf("%s\n", argv[0]);

return 0;
}

The other example where the double pointer is a pointer to a pointer, lets say you have a function foo() that allocates memory for a pointer that was declared in function main()

void foo( char ** ptr)
{
*ptr = malloc(255); // allocate some memory
strcpy( *ptr, "Hello World");
}

int main()
{
char *ptr = 0;
// call function with a pointer to pointer
foo( &ptr );
printf("%s\n", ptr);
// free up the memory
free(ptr);

return 0;
}

Creating 2D int array
//Create your pointer
int **ptr;
//Assign first dimension
ptr = new int*[5];
//Assign second dimension
for(int i = 0; i < 5; i++)
ptr[i] = new int[5];
Deleting 2D array
for (int x = 0; x < nCol; ++x)
delete [] ptr[x];
delete [] ptr;

How delete[] knows how many items to be destroyed ??
 When an array is created with new[], the size of array is stored in the metadata at the memory block. delete[] makes use of that information.

New[] operator requires default constructor  , for ex
   student* kar = new student[10](34,"");  // its completely wrong
  student * kar = new student[10];   // Correct

new & delete are based on malloc & free. Malloc additionally allocate a header storing the size allocated, so free() knows the size to free. new[] not only keeps the malloc header, but also has an extra header storing the size of the array/vector, so delete[] knows how many objects are in the array/vector, and thus knows how many times to call the destructor.


Allocation Using
Deallocation using
Result
Remarks
new
delete
Ok

new[]
delete[]
Ok

new
delete[]
The vector delete will try to delete more objects depending on the random value it gets from its object-count location and will obviously result in heap corruption.
Compiler will not track the Pointer, Its developer responsibility to call the respective delete operator (ie delete or delete[])
new[]
delete
Memory leak
It will delete only first item in the array

Monday, July 23, 2012

JNI step by step


1. Add native method in your activity class
    for instance : public native String display(boolean status);

2. open command prompt and go to bin/classes
    Example :~/workspace/test/bin/classes$ javah -jni com.example.test.MainActivity
    Abstract :~/workspace/test/bin/classes$ javah -jni <class name where u have added your native API>

    as a result , a header file will be created in   "test/bin/classes/"
3. Copy that header file and paste it in to jni folder (create jni folder in the project )

4. Using signature of the header , create the cpp file and have definition for that method

5. Create Android.mk file (add the cpp file and module name inside it)

6. go to the project
    Example:~/workspace/test$ "ndk-build.cmd"
    result : this will create lib (under the libs folder)

7. now, you can call the native method from the application

Saturday, June 23, 2012

(Anything)^0 =1 .. How

How many of you asked this question to others or yourself . did u guys get the answer for this ?

well, Proof for this so simple ..

(Anything)^0  =1 

replace 0 by  1/(Infinity) ... we finished    ... for instance    (234)^ 0 = 1 .. check out below fig



(A+B)^2 = A^2 + B^2 + 2AB ???



Thread

Thread is most important concept in software development . It is very important to know about it .

What is Thread : Thread is separate execution of code 

one thing I wanted to tell ,Threads are EVIL ,don't create it until and unless an absolute necessary . 

Why (Advantage) Threads : To have concurrent execution of code .

Why not (Disadvantages) : 
  1. Thread introduces context switch ,which will slow down the application performance
  2. Need to use synchronization for the shared object  

yes , I hope you know , why Thread is having separate STACK and why it is sharing HEAP ...

anyways I'll be explaining this in my future blogs ... Keep reading ...
 



Active Scheduler in Symbian

We know, what is Active Scheduler and what is the role in symbian ...
Now ,I wanted to explain ,why we are creating Active Scheduler for all the thread , why can't we have only one Active Scheduler  for all the thread ..

Ok, I'll explain multithreading application with single Active scheduler, at the end  you will come to know where the problem starts and why we are creating Activescheduler per Thread.

Active Scheduler contains 2 Active object both are requested from each thread.

Now 
1. AO1 is running ,Thread1 cycle is completed before completing the RunL() of the AO1
2. Now , Thread2's time to execute , and AO2's request has completed .
3. since AO1 is not completed ,Active scheduler won't start AO2  execution

    see , here we are wasting Thread2's cycle . and AS wont start AO2 until and unless AO1 is completed.

In case , If we have AS per thread , then there is no wastage of cycle.

Hope now you understand why we need to create AS for each Thread.





Tuesday, June 19, 2012

Synchronous AsyncTask

In Android , there are some APIs (specially network related ) can called from another thread. and there will be some situation you will be encountered that , the main thread needs result of the Asynctask thread .

well , it is quite easy in android

you have to use below piece of code

             new TimerTask().execute().get();


timertask is extends from AsyncTask .

ie Thread1 will wait for thread2's result ..