gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
hello
- User
hello
- Assistant
Hello! How can I assist you today?
- User
用C写一个内存池管理
- Assistant
好的,以下是一个简单的C代码示例,用于实现一个基本的内存池管理器:
#include <stdio.h> #include <stdlib.h> #define MAX_POOL_SIZE 10000 #define BLOCK_SIZE 16 typedef struct { char is_free; char data[BLOCK_SIZE]; } Block; typedef struct { Block blocks[MAX_POOL_SIZE]; } MemoryPool; MemoryPool memory_pool; void initialize_pool() { int i; for (i = 0; i < MAX_POOL_SIZE; i++) { memory_pool.blocks[i].is_free = 1; } } void* allocate_memory(size_t size) { int i, j, num_blocks; num_blocks = (size + BLOCK_SIZE - 1) / BLOCK_SIZE; for (i = 0; i < MAX_POOL_SIZE; i++) { if (memory_pool.blocks[i].is_free) { int found = 1; for (j = i; j < i + num_blocks; j++) { if (!memory_pool.blocks[j].is_free) { found = 0; break; } } if (found) { for (j = i; j < i + num_blocks; j++) { memory_pool.blocks[j].is_free = 0; } return memory_pool.blocks[i].data; } } } return NULL; } void free_memory(void* ptr) { int i; for (i = 0; i < MAX_POOL_SIZE; i++) { if (memory_pool.blocks[i].data == ptr) { memory_pool.blocks[i].is_free = 1; break; } } } int main() { initialize_pool(); // 使用示例 int* num = (int*)allocate_memory(sizeof(int)); *num = 10; printf("Value: %d\n", *num); free_memory(num); return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.