Chapter 13-p - Example 13.1 Write a program that uses a...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 13.1 Write a program that uses a table of integers whose size will be specified interactively at run time. The program is given in Fig.13.2. It tests for availability of memory space of required size. If it is available, then the required space is allocated and the address of the first byte of the space allocated is displayed. The program also illustrates the use of pointer variable for storing and accessing the table values.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
USE OF malloc FUNCTION Program #include <stdio.h> #include <stdlib.h> #define NULL 0 main() { int *p, *table; int size; printf(“\nWhat is the size of table?”); scanf(“%d”,size); printf(“\n”) /*------------Memory allocation --------------*/ if((table = (int*)malloc(size *sizeof(int))) == NULL) { printf(“No space available \n”); exit(1); } printf(“\n Address of the first byte is %u\n”, table); /* Reading table values*/ printf(“\nInput table values\n”); for (p=table; p<table + size; p++) scanf(“%d”,p); /* Printing table values in reverse order*/ for (p = table + size –1; p >= table; p --) printf(“%d is stored at address %u \n”,*p,p); } Output What is the size of the table? 5 Address of the first byte is 2262 Input table values 11 12 13 14 15 15 is stored at address 2270 14 is stored at address 2268 13 is stored at address 2266 12 is stored at address 2264 11 is stored at address 2262 Fig.13.2 Memory allocation with malloc
Background image of page 2
Example 13.2 Write a program to store a character string in a block of memory space created by malloc and then modify the same to store a larger string. The program is shown in Fig. 13.3. The output illustrates that the original buffer size obtained is modified to contain a larger string. Note that the original contents of the buffer remains same even after modification of the original size. USE OF realloc AND free FUNCTIONS Program #include <stdio.h> #include<stdlib.h> #define NULL 0 main() { char *buffer; /* Allocating memory */ if((buffer = (char *)malloc(10)) == NULL) { printf(“malloc failed.\n”); exit(1); } printf(“Buffer of size %d created \n”,_msize(buffer)); strcpy(buffer, “HYDERABAD”); printf(“\nBuffer contains: %s \n “, buffer); /* Realloction */ if((buffer = (char *)realloc(buffer, 15)) == NULL) { printf(“Reallocation failed. \n”);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/14/2010 for the course CS SS ZG653 taught by Professor Shanta during the Spring '09 term at Birla Institute of Technology & Science.

Page1 / 10

Chapter 13-p - Example 13.1 Write a program that uses a...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online