Presentation is loading. Please wait.

Presentation is loading. Please wait.

By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)

Similar presentations


Presentation on theme: "By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)"— Presentation transcript:

1 by Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)

2 Types of Memory Allocation  Global, Static  Stack or Automatic or Local.  Process Heap or dynamic allocation. Note: register variables are not relevant to this discussion SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)

3 Global and Static variables  They are part of the binary.  When the binary loads into memory by OS they also get allocated in the process address space.  First ones to get allocated.  Life - till the end of the program or unload of binary ( to be very strict )  Size set by complier.  Small allocations as size may affect the size of binary and more work for loader. SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)

4 Stack variables  On the execution thread stack.  Managed by stack pointer.  Life is till the stack pointer decrement which mean return from the function.  Very fast allocation.  Normally size set by complier at compile time.  Small allocation otherwise stack- overflow. SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)

5 Dynamic or heap allocation.  Run time allocation.  Heap inside the process.  Heap is nothing but a pool or list of allocated chunks of memory in the process address space.  Allocation done by functions in C like calloc or malloc.  This method is to be used for large allocation.  Life until you call free call on the same pointers.  Slow for allocation compared to stack. SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)

6 Lets think about implementing below logic. #include void main() { int amountToAllocate; scanf ( “%d”, &amountToAllocate ); int allocateMemory[amountToAllocate]; } What is the problem? How it can be done? SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)

7 Correct implementation #include void main() { int amountToAllocate; scanf ( “%d”, &amountToAllocate ); int *allocatedMemory = malloc(amountToAllocate); //use allocatedMemory free ( allocatedMemory ); } SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)

8 Some useful related functions.  memcpy  memset Note: Some are unsecure to use in production code but we are not getting into those intricacies at this point and focusing on language, concepts and functionality. SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)

9 Demo  Global, static  on stack arrays  dynamically allocated arrays  memset, memcpy  vmmap SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)

10 Thank you SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)


Download ppt "By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)"

Similar presentations


Ads by Google