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

时间:2012-11-27 16:16:27

标签: c memory struct malloc 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。两者都找不到任何东西。也许我不是在寻找合适的关键词? 提前谢谢。

4 个答案:

答案 0 :(得分:3)

Malloc使用自己的内部内存管理,因此不会从您提供的内存中进行子分配。

有许多malloc实现可用(Google“malloc alternative”),它们提供针对各种用例(嵌入式,多处理器,调试)优化的内存管理策略。您可能会找到一个现有的解决方案来解决您尝试使用此问题解决的潜在问题。

答案 1 :(得分:0)

编写自己的函数,它将与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;
}

请记住,您必须编写mem_free()free()函数

答案 2 :(得分:0)

答案 3 :(得分:0)

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

我希望这会有所帮助......,

相关问题