Upcoming SlideShare
×

# Stack & heap

195
-1

Published on

Published in: Engineering, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
195
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
5
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Stack & heap

2. 2. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
3. 3.  Stack is a special region of computer's memory that stores temporary variables created by each function (including the main() function).The stack is a "FILO" (first in, last out) data structure, that is managed and optimized by the CPU quite closely. Every time a function declares a new variable, it is "pushed" onto the stack.Then every time a function exits, all of the variables pushed onto the stack by that function, are freed (that is to say, they are deleted). Once a stack variable is freed, that region of memory becomes available for other stack variables.
4. 4.  the stack grows and shrinks as functions push and pop local variables  there is no need to manage the memory yourself, variables are allocated and freed automatically  the stack has size limits  stack variables only exist while the function that created them, is running
5. 5. main() { Int a=10,b=20,c; c=sum(a , b); } int sum(int a , int b) { int c; c=a+b; return c; } STACK Main() a=10 b=20 c ; sum() a =10 b=20 C=30
6. 6.  The heap is a region of computer's memory that is not managed automatically , and is not as tightly managed by the CPU. It is a more region of memory .  To allocate memory on the heap, you must use malloc() or calloc(), which are built-in C functions. Once you have allocated memory on the heap, you are responsible for using free() to deallocate that memory once you don't need it any more. If you fail to do this, your program will have what is known as a memory leak.That is, memory on the heap will still be set aside (and won't be available to other processes).
7. 7.  malloc() Allocates requested size of bytes and returns a pointer first byte of allocated space  calloc() Allocates space for an array elements, initializes to zero and then returns a pointer to memory  free() deallocate the previously allocated space  realloc() Change the size of previously allocated space
8. 8. malloc()  Syntax malloc(size in bytes);  This function allocates ‘size’ byte of memory and returns a pointer to the first byte or NULL if there is some kind of error  But we need a pointer to point to the allocated space in heap  int *p; p=malloc(2);
9. 9. int *p; p=malloc(2); //allocates 2 bytes of memory p=malloc(sizeof(int)); //sizeof() returns size of any data type so here it will be malloc(2) p=(int *)malloc(2); //malloc always return void * pointer . So you may type cast it into any data type. but it is unnecessary as the compiler does it for us p=(int *)malloc (sizeof(int)); //allocates 2 bytes of memory
10. 10.  Calloc() stands for "contiguous allocation”  Allocates multiple blocks of memory each of same size and sets all bytes to zero  Syntax calloc(number of elements , size in bytes); we need a pointer to point to the allocated space in heap
11. 11. int *p; p=calloc(2,2); //allocates 2 block of 2 bytes memory p=calloc(3,sizeof(int)); //sizeof() returns size of any data type so here it will be calloc(3,2) p=(int *)calloc(2,10); //calloc always return void * pointer . So you may type cast it into any data type. but it is unnecessary as the compiler does it for us p=(int *)calloc (3,sizeof(int)); //allocates 3 block of 2 bytes memmory
12. 12. MALLOC() • Allocates "size" bytes of memory. • The contents of allocated memory are not changed. i.e., the memory may contains garbage values. • returns void pointer (void *). If the allocation succeeds, a pointer to the block of memory is returned CALLOC() • Allocates a region of memory large enough to hold "n elements" of "size" bytes each. • The allocated region is initialized to zero. • returns void pointer (void *). If the allocation succeeds, a pointer to the block of memory is returned.
13. 13. realloc( pointer name , new size ); It will realloc the size of memory free( pointer name ); De allocate the memory
14. 14. #include <stdio.h> #include <stdlib.h> double* multiplyByTwo (double *input) { double *twice = malloc(sizeof(double)); *twice = *input * 2.0; return twice; } int main () { int *age = malloc(sizeof(int)); *age = 30; double *salary = malloc(sizeof(double)); *salary = 12345.67; double *myList = calloc(3 , sizeof(double)); myList[0] = 1.2; myList[1] = 2.3; myList[2] = 3.4; double *twiceSalary = multiplyByTwo(salary); printf("double your salary is %.3fn", *twiceSalary); free(age); free(salary); free(myList); free(twiceSalary); return 0; }