Dynamic memory allocation and Intraprogram Communication.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Objects Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
18. DECLARATIONS.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Scope: Portion of the program in which the identifier can be referenced. Various types of scope, my examples are of block scope and global scope.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Functions. Flow of Control Review while for do while goto break & continue switch Relational operators short circuits.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
Pointers.
Dynamic memory allocation and Intraprogram Communication
CSC 253 Lecture 8.
Chapter 5 - Functions Outline 5.1 Introduction
Dynamic memory allocation and Intraprogram Communication
Scope, Parameter Passing, Storage Specifiers
CSC 253 Lecture 8.
Local Variables, Global Variables and Variable Scope
Outline Defining and using Pointers Operations on pointers
Incremental operators
Chapter 7: User-Defined Functions II
ECE 103 Engineering Programming Chapter 12 More C Statements
Scope Rules Of Variables
In C Programming Language
STORAGE CLASS.
STORAGE CLASS.
C Programming Lecture-17 Storage Classes
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Storage Classes.
Presentation transcript:

Dynamic memory allocation and Intraprogram Communication

Pointers as 1D array #include int main(void) { char *array; int len = 20,i=0; array = (char *)calloc(len+1, 1); gets(array); printf("%s\n", array); while(array[i]) printf("%c", array[i++]); free((void *)array); printf("\n%s", array); return 0; }

Pointers as 2D array #include int main(void) { int **array; int row=5, col=3; int i,j; array = (int **)malloc(row*sizeof(int *)); for(i=0;i<row;i++) array[i] = (int *)malloc(col * sizeof(int)); for(i=0;i<row;i++) { for(j=0;j<col;j++) { scanf("%d", &array[i][j]); } for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("%d ", array[i][j]); } printf("\n"); } for(i=0;i<row;i++) { free(array[i]); } free(array); return 0; }

Study Reference C By Discovery 4.10 and 11.5 Teach yourself C Chapter 5,6

#include int increment(); int main(void) { printf("%d\n", increment()); return 0; } int increment() { int number = 0; return ++number; }

Output is ▫1 No storage specifier for number so it’s automatic, so number is created and initialized each time increment() is called. The value of number will be zero each time increment() starts executing.

Static variables A static variable exits the whole time the program is executing. To declare a static variable ▫static int counter; Without explicit initialization, a static variable will be given the default initial value. [0] static int counter = 1;

#include int increment(); int main(void) { printf("%d\n", increment()); return 0; } int increment() { static int number = 0; return ++number; }

Output is ▫1 ▫2 number is just initialized once when increment() is called the first time and holds its value between classes to increment().

Scope of an identifier The scope of an identifier determines which part of the program will know about the identifier and be able to use it. Local and global variables

Local variables A local variable is local to a block which may be a function block or a compound statement block. It’s declared inside the braces for the block and only be referenced inside its block. A local variable’s scope is from its point of declaration to the end of the block in which it was declared.

Example of local variables #include int main(void) { int a = 10; { int b = 8; } printf("%d\n", b); return 0; }

An error in the previous example: ▫error C2065: 'b' : undeclared identifier

Another example #include int addit(); int main(void) { int counter; for(counter = 1; counter < 5; counter++) { int i; i = counter; i = i + 5; addit(); } printf("i was %d; addit() returned %d.\n", i, addit()); return 0; } int addit(void) { int sum = 0; sum += counter; return sum; }

Another example #include int addit(); int main(void) { int counter; for(counter = 1; counter < 5; counter++) { int i; i = counter; i = i + 5; addit(); } printf("i was %d; addit() returned %d.\n", i, addit()); //first error: error C2065: 'i' : undeclared identifier return 0; } int addit(void) { int sum = 0; sum += counter; //second error: error C2065: 'counter' : undeclared identifier return sum; }

Global variables A global variable is declared outside all function blocks. It’s possible to access a global variable in more than one functions. If a global variable is declared before all the functions in a source file, it can be referenced by all the functions in that file. The scope of a global variable declared either before the functions in a file or between two functions in a file is from its point of declaration to the end of the file.

A global variable is in existence during the full execution time of the program. It has a default value if no initializer is given.