初学者在这里。是否有可能得到一个缓冲,例如:从缓冲区分配不同的内存块
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。两者都找不到任何东西。也许我不是在寻找正确的关键词? 在此先感谢。
的malloc已经在做这样的事情在内部说(如果我没有完全误解了你的问题) – UmNyobe