Memory Allocation. General Questions

Similar documents
4) In C, if you pass an array as an argument to a function, what actually gets passed?

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Dynamic Memory Allocation

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Lecture 8 Dynamic Memory Allocation

In Java we have the keyword null, which is the value of an uninitialized reference type

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Computer Programming Unit 3

Memory (Stack and Heap)

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Dynamic Data Structures. CSCI 112: Programming in C

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Dynamically Allocated Memory in C

Pointers, Dynamic Data, and Reference Types

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha

Fundamental of Programming (C)

o Code, executable, and process o Main memory vs. virtual memory

Final Intro to C Review

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Dynamic memory allocation (malloc)

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

{ int kk, ll; kk = ii + jj; ll = ii * jj; return (kk, ll); } A. Function addmult() return 7 and 12 B. No output C. Error: Compile error D.

Programming. Pointers, Multi-dimensional Arrays and Memory Management

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Procedural programming with C

Kurt Schmidt. October 30, 2018

CS61C : Machine Structures

Lecture 14. Dynamic Memory Allocation

Dynamic Allocation in C

Dynamic Memory Allocation: Basic Concepts

dynamic memory allocation

Dynamic Allocation of Memory Space

Memory Management. CSC215 Lecture

CS 11 C track: lecture 5

Library Functions. General Questions

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Memory Management and

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Arrays and Pointers (part 1)

Memory Management in C (Dynamic Strings) Personal Software Engineering

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

C Structures & Dynamic Memory Management

Lesson 4 struct and memory management

CS61C : Machine Structures

Dynamic Memory Allocation: Basic Concepts

Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370

Heap Arrays. Steven R. Bagley

Dynamic Memory Management

Memory Allocation in C

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

CS61, Fall 2012 Section 2 Notes

CSC 1600 Memory Layout for Unix Processes"

MODULE Z THE C STORAGE CLASSES, SCOPE AND MEMORY ALLOCATION

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Character Strings. String-copy Example

C Programming Basics II

C Tutorial Pointers, Dynamic Memory allocation, Makefile

ECE 15B COMPUTER ORGANIZATION

Binghamton University. CS-211 Fall Dynamic Memory

Class Information ANNOUCEMENTS

Dynamic memory allocation

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Huawei Test 3. 1 A card is drawn from a pack of 52 cards. The probability of getting a queen of club or a king of heart is:

Content. In this chapter, you will learn:

Understanding Pointers

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Advanced Pointer Topics

Fall 2018 Discussion 2: September 3, 2018

Dynamic Memory Allocation

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

Dynamic Allocation of Memory

COMP 2355 Introduction to Systems Programming

Lectures 5-6: Introduction to C

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

High Performance Programming Programming in C part 1

Arrays and Pointers (part 1)

Programs in memory. The layout of memory is roughly:

Quick review pointer basics (KR ch )

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

int marks[10]; // fixed size and fixed address No change in Memory address.

Dynamic Memory Allocation I Nov 5, 2002

Pointers, Arrays, and Strings. CS449 Spring 2016

PRINCIPLES OF OPERATING SYSTEMS

Data and File Structures Laboratory

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

Transcription:

General Questions 1 Memory Allocation 1. Which header file should be included to use functions like malloc() and calloc()? A. memory.h B. stdlib.h C. string.h D. dos.h 2. What function should be used to free the memory allocated by calloc()? A. dealloc(); B. malloc(variable_name, 0) C. free(); D. memalloc(variable_name, 0) 3. How will you free the memory allocated by the following program? #define MAXROW 3 #define MAXCOL 4 int **p, i, j; p = (int **) malloc(maxrow * sizeof(int*)); A. memfree(int p); B. dealloc(p); C. malloc(p, 0); D. free(p); Answer: Option D 4. Specify the 2 library functions to dynamically allocate memory? A. malloc() and memalloc() B. alloc() and memalloc() C. malloc() and calloc() D. memalloc() and faralloc()

2 Find Output of Program 1. What will be the output of the program? int *p; p = (int *)malloc(20); /* Assume p has address of 1314 */ free(p); printf("%u", p); A. 1314 B. Garbage value C. 1316 D. Random address 2. What will be the output of the program (16-bit platform)? int *p; p = (int *)malloc(20); printf("%d\n", sizeof(p)); free(p); A. 4 B. 2 C. 8 D. Garbage value 3. What will be the output of the program? #include<string.h> char *s; char *fun(); s = fun(); printf("%s\n", s); char *fun() char buffer[30];

strcpy(buffer, "RAM"); return (buffer); A. 0xffff B. Garbage value C. 0xffee D. Error 3 The output is unpredictable since buffer is an auto array and will die when the control go back to main. Thus s will be pointing to an array, which not exists. 4. What will be the output of the program? union test int i; float f; char c; ; union test *t; t = (union test *)malloc(sizeof(union test)); t->f = 10.10f; printf("%f", t->f); A. 10 B. Garbage value C. 10.100000 D. Error 5. Assume integer is 2 bytes wide. How many bytes will be allocated for the following code? #define MAXROW 3 #define MAXCOL 4 int (*p)[maxcol]; p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p)); A. 56 bytes B. 128 bytes C. 24 bytes D. 12 bytes

4 6. Assume integer is 2 bytes wide. What will be the output of the following code? #define MAXROW 3 #define MAXCOL 4 int (*p)[maxcol]; p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p)); printf("%d, %d\n", sizeof(p), sizeof(*p)); A. 2, 8 B. 4, 16 C. 8, 24 D. 16, 32 7. How many bytes of memory will the following code reserve? int *p; p = (int *)malloc(256 * 256); if(p == NULL) printf("allocation failed"); A. 65536 B. Allocation failed C. Error D. No output Hence 256*256 = 65536 is passed to malloc() function which can allocate upto 65535. So the memory allocation will be failed in 16 bit platform (Turbo C in DOS). If you compile the same program in 32 bit platform like Linux (GCC Compiler) it may allocate the required memory. Point Out Errors

5 1. Point out the error in the following program. int *a[3]; a = (int*) malloc(sizeof(int)*3); free(a); A. Error: unable to allocate memory B. Error: We cannot store address of allocated memory in a C. Error: unable to free memory D. No error We should store the address in a[i] 2. Point out the error in the following program. char *ptr; *ptr = (char)malloc(30); strcpy(ptr, "RAM"); printf("%s", ptr); free(ptr); A. Error: in strcpy() statement. B. Error: in *ptr = (char)malloc(30); C. Error: in free(ptr); D. No error Answer: ptr = (char*)malloc(30); Point Out Correct Statements 1. Point out the correct statement will let you access the elements of the array using 'p' in the following program?

6 int i, j; int(*p)[3]; p = (int(*)[3])malloc(3*sizeof(*p)); A. for(i=0; i<3; i++) for(j=0; j<3; j++) printf("%d", p[i+j]); for(i=0; i<3; i++) B. printf("%d", p[i]); C. for(i=0; i<3; i++) for(j=0; j<3; j++) printf("%d", p[i][j]); for(j=0; j<3; j++) D. printf("%d", p[i][j]); 2. Which of the following statement is correct prototype of the malloc() function in c? A. int* malloc(int); B. char* malloc(char); C. unsigned int* malloc(unsigned int); D. void* malloc(size_t); Answer: Option D 3. Point out the correct statement which correctly free the memory pointed to by 's' and 'p' in the following program? struct ex int i; float j; char *s ; struct ex *p; p = (struct ex *)malloc(sizeof(struct ex));

p->s = (char*)malloc(20); A. free(p);, free(p->s); B. free(p->s);, free(p); C. free(p->s); D. free(p); 7 4. Point out the correct statement which correctly allocates memory dynamically for 2D array following program? int *p, i, j; /* Add statement here */ for(i=0; i<3; i++) for(j=0; j<4; j++) p[i*4+j] = i; printf("%d", p[i*4+j]); A. p = (int*) malloc(3, 4); B. p = (int*) malloc(3*sizeof(int)); C. p = malloc(3*4*sizeof(int)); D. p = (int*) malloc(3*4*sizeof(int)); Answer: Option D True / False Questions 1. malloc() returns a float pointer if memory is allocated for storing float's and a double pointer if memory is allocated for storing double's. A. True B.False 2. malloc() allocates memory from the heap and not from the stack. A. True B.False

8 3. malloc() returns a NULL if it fails to allocate the requested memory. A. True B.False 4. If malloc() successfully allocates memory it returns the number of bytes it has allocated. A. True B.False Syntax: void *malloc(size_t size); The malloc() function shall allocate unused space for an object whose size in bytes is specified by size and whose value is unspecified. The order and contiguity of storage allocated by successive calls to malloc() is unspecified. The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object in the space allocated (until the space is explicitly freed or reallocated). Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned points to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer shall be returned. If the size of the space requested is 0, the behavior is implementation-defined: the value returned shall be either a null pointer or a unique pointer. Yes / No Questions 1. Can I increase the size of dynamically allocated array? A. Yes B.No Use realloc(variable_name, value); 2. Can I increase the size of statically allocated array? A. Yes B.No

3. When we dynamically allocate memory is there any way to free memory during run time? A. Yes B.No Using free() 9