C Programs

C Program on Managing memory using allocation and release

In this program you will learn Managing memory using allocation and release in a c program.

Also See : C Program on Using a pointer variable

#include<stdio.h>
#include<stdlib.h>

int main(void) {
	//Static arrays waste memory
	//Guaranteed to allocate 20 integers
	int staticArray[20];

	//Dynamic arrays save memory by creating a pointer that stores
	//the beginning of the array
	int *dynamicArray = malloc(20 * sizeof(int));
	*dynamicArray = 10;
	printf("Address %x stores value %d\n", dynamicArray, *dynamicArray);

	dynamicArray[1] = 20;
	printf("dynamicArray[1] stores value %d\n", dynamicArray[1]);
	printf("The size of dynamicArray is %d\n", sizeof(dynamicArray));

	// Release unused memory:
	free(dynamicArray);

	return EXIT_SUCCESS;
}

 

Join The Discussion