0

初学者在这里。是否可以获得缓冲区,例如:

char buffer[1024];

并使用 malloc 将其拆分为更小的内存块(随机大小取决于用户输入),直到缓冲区中没有更多空间?例如:第 1 块 = 16、第 2 块 = 256、第 3 块 = 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

4 回答 4

3

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

有许多可用的 malloc 实现(谷歌“malloc 替代方案”)提供针对各种用例(嵌入式、多处理器、调试)优化的内存管理策略。您很可能会找到一个现有的解决方案来解决您试图通过这个问题解决的潜在问题。

于 2012-11-27T16:19:35.360 回答
0

编写你自己的函数,它的作用与mallocmalloc 已经有它自己的实现一样,所以它不会从你分配的缓冲区中占用内存。

它总是从堆中分配内存

您可以像这样编写自己的函数:

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()函数

于 2012-11-27T16:22:04.497 回答
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;
}

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

于 2012-11-27T17:48:02.250 回答