Monday, 29 June 2015
Monday, 28 July 2014
Linux Memory Layout Test through C
If you have gone through any Operating System Course, you would have come across the Memory Layout Diagram of a Process in physical RAM. Below diagram show the layout of a process in the memory:
From this source code, let us try to predict where this variables would actually be getting stored in the memory.
We know that command line arguments and environment variable goes to the highest memory available to the process. Therefore, the address for this variables must be greater than any other variable or function address.
Next comes the stack. First function that is called in our program is main function itself which makes a call to func which in turn calls func2. Therefore, main should be at the bottom of the stack followed by func and func2 respectively. Mapping this stack into the memory, as stack grows from high memory to low memory, memory address of local variable of main should be greater than (but should be close to) local variable of func whereas func2's local variable should be having the lowest memory address among the three.
On the other hand, heap start from other side (lower address side) and grows toward stack. Therefore any memory allocated on heap should have address lower than any memory on stack. Our dynamic memory arr2 is allocated after arr1. Therefore, address of arr2 should be greater than arr1, as heap grow toward higher memory side.
Third section is data section which comprises of initialized and uninitialized data. Data section lies just below the heap. Hence, it address must be always less than any memory in heap or above heap. Also, uninitialized data is followed by initialized data. Therefore, address of any uninitialized data must be greater than that of initialized data. This behavior can be seen with the variable g1, g2, g3 and g4.
At last comes the text section which stores the read-only code. These falls at the bottom of the memory allocated to process and must be having address lower than any other section.
But, the question is how does these layout actually maps to a program that i have written?
Without an example with an actual running program, this concept seems vague and is pretty hard to visualize. I have written a C program to actually verify this layout in the memory. Here is the C code:
Without an example with an actual running program, this concept seems vague and is pretty hard to visualize. I have written a C program to actually verify this layout in the memory. Here is the C code:
From this source code, let us try to predict where this variables would actually be getting stored in the memory.
We know that command line arguments and environment variable goes to the highest memory available to the process. Therefore, the address for this variables must be greater than any other variable or function address.
Next comes the stack. First function that is called in our program is main function itself which makes a call to func which in turn calls func2. Therefore, main should be at the bottom of the stack followed by func and func2 respectively. Mapping this stack into the memory, as stack grows from high memory to low memory, memory address of local variable of main should be greater than (but should be close to) local variable of func whereas func2's local variable should be having the lowest memory address among the three.
On the other hand, heap start from other side (lower address side) and grows toward stack. Therefore any memory allocated on heap should have address lower than any memory on stack. Our dynamic memory arr2 is allocated after arr1. Therefore, address of arr2 should be greater than arr1, as heap grow toward higher memory side.
Third section is data section which comprises of initialized and uninitialized data. Data section lies just below the heap. Hence, it address must be always less than any memory in heap or above heap. Also, uninitialized data is followed by initialized data. Therefore, address of any uninitialized data must be greater than that of initialized data. This behavior can be seen with the variable g1, g2, g3 and g4.
At last comes the text section which stores the read-only code. These falls at the bottom of the memory allocated to process and must be having address lower than any other section.
Here is the sample output of the program when run at ideone. Click on the link to see the run output at their site itself. I am pasting the same here:
You can verify that this conforms to above discussion. Here is a above memory layout diagram modified for our program:
You can verify that this conforms to above discussion. Here is a above memory layout diagram modified for our program:
Thursday, 24 July 2014
Setting and Getting Environment Variable in C
There are a number of way by which you can access environment variable in C. Most common of them is to declare as
envp contains all environment strings. Here is an example program:
We can also use external variable 'environ' to get all the environment variable.
Here is an example program:
Also,there are function available in stdlib through which we can access any particular environment variable as well as add and modify them. getenv is use to get value of an environment variable whereas setenv is use to add and replace them. setenv also takes an optional parameter which specify whether we want to overwrite any existing variable or not.
Here is a sample program:
The output of above code would be:
We can also use external variable 'environ' to get all the environment variable.
Here is an example program:
Also,there are function available in stdlib through which we can access any particular environment variable as well as add and modify them. getenv is use to get value of an environment variable whereas setenv is use to add and replace them. setenv also takes an optional parameter which specify whether we want to overwrite any existing variable or not.
Here is a sample program:
The output of above code would be:
Friday, 6 December 2013
Java Code To Search Directories Recursively For A File
Here is the Java Code to search for a file recursively through the code.
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class FileSearch {
static List<String> result = new ArrayList<String>();
public static void main(String[] args) {
String dirc = "C:\\Users\\naved.alam\\workspace\\Test";
String fName = "FileSearch.java";
FileSearch.search(new File(dirc),fName);
int size = result.size();
if(size ==0){
System.out.println("\nNo File found.");
}else{
System.out.println("\nFound " + size + " File:");
for (String matched : result){
System.out.println("File : " + matched);
}
}
}
private static void search(File directory,String fileName) {
if (directory.isDirectory()) {
System.out.println("Searching directory..." + directory.getAbsoluteFile());
//Check for permission
if (directory.canRead()) {
for (File temp : directory.listFiles()) {
if (temp.isDirectory())
search(temp,fileName);
else if (fileName.equals(temp.getName()))
result.add(temp.getAbsoluteFile().toString());
}
}
else
System.out.println(directory.getAbsoluteFile() + ": Permission Denied");
}
}
}
A sample run is shown below:
Searching directory...C:\Users\naved.alam\workspace\Test Searching directory...C:\Users\naved.alam\workspace\Test\.settings Searching directory...C:\Users\naved.alam\workspace\Test\backup Searching directory...C:\Users\naved.alam\workspace\Test\bin Searching directory...C:\Users\naved.alam\workspace\Test\bin\com Searching directory...C:\Users\naved.alam\workspace\Test\bin\com\example Searching directory...C:\Users\naved.alam\workspace\Test\src Searching directory...C:\Users\naved.alam\workspace\Test\src\com Searching directory...C:\Users\naved.alam\workspace\Test\src\com\example Found 2 File: File : C:\Users\naved.alam\workspace\Test\backup\FileSearch.java File : C:\Users\naved.alam\workspace\Test\src\com\example\FileSearch.javaAbove code can be modified to search for a file matching certain pattern or expression and can be use to integrate advance file searching functionality in an application.
Horizontal Scrolling And Problem Of Word Wrap in Edittext in Android
Recently, I was trying to develop some application for Android device, I came across a bug. I had a edittext with inputType set to 'textMultiLine'.
android:inputType="textMultiLine"
Sunday, 1 December 2013
Generating ASCII Art From Input Text Or Image Using JAVA
Wikipedia describe as ASCII art as "A graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard from 1963 and ASCII compliant character sets with proprietary extended characters (beyond the 128 characters of standard 7-bit ASCII)."
Friday, 22 November 2013
C# keywords VS Java keywords
Here is comparison between the keywords available in Java Language and C# Language:
Subscribe to:
Posts (Atom)

