2012-11-27 55 views
0

初学者在这里。是否有可能得到一个缓冲,例如:从缓冲区分配不同的内存块

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

研究我做:谷歌和SO。两者都找不到任何东西。也许我不是在寻找正确的关键词? 在此先感谢。

+1

的malloc已经在做这样的事情在内部说(如果我没有完全误解了你的问题) – UmNyobe

回答

3

Malloc使用自己的内部内存管理,所以它不会从您提供的内存进行重新分配。

有许多可用的malloc实现(谷歌“的malloc方案”),提供了各种用例(嵌入式,多处理器调试)优化的内存管理策略。您可能会发现一个解决您尝试解决此问题的根本问题的现有解决方案。

+0

这还挺我的想法。感谢您的输入。 –

+0

@MirkoCroCop。乐意效劳。请记住接受每个问题的最佳答案(假设至少有一个很好的答案)。 –

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()功能

+0

谢谢,我会尽力实施它。 –

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

我希望这有助于...,

+0

感谢阿迪尔,但我不明白你为什么做这个主:A =分配(的sizeof(int)的* 10); b = allocate(sizeof(char)* 10); –

+0

就只是一个例子,如何调用函数“分配” ...... –