簡體   English   中英

從緩沖區分配不同的內存塊

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM