MY mENU


Monday 24 September 2012

Memory Leak, Union, Heap,Stack in C


What is a memory leak?
Memory leak is that when memory is allocated but its not released because of an application consume memory reducing the available memory for other applications and causing the system to page virtual memory to the hard drive showing the application or crashing the application. When the computer memory resource limits are reached.

 Explain about storage of union elements.
The key point about storage of union elements is that Union elements are use to share common memory space.

What is the difference between the Heap and the Stack?
The main difference b/n the Heap and the Stack are given below:
- In stack we create object temporary where programmer used to reserve allocation.'
- stack not have automatic garbage collection where as heap have automatic garbage collection.

How the processor registers can be used in C?


using keyword register we can use the process register in C.Basically process register is used to store variables those i want to access frequently.if these registers are not used in any other program,then those registers we used to allocate memory these variables.

What are the stream files?


We can open file in C with using stream files.which are identified with a pointer to a FILE structure, or as low-level files, which are identified with an integer handle. We can open stream files with using one of the following functions.
fopen(): Opens the specified file with the specified mode 
freopen(): Closes the file specified, then opens a new file as specified 
fdopen(): Opens a duplicate stream file for an already open low-level file

Download manager Program in Android


This example shows how you can create download manager activity.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it DownloadManager.
2.) Add following permission into your manifest file:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
3.) Write following into main.xml:
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button android:text="Start Download" android:id="@+id/button1"
                android:layout_width="wrap_content"android:layout_height="wrap_content"
                android:onClick="onClick"></Button>
        <Button android:text="View Downloads" android:id="@+id/button2"
                android:layout_width="wrap_content"android:layout_height="wrap_content"
                android:onClick="showDownload"></Button>
        <ImageView android:layout_height="wrap_content"android:id="@+id/imageView1"
                android:src="@drawable/ic_launcher"android:layout_width="wrap_content"></ImageView>
</LinearLayout>
4.) Run for output.
Steps:
1.) Create a project named DownloadManager and set the information as stated in the image.
Build Target: Android 4.0
Application Name: DownloadManager
Package Name: com. example. DownloadManager
Activity Name: DownloadManagerActivity
Min SDK Version: 9
2.) Open DownloadManagerActivity.java file and write following code there:
package com.example.downloadmanager;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class DownloadManagerActivity extends Activity {
        private long enqueue;
        private DownloadManager dm;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                BroadcastReceiver receiver = new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context context, Intent intent) {
                                String action = intent.getAction();
                                if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                                        long downloadId = intent.getLongExtra(
                                                        DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                                        Query query = new Query();
                                        query.setFilterById(enqueue);
                                        Cursor c = dm.query(query);
                                        if (c.moveToFirst()) {
                                                int columnIndex = c
                                                                .getColumnIndex(DownloadManager.COLUMN_STATUS);
                                                if(DownloadManager.STATUS_SUCCESSFUL == c
                                                                .getInt(columnIndex)) {
                                                        ImageView view =(ImageView) findViewById(R.id.imageView1);
                                                        String uriString = c
                                                                        .getString(c
                                                                                        .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                                                        view.setImageURI(Uri.parse(uriString));
                                                }
                                        }
                                }
                        }
                };
                registerReceiver(receiver, new IntentFilter(
                                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        }
        public void onClick(View view) {
                dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                Request request = new Request(
                                Uri.parse("https://encrypted-tbn3.google.com/images?q=tbn:ANd9GcTIQQgqjfdNDRYQ3btnNJwRB07htjMKgeW2FyZrDCkxM7lfikmtHg"));
                enqueue = dm.enqueue(request);
        }
        public void showDownload(View view) {
                Intent i = new Intent();
                i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
                startActivity(i);
        }
}
3.) Compile and build the project.
Output