Dynamic memory allocation and Intraprogram Communication

Slides:



Advertisements
Similar presentations
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Advertisements

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.
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 Classes.
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.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
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.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
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.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
Functions. Flow of Control Review while for do while goto break & continue switch Relational operators short circuits.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Dynamic memory allocation and Intraprogram Communication.
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.
Pointers verses Variables
Functions Course conducted by: Md.Raihan ul Masood
The Three Attributes of an Identifier
Functions and Pointers
C Functions -Continue…-.
Functions and Structured Programming
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
The Three Attributes of an Identifier
Chapter 12 Variables and Operators
CSE1320 Loop Dr. Sajib Datta
Functions Dr. Sajib Datta
Storage class in C Topics Automatic variables External variables
Functions.
Pointers.
Day 04 Introduction to C.
Module 5 Sorting and Searching: Bubble sort Selection sort
Command-Line Arguments
2-D arrays a00 a01 a02 a10 a11 a12 a20 a21 a22 a30 a31 a32
Programmazione I a.a. 2017/2018.
Functions and Pointers
CSC 253 Lecture 8.
CSC 270 – Survey of Programming Languages
Semantics CSE 340 – Principles of Programming Languages Spring 2016
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
C Storage classes.
Outline Defining and using Pointers Operations on pointers
Scope Rules and Storage Types
Incremental operators
Dr Tripty Singh Tutorial for Fuctions
ECE 103 Engineering Programming Chapter 12 More C Statements
Scope Rules Of Variables
Dynamic Memory.
In C Programming Language
Programming Languages and Paradigms
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.
Functions.
Storage Classes.
Presentation transcript:

Dynamic memory allocation and Intraprogram Communication

Pointers as 1D array #include <stdio.h> #include<stdlib.h> int main(void) { char *array; int len = 20,i=0; array = (char *)calloc(len+1, 1); fgets(array,21,stdin); 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 <stdio.h> #include<stdlib.h> 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(j=0;j<col;j++) scanf("%d", &array[i][j]); } printf("%d ", array[i][j]); printf("\n"); free(array[i]); free(array); return 0;

#include <stdio.h> 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 <stdio.h> 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 <stdio.h> 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 <stdio.h> 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 <stdio.h> 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.

#include<stdio.h> void output(); int main() { printf("%d.\n",x); //error output(); return 0; } int x=5; void output() printf("In the output, %d.\n",x);

A global variable is in existence during the full execution time of the program.

Communication across files Functions in separate files share the same variable. Define a global variable in one file, and other files can declare (can not define) this same variable and give it the storage class extern. Note you can only define a variable once and for all other places, you can declare.

Driver.c #include <stdio.h> int x = 5; extern void output(); extern void incr(); int main(void) { output(); incr(); return 0; }

Output.c #include<stdio.h> extern int x; void output() { printf("The value of x is %d.\n", x); }

Incr.c extern int x; void incr() { x++; }

The value of x is 5. The value of x is 6.