Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Similar documents
Understanding Pointers

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Common Misunderstandings from Exam 1 Material

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

PESIT Bangalore South Campus Hosur road, 1km before ElectronicCity, Bengaluru -100 Department of Basic Science and Humanities

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

Kurt Schmidt. October 30, 2018

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

CSC 1600 Memory Layout for Unix Processes"

PRINCIPLES OF OPERATING SYSTEMS

Dynamic memory allocation (malloc)

Array Initialization

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

ECE264 Fall 2013 Exam 3, November 20, 2013

CS 0449 Sample Midterm

Dynamic Memory Allocation

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

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

Memory Allocation in C

Course organization. Course introduction ( Week 1)

COMP 2355 Introduction to Systems Programming

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

Overview (1A) Young Won Lim 9/14/17

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Dynamic Memory Allocation and Command-line Arguments

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Overview (1A) Young Won Lim 9/9/17

Dynamic memory allocation

CSC 2400: Computer Systems. Arrays and Strings in C

Overview (1A) Young Won Lim 9/25/17

EL2310 Scientific Programming

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

C Programming Basics II

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

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

High Performance Programming Programming in C part 1

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

Dynamic Data Structures. CSCI 112: Programming in C

Contents. A Review of C language. Visual C Visual C++ 6.0

EE 312 Fall 2018 Midterm 1 Version A October 10, 2018

C & Data Structures syllabus

Arrays and Pointers (part 2) Be extra careful with pointers!

CSC 2400: Computer Systems. Arrays and Strings in C

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Arrays and Pointers (part 2) Be extra careful with pointers!

CS61, Fall 2012 Section 2 Notes

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

Subject: Fundamental of Computer Programming 2068

Pointers and File Handling

Department of Electrical and Computer Engineering The University of Texas at Austin

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

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

Data Representation and Storage. Some definitions (in C)

Advanced Pointer Topics

C PROGRAMMING Lecture 5. 1st semester

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Answer all questions. Write your answers only in the space provided. Full marks = 50

Arrays and Pointers. CSE 2031 Fall November 11, 2013

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Arrays, Pointers and Memory Management

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CS240: Programming in C

C programming basics T3-1 -

Lesson #8. Structures Linked Lists Command Line Arguments

CMPE-013/L. Introduction to C Programming

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Pointers, Dynamic Data, and Reference Types

CSE 124 Discussion (10/3) C/C++ Basics

CS240: Programming in C

Self-referential Structures and Linked List. Programming and Data Structure 1

CS349/SE382 A1 C Programming Tutorial

C programming for beginners

Introduction to Data Structures. Systems Programming

Character Strings. String-copy Example

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Arrays and Pointers in C. Alan L. Cox

( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING UNIT V 2 MARKS

Quick review pointer basics (KR ch )

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

211: Computer Architecture Summer 2016

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

PROGRAMMAZIONE I A.A. 2017/2018

Memory Allocation. General Questions

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

COMP2521. Generic ADTs in C

POINTER AND ARRAY SUNU WIBIRAMA

Call The Project Dynamic-Memory

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

CSC231 C Tutorial Fall 2018 Introduction to C

LSN 3 C Concepts for OS Programming

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

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Transcription:

Memory Allocation

Memory What is memory? Storage for variables, data, code etc. How is memory organized? Text (Code) Data (Constants) BSS (Global and static variables) Text Data BSS Heap Stack (Local variables) Heap (Dynamic Memory) Stack

Memory Allocation int isize; BSS char *f(){ char *p; isize = 8; p = malloc(isize); return p; } Stack Data Heap

Memory Allocation How memory is allocated? Global and static variables = program start up Local variables = function call Dynamic memory = malloc() / calloc()

Memory Allocation int isize; Allocated in BSS, set to 0 at startup char *f(){ char *p; isize = 8; p = malloc(isize); return p; } Allocated on Stack at start of function f Allocated in Heap by malloc

Memory Deallocation How memory is deallocated? Global and static variables = program finish Local variables = function return Dynamic memory = free() All memory is deallocated at program termination Good practice to use free()

Memory Initialization Local variables do not have any initialization Memory allocated by malloc has no initialized value Memory allocated by calloc is initialized to 0 by default Global and static variables are initialized to 0 by default

Two new data types Structure Union

Structure A variable in C can hold only one data of one type (eg. int a, float b, char c etc.) An array can hold group of data of same data types (eg. int a[5]) What is structure? Collection of different data types

Structure Syntax struct point{ }; int x; int y; Declaring structure variable struct point p; Declaring structure using pointer variable struct point *p;

Structure Use typedef to rename a data type struct point p1; struct point p2; or typedef struct point point; point p1; point p2;

Structure Example #include <stdio.h> #include <string.h> typedef struct student{ int id; char name[20]; float percentage; }student; int main() { student record = {0}; //Initializing to null record.id=1; strcpy(record.name, "XYZ"); record.percentage = 86.5; printf(" Id is: %d \n", record.id); printf(" Name is: %s \n", record.name); printf(" Percentage is: %f \n", record.percentage); return 0; }

Structure Example #include <stdio.h> #include <string.h> struct student { int id; char name[20]; float percentage; }; void func(struct student *record){ printf(" Id is: %d \n", record->id); printf(" Name is: %s \n", record->name); printf(" Percentage is: %f \n", record->percentage); } int main() { struct student record; record.id=1; strcpy(record.name, "XXX"); record.percentage = 86.5; func(&record); return 0; }

Structure memory allocation #include <stdio.h> #include <string.h> /* Below structure1 and structure2 are same. They differ only in member's allignment */ struct structure1 { int age; int id; char namefirstletter; char surnamefirstletter; float percentage; }; struct structure2 { int age; char namefirstletter; int id; char surnamefirstletter; float percentage; };

Structure memory allocation struct structure1 { int age; int id; char namefirstletter; char surnamefirstletter; float percentage; }; struct structure2 { int age; char namefirstletter; int id; char surnamefirstletter; float percentage; }; What is expected? 4 4 1 1 4 4 1 4 1 4

Structure memory allocation int main() { struct structure1 a; struct structure2 b; printf("size of structure1 in bytes : %d\n", sizeof(a)); printf ( "\n Address of age = %u", &a.age ); printf ( "\n Address of id = %u", &a.id ); printf ( "\n Address of namefirstletter = %u", &a.namefirstletter ); printf ( "\n Address of surnamefirstletter = %u", &a.surnamefirstletter ); printf ( "\n Address of percentage = %u", &a.percentage ); } printf(" \n\nsize of structure2 in bytes : %d\n", sizeof(b)); printf ( "\n Address of age = %u", &b.age ); printf ( "\n Address of namefirstletter = %u", &b.namefirstletter ); printf ( "\n Address of id = %u", &b.id ); printf ( "\n Address of surnamefirstletter = %u", &b.surnamefirstletter ); printf ( "\n Address of percentage = %u", &b.percentage ); getchar(); return 0;

Structure memory allocation Output: size of structure1 in bytes : 16 Address of age = 3057402288 Address of id = 3057402292 Address of namefirstletter = 3057402296 Address of surnamefirstletter = 3057402297 Address of percentage = 3057402300 size of structure2 in bytes : 20 Address of age = 3057402256 Address of namefirstletter = 3057402260 Address of id = 3057402264 Address of surnamefirstletter = 3057402268 Address of percentage = 3057402272

Structure memory allocation age id surname (4 bytes) struct structure1 { int age; int id; char namefirstletter; char surnamefirstletter; float percentage; };

Structure memory allocation age name surname struct structure2 { int age; char namefirstletter; int id; char surnamefirstletter; float percentage; };

Union C Union is also like structure, collection of different data types Each element in a union is called member. Union and structure in C are same in concepts, except allocating memory for their members. Structure allocates storage space for all its members separately Union allocates one common storage space for all its members We can access only one member of union at a time This is because, Union allocates one common storage space for all its members. Many union variables can be created in a program and memory will be allocated for each union variable separately

Union union student{ }; int mark; char name[6]; double average; For above union, only 8 bytes of memory will be allocated since double data type will occupy maximum space of memory over other data types. Total memory allocation = 8 Bytes

Command Line Argument

Command Line Argument int main(int argc, char **argv){} argc : count of argument passing through command line argv : arguments By default argc = 1; argv[0] = the name by which the program was called

Command Line Argument #include <stdio.h> #include <string.h> int main(int argc, char **argv){ } int i; printf ("%d", argc); for (i = 0; i < argc; i++) printf ("\nargv[%d]%s", i, argv[i]); printf("\n"); Run :./a.out Indian Statistical Institute

Example of Union void testendian(){ } union xx{ int myint; char val; } xunion; xunion.myint = 1; if (xunion.val) printf ( Little Endian ); else printf ( Big endian );