Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack and Heap Memory Stack resident variables include:

Similar presentations


Presentation on theme: "Stack and Heap Memory Stack resident variables include:"— Presentation transcript:

1 Stack and Heap Memory Stack resident variables include:
Parameters passed to functions Variables declared inside basic blocks that are not declared static. The size of the variables must be known at compile time.

2 Stack and Heap Memory Stack resident variables are:
Created to "push on to" the stack when a basic block that contains them (i.e. function) is activated. Disappear from, "popped off". the stack when the function is done. Those areas of memory used by variables when a basic block was activated may then be overwritten after exiting from that block.

3 Stack and Heap Memory Heap resident variables include:
Variables declared outside of all functions. Variables declared inside basic building blocks that are declared static. Memory areas dynamically allocated at run time with malloc() or calloc().

4 Stack and Heap Memory Storage for declared heap resident variables is:
Assigned at the time a program is loaded, and Remains assigned for the life of a program. Storage allocated on the heap using malloc() remains allocated until: Either the free() is called, or The program ends.

5 Example: Stack and Heap Memory Variables
#include <stdio.h> #include <stdlib.h> int main() { int y; int *ptr; static int a; ptr = &y; *ptr = 99; printf("y is %d \nptr is %p \naddress of ptr is *%p \n", y, ptr, &ptr); ptr = &a; printf("the address of a is %p, ptr); return 0; OUTPUT: y is 99 ptr is 0x7fffca69b390 the address of a is 0x601030 notice this address is far removed from the two above

6 malloc() malloc() – memory allocation – function can be used to dynamically allocate an area of memory to be used at run time. Heap memory is used for these variables! Include <stdlib.h> whenever using malloc(). advantages of malloc(): Permits positioning the decision on the size of the memory block needed to store a large data type, such as a large (maybe sequence of) structure or an array. Permits using a section of memory for the storage of a large data type at one point in time, and then when that memory is not longer needed, it can freed up for other uses.

7 malloc() advantages of malloc() cont'd:
Is better when storing large data types, such as a large structure or array, because copying large data structures on the stack is very inefficient. Stack memory is a small finite space, therefore it is better to store large data structures in a heap to avoid stack overflows.

8 malloc() malloc() - parameter is the size of the area to be allocated in bytes. So, if you want to allocate space for 100 ints, then you must pass 400 to malloc() because each int occupies 4 bytes. Use the sizeof operator to find how big each data type is on a particular OS.

9 malloc() Example int *base = NULL; // declare and initialize an int pointer. Remember to initialize pointers before use. base = (int *) malloc(sizeof(sizeof(int) * 100); // typecast the function call to // malloc() to also be of type // int pointer and assign the // result of the function call to // base, which is the pointer // declared above Size of a single int Number of ints to be allocated if(base -- NULL) { fprintf(stderr, "malloc failed. Exitin\n"); exit (1); . . . // process // free(base) ; // remember to always free any dynamically allocated memory

10 calloc() The function calloc() works similar to malloc(), except calloc() takes two arguments that specify (separately): The number of elements to be reserved Size of each element in bytes Both malloc() and calloc() are declared in the standard header file <stdlib.h

11 More on the sizeof() operator
Used to determine the size of data elements reserved by calloc() or malloc(). Returns the size of the item in bytes. The sizeof operator can be used to give byte size for a variable, an array name, basic data type, name of a derived data type, or an expression. e.g. sizeof(int) // gives the number of bytes needed to store an int sizeof(x) // if x were assigned to an array of 100 integers, this would give the amount of storage (in bytes) required for` for 100 integers sizeof(struct pixelType) // would be the amount of storage required to store one pixel_t structure

12 calloc() vs malloc() Allocating enough storage in your program for 1000 integers: #include <stdio.h> int *intPtr; Using calloc, the function call looks like this: intPtr = (int *) calloc(sizeof(int), 1000); Using malloc, the function call looks like this intPtr = (int *) malloc(sizeof(int) * 1000); If you ask for more memory than the system has available, calloc (or malloc) returns a NULL pointer. Good programming practice is to test this pointer before using it: if(intPtr == NULL) { printf("calloc failed\n"); exit(1); }

13 calloc() vs malloc() Set all 1000 elements to -1: int *p;
for (p = intPtr; p < intPtr +1000; p++) *p = -1; }

14 Work with struct pixels
To reserve storage for n elements of type pixel_t, we first need to define a pointer to the appropriate type, e.g, pixel_t *pixlePtr; Use malloc to dynamically allocate memory for n pixel_t elements. pixelPtr - (pixel_t *) malloc(sizeof(pixel_t * n);

15 Work with struct pixels
After checking to make sure that pixel_t pointer is not NULL, you can assign values to the members or use the members in an expression. Note: since pixelPtr is a pointer to a structure, you can access its members in one of two ways: (*pixelPtr).r = 255; OR pixelPtr->r = 255; (*pixelPtr).g = 0; pixelPtr->g = 255; (*pixelPtr).b = pixelPtr->b = 255; Most students choose the syntax on the right to avoid putting the parentheses in the wrong place. *(pixelPtr).r is not valid and will generate an error message.


Download ppt "Stack and Heap Memory Stack resident variables include:"

Similar presentations


Ads by Google