Passing the course of operating systems and trying to implement the paging memory algorithm. The algorithm has a limited number of pages ( NUMBER_PAGEBLE_PAGES = 33 ) and a mem_map_t structure containing a pointer to the first element of each page on the uint32_t *page .
Below is the code that is supposed to contain a local pointer that receives the address of the free page, and then, starting from the received address, performs a sequential initialization of the entire address space of this page with the value 0:
static uint32_t *allocate_page(void) { uint32_t i = 0; uint32_t j = 0; uint32_t *page = NULL; printf("here 2\n"); while (i < NUM_PAGEABLE_PAGES) { if (m_map[i].in_use == 0) { m_map[i].in_use = 1; page = m_map[i].page; printf("here 3 %p\n", page); // Данная строка выдает segmentation fault 11 при инициализации ячейки памяти по указанному адресу. Почему? *page = 0; printf("here 4 %p\n", page); // данный "while-loop" пытается инициализировать все остальные ячейки в указанном while (j < 1024) { j++; printf("here 5\n"); page[j] = 0; printf("here 6\n"); } break; } i++; } return page; } Вывод в терминале следующий $ ./a.out here 2 here 3 0x100000 Segmentation fault: 11 It turns out that the local pointer uint32_t *page gets the initial value of 0x100000, but when initializing the memory cell at the specified address, the value 0 gives an error.
Maybe there are ideas what is the matter here? Thank you in advance!
page = m_map[i].page;This means that you do not store allocated memory or insufficiently allocated memory. Is it really there that stands out 1025 ?? After all, you start the cycle with index 1! Try to put the iterations after accessing the memory, if it was not so strangely conceived. And do not put j to zero before the cycle. And this is intended? - AivanF.