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

/* program to find area of a ring */ #include int main() { float a1,a2,a,r1,r2; printf("Enter the radius : "); scanf("%f",&r1); a1 = 3.14*r1*r1; printf("Enter.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
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);
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
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.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
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.
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.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Dynamic memory allocation and Intraprogram Communication.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
Pointers verses Variables
Stack and Heap Memory Stack resident variables include:
Functions and Pointers
C Functions -Continue…-.
C Scope Rules and Arrays
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
The Three Attributes of an Identifier
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
Programmazione I a.a. 2017/2018.
Command-Line Arguments
Module 2 Arrays and strings – example programs.
2-D arrays a00 a01 a02 a10 a11 a12 a20 a21 a22 a30 a31 a32
Programmazione I a.a. 2017/2018.
Dynamic memory allocation and Intraprogram Communication
Some examples.
Functions and Pointers
CSC 270 – Survey of Programming Languages
Scope, Parameter Passing, Storage Specifiers
Programming and Data Structures
CNG 140 C Programming (Lecture set 8)
Pointers and dynamic memory
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Dynamic Memory Allocation
Declaration, assignment & accessing
C Storage classes.
Outline Defining and using Pointers Operations on pointers
Incremental operators
Dr Tripty Singh Tutorial for Fuctions
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Scope Rules Of Variables
In C Programming Language
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Programming Languages and Paradigms
Arrays, Pointers, and Strings
STORAGE CLASS.
ICS103 Programming in C Lecture 12: Arrays I
C Programming Lecture-17 Storage Classes
More Loops Topics Counter-Controlled (Definite) Repetition
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;

Example Creating a 2D array that stores 5 different words of size 5. int main() { int i; char **two_d_array; two_d_array = (char **) malloc (5 * sizeof(char *) ); for(i = 0; i < 5; i++) { printf("Enter Word %d :", i+1); two_d_array[i] = (char *) malloc (5 * sizeof(char) ); scanf("%s", two_d_array[i]); printf("\n"); } printf("Printing The Words...\n"); for(i = 0; i < 5; i++) printf("Word %d :%s\n", i+1, two_d_array[i]); 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.