简体   繁体   English

从缓冲区分配不同的内存块

[英]Allocating different blocks of memory from a buffer

Beginner here. 初学者在这里。 Is it possible to get a buffer, for example: 是否可以获取缓冲区,例如:

char buffer[1024];

And split it into smaller chunks of memory (random size depending on user input) using malloc until there's no more space in the buffer? 并使用malloc将其分成较小的内存块(随机大小取决于用户输入),直到缓冲区中没有更多空间了? For example: 1st block = 16, 2nd block = 256, 3rd block = 32, etc.. until I reach 1024. Also I would like to create a structure for every block created. 例如:第一个块= 16,第二个块= 256,第三个块= 32,依此类推,直到达到1024。我还想为每个创建的块创建一个结构。 I'm using plain C. 我正在使用纯C。

Even though I'm not sure if I can do that, I've started something: 即使我不确定是否可以这样做,我还是开始了一些事情:

#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;
}

Research I've done: Google and SO. 我已经完成的研究:Google和SO。 Couldn't find anything on both. 两者都找不到。 Perhaps I'm not searching for the right key words? 也许我不是在寻找正确的关键词? Thanks in advance. 提前致谢。

Malloc uses its own internal memory management, so it will not sub-allocate from memory that you provide. Malloc使用自己的内部内存管理,因此它不会从您提供的内存中进行子分配。

There are a number of malloc implementations available (Google "malloc alternatives") that provide memory management strategies optimized for various use cases (embedded, multiprocessor, debugging). 有许多可用的malloc实现(Google“ malloc替代方案”)提供了针对各种用例(嵌入式,多处理器,调试)进行了优化的内存管理策略。 You may well find an existing solution that addresses the underlying problem you are trying to address with this question. 您可能会找到一个现有的解决方案,该解决方案可以解决您要解决的基本问题。

write your own function which will do the same as malloc because malloc has already it's own implementation so it won't take memory from your allocated buffer. 编写与malloc相同的函数,因为malloc已经拥有自己的实现,因此不会从分配的缓冲区中占用内存。

It always allocates memory from heap 它总是从堆分配内存

You can write your own function like this : 您可以这样编写自己的函数:

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;
}

Remember for this, you will have to write mem_free() your own free() function 记住这一点,您将必须编写自己的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;
}

I hope this helps...., 我希望这有帮助....,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM