{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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.
Image of page 1

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

View Full Document Right 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
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”); exit(1); } printf(“\nBuffer size modified. \n”);
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern