C: Memory
2 min readApr 29, 2024
Memory manipulations are hidden in their own ways in various OS. In defensive C, engineers wants to control them in embedded systems for defensive reasons.
Test usages for different test cases:
test_usage_check_if_within_memory_bounds()
test_usage_work_area()
test_usage_check_on_malloc_success_and_location()
test_usage_of_memcpy()
test_usage_write_a_byte_to_register_with_specific_memory_address ()
test_usage_memset_different_from_cpy_due_to_null_byte ()
In embedded systems, ensuring memory zeroization can be critical.
// ========
/*
c to malloc
put some data into array
get the address
then show that after delete, use the memory pointer to show that the data is not zeroize
and also give a zeroization function by pointer and length to clear
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to zeroize memory pointed to by 'ptr' for 'size' bytes
void zeroize(void *ptr, size_t size) {
memset(ptr, 0, size);
}
void show_data(int *data, size_t data_length) {
for (int i = 0; i < data_length; i++) {
printf("%d\n", data[i]);
}
}
int main() {
int *data; // Pointer to integer array
int data_length = 5;
// Allocate memory for an integer array of size <data_length>
data = (int *)malloc(data_length * sizeof(int));
if (data == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
// Initialize the array with some…