Dynamic Memory Allocation
An Example of DMA in C
// Implementation of the malloc function to create an int array
int* i = (int*) malloc( 10 * sizeof(int));
if (i==NULL){
printf("ERROR: unsuccessful allocation\n");
return -1; // End the proram
}
// Accessing this array through pointer arithmetic
*(i + 1) = 3; // Store the number 3 in the 2nd position
printf("%d", i + 0); // Print the address of the 1st int
printf("%d", *(i)); // Print the value of the 1st int
// Accessing this array through array syntax
i[1] = 3; // Store the number 3 in the 2nd position
printf("%d", &i[0]); // Print the address of the 1st int
printf("%d", i[0]); // Print the value of the 1st int
// Free up the allocated space in memory
free(i);
i = NULL; // Make sure the i pointer is not left hanging!
Last updated
Was this helpful?