[英]How to initialize and manipulate an address pointer
I'm trying to initialize a block of heap space into several smaller fixed size blocks without using malloc. 我正在尝试将一个堆空间块初始化为几个较小的固定大小的块而不使用malloc。 Each block points to the next block. 每个块指向下一个块。 Basically, it's a home-rolled linked list, not using malloc. 基本上,它是一个家庭滚动链表,不使用malloc。 The structure looks like so: 结构看起来像这样:
/******** Memory Table Entry Data Structure**********/
typedef struct
{
????? block_address; //<- What datatype should I use here?
void* next_free_block;
}mem_table_entry_t;
/******** Memory Table Data Structure**********/
typedef struct
{
mem_table_entry_t two_kib[8];
}mem_table_t;
The linker makes the starting address of the heap available for use by a program through the an external variable _sys_memory, like so: 链接器通过外部变量_sys_memory使程序可以使用堆的起始地址,如下所示:
extern void* _sys_memory; // Start of system memory
In my initialization routine, I want to make a pointer and have it point to the address same address as _sys_mem. 在我的初始化例程中, 我想创建一个指针,并指向与_sys_mem相同的地址。 Later, I want to increment the pointer by 2048 and assign that value to the starting address of the next block. 稍后,我想将指针递增2048并将该值赋给下一个块的起始地址。 What kind of datatype should I use and how can I manipulate it? 我应该使用什么样的数据类型以及如何操作它?
#include <stdint.h>
#include "mem_manager.h"
mem_table_t memory_table;
void mem_initialize(void)
{
int block_count = 0;
void *dynamic_address;
dynamic_address = _sys_memory;
while(block_count < 8)
{
memory_table.two_kib[block_count].block_address = dynamic_address;
dynamic_address = (&dynamic_address + 0x800);
memory_table.two_kib[block_count].next_free_block = dynamic_address;
block_count++;
}
There's some ambiguity in your idea (you didn't give many details on the idea of implementing a malloc). 你的想法有些含糊不清(你没有详细介绍实现malloc的想法)。 Anyway, you have committed a mistake when you wrote 无论如何,你在写作时犯了一个错误
dynamic_address=(&dynamic_address+0x800)
Cause it should be: 因为它应该是:
dynamic_address = dynamic_address + 0x800
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.