2012-11-27 57 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

就只是一個例子,如何調用函數「分配」 ...... –