Functions with heap and stack....by thanveer danish
Click here to load reader
-
Upload
muhammed-thanveer-danish-melayi -
Category
Software
-
view
63 -
download
0
description
Transcript of Functions with heap and stack....by thanveer danish
![Page 1: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/1.jpg)
![Page 2: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/2.jpg)
Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
![Page 3: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/3.jpg)
Functions with heap and stack
MUHAMMED [email protected]/usernametwitter.com/usernamein.linkedin.com/in/profilename9526960445
![Page 4: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/4.jpg)
There are two ways momery can allocated in c
By declaring variables (stack)By explicitly requesting space from c(heap)
![Page 5: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/5.jpg)
It is the place where all the variables that are declared and initialized before run time are stored
Stack
The advantage of using the stack to store variables, is that memory is managed for you. You don't have to allocate memory by hand, or free it once you don't need it any more. What's more, because the CPU organizes stack memory so efficiently, reading from and writing to stack variables is very fast.
oIt is follows last in first out order
![Page 6: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/6.jpg)
ExampleMain(){Int a=10, b=20, c;C=sum( a,b);}Int sum(int a,int b){Int c;C=a+b;Return c;}
![Page 7: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/7.jpg)
Heap
Heap is a area of memory used for dynamically memory allocation
Programmer allocates memory mannually at heapTo allocate memory using by calloc ,malloc and realloc functionsMalloc and calloc ‘s purpose same…but realloc allocation to adjust it after allocating memory..
![Page 8: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/8.jpg)
example#include <stdio.h>#include <string.h>#include <stdlib.h> int main(){ char *mem_allocation; /* memory is allocated dynamically */ mem_allocation = malloc( 20 * sizeof(char) ); if( mem_allocation== NULL ) { printf("Couldn't able to allocate requested memory\n"); } else { strcpy( mem_allocation,“[email protected]"); } printf("Dynamically allocated memory content : " "%s\n", mem_allocation ); free(mem_allocation); }
![Page 9: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/9.jpg)
Recursive functionRecursion is the process of repeating items in a self-similar way. Same applies in programming languages as well where if a programming allows you to call a function inside the same function that is called recursive call of the function
Example
void main(){printf(“recursive function called.\n”);main();}
![Page 10: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/10.jpg)
A recursive function is a function which calls itself.The speed of a recursive program is slower because of stack overheads. (This attribute is evident if you run above C program.)A recursive function must have recursive conditions, terminating conditions, and recursive expressions.
![Page 11: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/11.jpg)
If this presentation helped you, please visit our page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
![Page 12: Functions with heap and stack....by thanveer danish](https://reader038.fdocuments.us/reader038/viewer/2022100600/555e34b6d8b42aad058b46c9/html5/thumbnails/12.jpg)
Contact Us
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Start up VillageEranakulam,Kerala, India.
Email: [email protected]