[英]Allocating different blocks of memory from a buffer
初学者在这里。 是否可以获取缓冲区,例如:
char buffer[1024];
并使用malloc将其分成较小的内存块(随机大小取决于用户输入),直到缓冲区中没有更多空间了? 例如:第一个块= 16,第二个块= 256,第三个块= 32,依此类推,直到达到1024。我还想为每个创建的块创建一个结构。 我正在使用纯C。
即使我不确定是否可以这样做,我还是开始了一些事情:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int x = 0;
printf("Enter size of block to be allocated: ");
scanf("%d", &x);
/*(need to implement): call the following function until there's no more
space left in the buffer*/
allocate(x);
return 0;
}
void *allocate(size_t size)
{
char buffer[1024];
char *block;
/*The following allocates a block with the size of the user input.
How do I associate it with the buffer?*/
block = (char *) malloc(size + 1);
//Creates a structure. How do I create one for every block created?
typedef struct blk_struct
{
int data;
struct blk_struct *size_blk;
struct blk_struct *next;
}blk_struct;
blk_struct *first;
}
我已经完成的研究:Google和SO。 两者都找不到。 也许我不是在寻找正确的关键词? 提前致谢。
Malloc使用自己的内部内存管理,因此它不会从您提供的内存中进行子分配。
有许多可用的malloc实现(Google“ malloc替代方案”)提供了针对各种用例(嵌入式,多处理器,调试)进行了优化的内存管理策略。 您可能会找到一个现有的解决方案,该解决方案可以解决您要解决的基本问题。
编写与malloc
相同的函数,因为malloc已经拥有自己的实现,因此不会从分配的缓冲区中占用内存。
它总是从堆分配内存
您可以这样编写自己的函数:
char buffer[1024];// fixed size buffer
int freeindex; // global variable or make it static to keep track of allocated memory
char* mem_alloc(size_t size)
{
if(freeindex == 1023 || (freeindex + size ) > 1023)
return NULL;
char * ret_addr = &buffer[freeindex];
freeindex+=size;
return ret_addr;
}
记住这一点,您将必须编写自己的free()
函数mem_free()
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFER_SIZE 1024
//Creates a structure. How do I create one for every block created?
typedef struct blk_struct
{
char *dataptr;
int start_blk, size_blk;
struct blk_struct *prev;
struct blk_struct *next;
}blk_struct;
char buffer[BUFFER_SIZE];
blk_struct *first = NULL;
blk_struct *last = NULL;
int main(void)
{
int x = 0;
int *a;
char *b;
printf("Enter size of block to be allocated: ");
scanf("%d", &x);
/*(need to implement): call the following function until there's no more
space left in the buffer*/
a = allocate(sizeof(int) * 10);
b = allocate(sizeof(char) * 10);
return 0;
}
void *allocate(size_t size)
{
blk_struct *block;
/* checking for required memory */
if (((last->dataptr + last->size_blk + size) - buffer) > BUFFER_SIZE)
return NULL; /* Memory Full */
/*The following allocates a block with the size of the user input.
How do I associate it with the buffer?*/
block = malloc(sizeof(blk_struct));
/* Changing the first and last block */
if (first) {
/* Filling Block Info */
block->dataptr = buffer;
block->start_blk = 0;
block->size_blk = size;
block->prev = NULL;
block->next = NULL;
first = block;
last = block;
}
else {
/* Filling Block Info */
block->dataptr = last->dataptr + last->size_blk;
block->start_blk = last->start_blk + last->size_blk;
block->size_blk = size;
block->prev = last;
block->next = NULL;
last->next = block;
last = block;
}
return block->dataptr;
}
我希望这有帮助....,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.