Tuesday, 25 August 2015

Pointers

To understand the basic level of pointers we will be using C language. I'll be sharing some examples from HarvardX school of Computer Sciences & Stanford cs library.

The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, great power comes with great responsibility and this power is enough to screw things up spectacularly as simple errors can corrupt the state of your program in ways that make debugging difficult. The goal of these slides is to make sure that you understand exactly what you're doing when you use pointers in your code.
Here is an video example of  Binky clay telling about pointers.

FIRST OF ALL WHY DO WE USE POINTERS?

Yea one may ask what is the need of the pointers? Well when it comes to the concept of data structures using Linked List and Binary Search Trees. Pointers made it possible to implement them because without them there would be no Linked list or Binary Trees. Another reason of using pointers is we get access to our variables for sharing purpose quite easily, like without pointers we may have to work around to copy back and forth the data but because of pointers we don't need this.

USAGE

Next we are thinking how do we declare a pointer? See the syntax below
syntax: int* a;You have just declared an integer type pointer of variable a.



Referencing 

After declaration the very first thing you need to do is referencing your pointer in order to use it, Unfortunately compiler or langues won't help you in that thats the task you have to perform by your hands.
So next question raises in you mind must be how do we reference it? right..
You can do it like
syntax: a = malloc(sizeof(int)); 
Note:
it gives a new place in the memory to the variable a

You can make a reference for already declared variable as a = &variable as well because there maybe a chance if you try to refer it and there is lack of space in the memory. 


De-Referencing

You have successfully referenced your variable and you are ready to de-reference it. We can use the * operator to de-referencesyntax: a* = 40;Remember here you can de-reference only similar type otherwise it would be a run time error.
Code and data for your program are stored in random-access memory (RAM), which is basically a huge array of 1 byte (8 bits) blocks. Each block is associated with a hexadecimal number that represents its memory address.
Just as ints are variables that store integers and floats are variables that store floating-point values, a pointer is a variable that stores memory addresses. In the appliance (a 32-bit operating), a memory address is 4 bytes, so it makes sense that a pointer is also 4 bytes

Saturday, 1 August 2015

Working of Stack (Explained)

Stack is the simplest Data Structure with the policy of LIFO (Last In First Out), remember stack of trays ? Stack of coins placed above on each? Or you may think about it when you are Undoing and Redoing something on computer, Yes it works according to stack as well. This was the pretty much theory you need to know for stack now lets get implement it in programming.
What Ingredients you need to have to make a perfect working stack ?
Here are the followings:

  • You need to PUSH any object in stack
  • You need to POP any object from stack
  • You may check the current POSITION 
  • For PUSH you need to know if it isn't FULL so you'll be making it as well
  • For POP you need to know if it isn't EMPTY.
  • Then the last you need to built a MENU for taking user input again and again
First make the functions is_Empty and is_Full so that Push and Pop functions can check the error and not get caught in exception.
So for making the function is_Full we need to define variables and stuff to get know how bigger is stack and at what condition stack will be called full. right?



We'll be using Class here which is named "Stack". So in the body we have declared top to locate the current position of stack , size to ask user how much bigger stack user wants. Then in constructor "Stack()" assigned the values to the pointer array for dynamic input in the program.


Here we go straight after making Constructor we defined prototypes of our functions we'll be defining outside of the class (Prototypes are just a promise to function that declare them here but we'll define them later in the program). Our functions will be returning the True or False (e.g if stack is empty ? Yes or No ?) except current_place( ) because it has to return the integer with the current place. 

We'll be defining these functions outside the class. For that you have to do it as " Type NameOfClass ScopeResolutionOperator FunctionName(your declared prototype) " in code picture you can see it as " bool Stack :: is_full( ) ".
Till now we got set our Stack program to ask user how much bigger stack should work for you. What'll be function you'll be defining in this program. Next we want to start defining our functions to work with.



Simply put "is_Empty( )" & "is_Full( )" inside the if condition to check whether there's any exception or not. While the function "Current_Place( )" will simply tell you what's in your stack and if your stack is empty or not. How easy is that?



Function for displaying the menu list for the user to select the option and perform their operations.



Sunday, 1 March 2015

How to get your lost data back ?

                             Welcome to my blog

Three days back i was running into a terrible problem. I ask you have you ever suffered through, that you accidentally formatted your hard disk and then you realize "o gosh!, what i have done this data was so important to me." and started abusing and get frustrated about your act.? Well if you do then continue reading.I'll like to go in detail for your understanding that what operating system has been doing with your. If you are not interested in getting the explanation that how it works you can skip to the section for quick methods. As kinda same thing happened to me i accidentally formatted my whole data of 500 Gigabytes and installed ubuntu 14.0.2 replacing Windows 8.1. 
So the First thing first, if it ever happened to you follow these rules: 
1. Don't get panicked (you can get your all data back but with some presence of mind if you panicked things can go more wrong)
2. Find out your category of problem on this blog(i'll explain later each possibility i have faced).
Okay when you have done these two things you are ready to proceed further we gonna take this sunken ship out of the sea. Here i just wanted to share this " Do you know the empty/formatted drive is just an eye wash to you ?" yes the data is never deleted from your hard disk until you override that data with something else. Was it little bit of motivating toward your task ? i think it was now you can surely hope how to get our data back. I have came across two Operating systems:
 1. Ubuntu 
 2. Windows
Honestly saying i was under opinion that  i should switch totally to Linux from windows but it was not worth migrating totally because in my opinion you can get plenty of software which you can have on windows for free but you don't have another way on Linux  so as being a student i made a choice to keep both of them. If you are Linux user and your data is lost on Linux os as well then you got a great chance to recover that data through 'testdisk'. Before jumping on to it i would like to tell you the pretty background of Linux file system. 
1. Ext2
It uses It is known as 'second extended file system' used for flash drives, usb drives, ext2 is recommended and it has maximum individual file size can be from 16 GB to 2 TB 
2. Ext3 It stands for 'third extended file system' the basic benefit of this system is that it keeps track of the changes that will be made on drive and place them on right place, and its size can go from 2 TB to 32 TB.
3. Ext4 It is known as 'fourth extended file system' it sizes even bigger than ext3 upt to 1 Exabyte which is actually 1024 petabyte of data. Now your drives are on ext3/ext4 system which is pretty different from windows file based system (NTFS, FAT),
Windows cant read those files listed above but some how Linux can read them and can show their existence while they are not operate able on Linux. That's how much the pretty basics of it lets get back to our original topic.                                                               
                                                                     1. Ubuntu

Linux users should follow these steps:

  1. Open up the terminal if you are using the GUI on Linux just press "Ctrl+Alt+T".
  2. You need to install 'testdisk' on your PC as its not official software you won't find it on store probably write "sudo get-apt testdisk". it will eventually install this software.
  3. Now you need to run it just type in the terminal "testdisk".
  4. A main screen of testdisk will appear with multiple options to select choose the first one "Analyze".
  5. After selecting it the screen will let you choose the hardware system you are using "intel" is the mostly used hardware, but this software will actually let you to the option it detects automatically just choose it.
  6. Then you need to select the hard disk or current partition you are shown select one you want to recover. You will have option of "Quick Search" on the left bottom of the terminal select it to search in the hard disk it will take some time in searching depending on the size of the disk.
  7. Almost half of the part is done, now you will see multiple folders due to search on the hard you will see multiple  
  8. You can look into those folders by just pressing "P'" on them and check whether you want it back or not.
  9. If you have decided to take it back method is so simple just press "C" on that particular folder and provide it destination where you want to place it. 
So that was it pretty sweet you can recover you data from Linux on Linux.

                                                                         2. Windows
Windows users have GUI so dont have any problem in dealing with softwares i guess, any how just follow these steps:

  1. Download "R Studio" first.
  2. It will show you the drives which are active on the left side. Select one which you want to diagnose.
  3. Run "Scan" on it, just right click on the drive and choose scan by selecting the option further on it "without showing the progress fastest way" . It will not show you the scan progress but it will work fastest.
  4. Wait for the scan to complete and list of your data will be shown there will be four colors of them "Red, Green, orange and Black". The Green,orange and black folders will recover with no problem but red will recover with loss of some data.
  5. After choosing your folders provide them your destination to copy the data by selecting "Recover Marked" option on the bar of the window.
that's it you have restored the files which you have formatted or deleted permanently .

 What if you have over ridden a Partition type and want previous version of HardDisk ?


Those were the simple ways explained above but what will you do if you have formatted a hard drive in switching between Linux to Windows or Windows to Linux ? Well now you gotta take it seriously because it become harder and harder to get your data back if you paste another layer on your data the deeper it goes harder it is to take it back.

You can see files of windows from Linux but you cant see files of Linux through windows.
Well in that case you need an external Hard disk to solve this matter, Microsoft have introduced a new featured "Microsoft Go" it gets install on any USB or External Hard Drive with recommended 3.0 port of usb if not its still okay to go with 2.0 port.
Some people encounter installing "Microsoft Go" like that you can not install this on MBR type discs and you can install it on GPT type, and another error like that. I can quickly guide you through this as well when you encounter these error just close the dialog box of selecting external drive and you will came back on the starting dialogue with "Install Windows". Just there you need to follow these steps:



  1. press "Shift+f10", another dialogue box will appear,.
  2. Type command "diskpart".
  3. Then "list disks". it will show the discs connected to your system. (if you have external hard/usb same of your hard inside your system you can unplug the external one and type again "list disks" you will get to know which one id your internal hard and external hard)
  4. Now you know your external hard after seeing the list type command "select disk 0" (you can put your disk number i just typed '0' for now)
  5.  Now type "clean" and it will erase the auto set type of disks and fix the error. Select the drive now install the windows.
Point is you can install that Microsoft GO on your external hard drive and use it as a portable windows just plug the usb/external hard and work on your windows so by doing this you can install "R Studio" on it and scan the linux hard drive on it. There you will be able to see that hard only in this software. Repeat the process as explained above for windows and recover your data. Thats how it worked for me if you got any problem in doing this please comment and let me know , Thanks for reading this post :3