Skip to main content

Featured Post

How to use Tabs in ASP.NET CORE

I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component  public class AllViewComponent : ViewComponent     {         private readonly UserManager<ApplicationUser> _userManager;         public AllViewComponent(UserManager<ApplicationUser> userManager)         {             _userManager = userManager;         }         public async Task<IViewComponentResult> InvokeAsync()         {             List<StudentViewModel> allUsers = new List<StudentViewModel>();             var items = await _userManager.Users.ToListAsync();             foreach (var item in items)             {                 allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email });             }            

Pointer and Dynamic memory Allocation in data structure through C Language

                                                            When the variables are declared in a program, usually the memory is allocated during the compilation that is statically. The memory referred by such variables is used to store the value (data) directly. Instead of data directly if another memory location is to be stored in the variables, then it should be a pointer variable. As we are already familiar with the dynamic memory allocation, and if it is to be implemented then without pointer variables it is not possible. During the execution of program dynamic memory is allocated, so there must be a variable that stores addresses of memory at that time. That is the reason why pointer variable is used. When pointer variables are declared no memory is allocated to store the data. The memory is allocated to them only to store the memory address not the data. Taking this as an advantage the pointer variable can be made to point dynamically allocated memory location.

For example if “p” is a pointer variable declared of the type “int” then for p memory is allocated to store the address of a memory is location that stores an “int” (integer data). Here the memory address that is to be stored in pointer variable may be static or dynamic. Usually it is to be dynamic memory allocation. It can be pictured as follows:
int *p, a;           ‘p’ is a pointer variable, ‘a’ is a simple integer variable. For ‘a’
                           memory is allocated statically to store integer data. Of course for                  
                           ‘p’ also memory is allocated but to store an address not data.
a=10;                 at memory location is reserved by ‘a’, an integer 10 is stored.
P=&a;                ‘p’ as it is pointer variable used to store address of statically
                           allocated memory.
With the help of pointer ‘p’ is possible to refer integer value stored at variable ‘a’ indirectly through pointer ’p’.

If ‘a’ is user to print, it prints the value 10, if &a (&a is called address operator) is used, it prints 1000 as per the examples. If p is used it also prints 1000 the content of p the address of memory is allocated to a. If *p (de-referencing of pointer) is used it prints the value of memory location pointed by the pointer variable. Here it prints the value of ‘a’ that is 10.
We can take the advantage of storing the memory address by a pointer variable during the execution of program. So instead of storing the already allocated static memory, a dynamic memory maybe requested during the execution of program and the dynamically allocated memory address may be stored in a pointer variable and it is used as it is normal variable.
Dynamically the memory is allocated using the standard functions calls depending on the high level languages used. Like in Pascal new used, in C++ new operator is used and in C language the functions like malloc(), calloc() and realloc() are used.
So, in C language if memory is requested by the user during the execution of the program to store data one of the following functions may be used. The functions are different as per the purpose and the numbers of arguments passed to the functions. All these functions return an address called the base address of dynamically allocated memory and is store in a pointer variable.


For example (int *) malloc (20) – allocates 20 bytes of consecutive memory dynamically on success, (or return a NULL address if not successfully allocates) and returns the base address. The returned address may be stored in a pointer variable of the type int and 10 integers can be referred with the help of that pointer variable. It is nothing but an array of size 10 for which the memory is allocated during the execution of program. The initial values stored in the memory location allocated are garbage values.




So, the memory allocated by the ‘calloc’ function is the product of number_of_elements and size_of_each_element. For example (int *) calloc (10, 2) allocates 20 bytes of dynamic memory. So, 10 integers can be stored in those memory locations. The initial values of these memory locations will be zero. Similar to malloc (), the returned address may be stored in a pointer variable and treated as an array of 10 integers.


If the dynamic memory allocated using either calloc or malloc is to be changed during the execution of program then realloc is used. It allocates completely a new memory block and copies the contents of already allocated block and returns the new base address. That address may be stored in the same pointer variable. Otherwise it expands the existing block and returns the same base address. The change may even reduce the size of already allocated block.
The dynamic memory is allocated from the heap area of main memory. If the requested memory is not available the dynamic memory is allocation functions return NULL address or invalid address, it is also treated as a 0 (zero) value. The dynamically allocated memory may be freed from the programmer after his/her usage using free () function. The prototype of void free (void *block) is available in alloc.h. If the user does not free the dynamically allocated memory, it is the function of operating system to collect the dynamic memory periodically and give it back to heap. This function of operating system called as “garbage collection”. It is better practice for the programmers to free the dynamically allocated memory after the use of such memory. If you write the statements for dynamic memory allocation then remember that the free statement is also used in the program before the end of program to ensure to free the dynamically allocated memory. 

Comments

Popular Post

How to use Tabs in ASP.NET CORE

I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component  public class AllViewComponent : ViewComponent     {         private readonly UserManager<ApplicationUser> _userManager;         public AllViewComponent(UserManager<ApplicationUser> userManager)         {             _userManager = userManager;         }         public async Task<IViewComponentResult> InvokeAsync()         {             List<StudentViewModel> allUsers = new List<StudentViewModel>();             var items = await _userManager.Users.ToListAsync();             foreach (var item in items)             {                 allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email });             }            

Polynomial representation using Linked List for Data Structure in 'C'

Polynomial representation using Linked List The linked list can be used to represent a polynomial of any degree. Simply the information field is changed according to the number of variables used in the polynomial. If a single variable is used in the polynomial the information field of the node contains two parts: one for coefficient of variable and the other for degree of variable. Let us consider an example to represent a polynomial using linked list as follows: Polynomial:      3x 3 -4x 2 +2x-9 Linked List: In the above linked list, the external pointer ‘ROOT’ point to the first node of the linked list. The first node of the linked list contains the information about the variable with the highest degree. The first node points to the next node with next lowest degree of the variable. Representation of a polynomial using the linked list is beneficial when the operations on the polynomial like addition and subtractions are performed. The resulting polynomial can also

Print the asp.net webpage using button

Introduction Today i am talking about printing, and how to print the content or you can say selected content in asp.net. If you have a webpage and you want to print this then you have to choose command key (ctrl+p) for that. Also you want to print the selected part then you have to select the text first then you have to use command key. But sometimes your selected page could not printed. So many websites provide the printing facilities on the webpage. Today i am talking about the same topics here. Lets take a simple example to demonstrate the topic. Source code: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="printpage.aspx.cs" Inherits="printpage" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title></title> <script> function printpage() { var getpanel = document.getElementById("<%= Panel1.ClientID%>"); var MainWin