0% found this document useful (0 votes)
5 views62 pages

8 Malloc

The document discusses the use of void pointers in C programming, demonstrating how to store and access different data types using a void pointer. It also covers dynamic memory allocation using malloc and calloc, including examples of reallocating memory for an array of integers. Finally, it emphasizes the importance of freeing allocated memory to prevent memory leaks.

Uploaded by

Raghav Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views62 pages

8 Malloc

The document discusses the use of void pointers in C programming, demonstrating how to store and access different data types using a void pointer. It also covers dynamic memory allocation using malloc and calloc, including examples of reallocating memory for an array of integers. Finally, it emphasizes the importance of freeing allocated memory to prevent memory leaks.

Uploaded by

Raghav Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Void Pointer

#include <stdio.h>
Integer value = 10
int main()
Float value = 5.5
{
Character value = A
int a = 10;
float b = 5.5;
char c = 'A';

void *p;

p = &a;
printf("Integer value = %d\n", *(int *)p);

p = &b;
printf("Float value = %.1f\n", *(float *)p);

p = &c;
printf("Character value = %c\n", *(char *)p);

return 0;
}
Example
int *p = (int*)malloc(sizeof(int));
scanf("%d", &ptr[i]);
Accessing Memory with Pointers scanf("%d", ptr + i);

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

int main() {
int *a = (int *)malloc(3 * sizeof(int));
int *b = (int *)calloc(3, sizeof(int));

printf("malloc values: %d %d %d\n", a[0], a[1], a[2]);


printf("calloc values: %d %d %d\n", b[0], b[1], b[2]);

free(a);
free(b);
return 0;
}

output
malloc values: garbage garbage garbage
calloc values: 0 0 0
int main() {
int i;
int *ptr;
int initial_size = 2;
int new_size = 4;
// Allocate memory for the initial 2 integers
ptr = (int *)malloc(initial_size * sizeof(int));

// Check for allocation failure


if (ptr == NULL) {
printf("Memory not available!\n");
exit(1);
}
printf("Enter the two numbers:\n");
for (i = 0; i < initial_size; i++) {
// Read into ptr[0] and ptr[1]
scanf("%d", ptr + i);
}

// --- 3. Reallocation for 4 Integers (Extending the Array) ---


printf("\n// Memory allocation for 2 more integers (Reallocating to size 4)\n");

// The current size is 2. We reallocate to new_size (4).


// realloc safely increases the block size, preserving the original data (the first 2 numbers).
ptr = (int *)realloc(ptr, new_size * sizeof(int));
// Check for re-allocation failure
if (ptr == NULL) {
printf("Memory not available!\n");
exit(1);
}

// --- 4. Reading the Next 2 Integers ---


printf("Enter 2 more integers (to fill slots 2 and 3):\n");
for (i = initial_size; i < new_size; i++) {
// Start reading from index 2 (the newly available space)
scanf("%d", ptr + i);
}
// --- 5. Printing the Final 4 Values ---
printf("\n// Printing the values on the screen\n");
printf("The 4 integers are: ");
for (i = 0; i < new_size; i++) {
printf("%d ", *(ptr + i));
}
printf("\n");

// --- 6. Cleanup ---


// Free the dynamically allocated memory
free(ptr);
ptr = NULL; // Prevent dangling pointer

return 0;
}
Linked list

You might also like