{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect23 - DS CS 11002 Computer Sc Engg IIT Kharagpur $ 1...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 & $ % Dynamic Allocation of Array Space Lect 23 Goutam Biswas
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
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 & $ % Request for Run-Time Allocation The volume of data may not be known before the run-time. It provides flexibility in building data structures. The space is allocated in global data area (not on the stack) called heap and so does not evaporate at the end of function call. Lect 23 Goutam Biswas
Image of page 2
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3 & $ % void *malloc(size t n) The C library function malloc() is used to request for an allocation of n bytes of contiguous memory space in the global data area . After a successful allocation the function returns a generic pointer void * that can be casted to any required type. Lect 23 Goutam Biswas
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
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4 & $ % void *malloc(size t n) If malloc() fails to allocate the requested space, it returns a NULL pointer. The interface of the function is defined in the header file stdlib.h , so it is to be included. Lect 23 Goutam Biswas
Image of page 4
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5 & $ % Note The function malloc() sends request to the OS for more data space. On request OS supplies multiples of a fixed chunk of memory e.g. multiples of 4 Kbytes. The underlying memory management system of malloc() manages the available memory. Lect 23 Goutam Biswas
Image of page 5

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6 & $ % Pointer Variables and Array Consider the following declarations: int *p, (*q)[5], *r[3], **s ; The variable p is of type int * . It can store the address of a location of type int and follows the int pointer arithmetic i.e. p + i is (int)p + i × sizeof(int) . Lect 23 Goutam Biswas
Image of page 6
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7 & $ % Pointer Variables and Array int *p, (*q)[5], *r[3], **s ; The variable q is of type int (*)[5] . It also stores an address but its arithmetic is different from p . The value of q + i is (int)q + i × 5 × sizeof(int) . It may be viewed as a pointer to an 1-D array of five (5) locations of type int . The arithmetic of q is identical to the arithmetic of a in a[3][5] . Lect 23 Goutam Biswas
Image of page 7

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8 & $ % Pointer Variables and Array int *p, (*q)[5], *r[3], **s ; r is not a variable but a constant. Its value is the address of the 0 th location of the array of three ( 3 ) locations, each of type int * . Naturally r + i is (int)r + i × sizeof(int *) . Lect 23 Goutam Biswas
Image of page 8
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9 & $ % Pointer Variables and Array int *p, (*q)[5], *r[3], **s ; Finally the variable s is of type int ** . It can store the address of a location of type int * . The meaning of s + i is (int)s + i × sizeof(int *) . It is a pointer to an int pointer, so the arithmetic of r and s are identical. Lect 23 Goutam Biswas
Image of page 9

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10 & $ % C Program #include <stdio.h> #include <stdlib.h> int main() { int *p, (*q)[5], *r[3], **s; printf("sizeof(int): %d\n", sizeof(int)); printf("sizeof(int *): %d\n", sizeof(int *)); printf("sizeof(int [5]): %d\n", sizeof(int [5])); printf("sizeof(int (*)[5]): %d\n", sizeof(int (*)[5])); printf("sizeof(int **): %d\n", sizeof(int **)); putchar(’\n’); Lect 23 Goutam Biswas
Image of page 10
Image of page 11
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