File: memory_status.c

package info (click to toggle)
chibicc 1.0.23.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,832 kB
  • sloc: ansic: 62,911; sh: 275; makefile: 92
file content (52 lines) | stat: -rw-r--r-- 1,235 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

#include <stdlib.h>
#include "test.h"

#define MEM_SIZE 100 // Define the size of the memory block

void printMemoryStatus(char *memory, int size);

int main() {
    char *memory = (char *)malloc(MEM_SIZE * sizeof(char)); // Allocate memory block
    if (memory == NULL) {
        printf("Memory allocation failed. Exiting.\n");
        return 1;
    }

    // Initially, mark all memory as free
    for (int i = 0; i < MEM_SIZE; ++i) {
        memory[i] = 'F';
    }

    // Example usage: Allocating memory at address 10 to 20
    for (int i = 10; i < 20; ++i) {
        memory[i] = '*';
    }

    // Print the memory status
    printMemoryStatus(memory, MEM_SIZE);

    // Freeing memory at address 10 to 20
    for (int i = 10; i < 20; ++i) {
        memory[i] = 'F';
    }

    // Print the memory status after freeing memory
    printMemoryStatus(memory, MEM_SIZE);

    // Free dynamically allocated memory
    free(memory);

    return 0;
}

// Function to print memory status
void printMemoryStatus(char *memory, int size) {
    printf("Memory Status:\n");
    for (int i = 0; i < size; ++i) {
        printf("memory[%d]=%c ", i, memory[i]);
        if (i < 10 )
            ASSERT(70, memory[i]);
    }
    printf("\n");
}