Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.

Similar presentations


Presentation on theme: "1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to."— Presentation transcript:

1 1 Dynamic Memory Allocation

2 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to allocate memory dynamically Determine size at run time. Allocate at run time.

3 3 Dynamic Memory Allocation Why allocate memory dynamically? In many cases, we can’t predict in advance what the program will need. May depend on data not known at compile time. May vary dramatically from one run to another. Needed to implement dynamic data structures like linked lists

4 4 malloc To allocate memory at run time, call the library function malloc(). memory allocation. Function prototype: void* malloc (size_t sz); Number of bytes to allocate (positive integer) A generic pointer Can be used as any pointer type. Optionally typecast as desired pointer type.

5 5 malloc malloc returns a pointer to the memory that it allocated. Store the returned value in a pointer so that you can use it. malloc can fail! Returns NULL if it cannot allocate the requested amount of memory. Always check for NULL being returned. Normally have to abort the program if this happens.

6 6 malloc The function declaration for malloc is in stdlib.h. You will need #include

7 7 Pointers and Arrays When an array declaration like int A[10] is executed, the compiler allocates a block of 10*sizeof(int) bytes; then sets A equal to the address of the first byte of that block Thus the value of A is just &A[0]. Recall that an array variable may not be the left-hand side of an assignment statement Thus, A is effectively a constant pointer

8 Pointers and Arrays An array name A is effectively a constant pointer Recall that the compiler computes the address of A[i] as A + i*sizeof(sizeof(A[0])) It will do the same with any pointer p in place of A : If p is a pointer, then p[i] is a variable with address p + i*sizeof(*p) Given int A[5] = {5,3,7,1,4}, i; int* p = A; The following are equivalent: for(i=0;i<5;++i) printf(“%d”,A[i] for(i=0;i<5;++i) printf(“%d”,p[i]

9 Dynamic Arrays We can create an object that acts like an array during program execution as follows: int n, i; int *p; scanf(“%d”,&n); p = malloc(n*sizeof(int)); assert(p != NULL); /* p may now be used as if it were an array name */ for(i = 0; i < n; ++i) scanf(“%d”,&p[i])

10 10 Example Read a sentence from the keyboard using a large input buffer. Allocate memory to hold the sentence just large enough to hold what was entered. Copy the sentence into the dynamically allocated memory block.

11 11 Example #include int main() { char input_buffer[1000]; int length = 0; char* sentence = NULL; printf ("Please type a sentence:\n"); fgets(input_buffer, 1000, stdin); printf ("You typed: \n%s\n\n", input_buffer);

12 12 Example length = strlen(input_buffer); printf ("Allocating %d bytes to hold the sentence\n\n", length+1); sentence = malloc(length+1); assert (sentence != NULL); strcpy (sentence, input_buffer); printf ("The sentence in dynamically allocated memory:\n"); printf ("%s\n", sentence); getchar(); /* Not needed */ getchar(); /* when using CLUE */ return 0; } why +1 ?

13 13 Program Running

14 14 Things to Notice Always check the pointer returned by malloc to be sure the malloc was successful. We can make the dynamically allocated memory block any type that we need. Just copy the void* pointer returned by malloc() into a pointer of the desired type. (Some older compilers may require a typecast.)

15 15 Some C Standard Library Functions for Dynamic Memory Allocation void* malloc (size_t sz ); Returns pointer to uninitialize block of memory of specified size (in bytes) void* calloc (size_t n, size_t sz ); Allocate and clear. Returns pointer to an array of n entries, each of size sz bytes, cleared to binary 0’s. void free (void* pt ); Deallocate block at address pt. Must be a block allocated with malloc or calloc We will discuss this next slides.


Download ppt "1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to."

Similar presentations


Ads by Google