我的目标是创建一个.dat文件,其中包含100个空记录。我已经使用以下代码启动了该程序:
void* kalloc(int _size) {
void* ptr = NULL;
struct ListNode* freeMem = kallocator.free_memory;
struct ListNode* allocMem = kallocator.allocated_memory;
struct KBlock* freeBlock;
freeBlock = freeMem->item;
struct KBlock* allocBlock = (struct KBlock*) malloc(sizeof(struct KBlock));
if(kallocator.aalgorithm == FIRST_FIT){
while(freeMem){
if(freeBlock->size >= _size){
allocBlock->size = _size;
allocBlock->address = freeBlock->address;
struct ListNode* listnode = List_createNode(allocBlock);
List_insertTail(&allocMem, listnode);
printf("size of freeBlock: %d\n", freeBlock->size);
printf("address of freeBlock: %p\n", freeBlock->address);
ptr = freeBlock->address;
printf("Pointer value: %p\n", ptr);
freeBlock->address = freeBlock->address + _size;
allocMem = allocMem->next;
freeBlock->size = freeBlock->size - _size;
printf("allocBlock size: %d\n", allocBlock->size);
}
else{
printf("Not enough space in memory\n");
return NULL;
}
freeMem = freeMem->next;
}
}
else if(kallocator.aalgorithm == BEST_FIT){
}
else if(kallocator.aalgorithm == WORST_FIT){
}
else{
printf("No algorithm chosen\n");
exit(-1);
}
// Allocate memory from kallocator.memory
// ptr = address of allocated memory
return ptr;
}
我得到一个.dat输出,但是当我打开它以检查所有项目都为NULL时,它们下面有角色。如何检查这是否是垃圾值,或者我实际上是将100个结构初始化为空白?
@@ @@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
答案 0 :(得分:0)
struct item createEmptyRecord = {0, "", "", 0.0};
请注意,您在此处传递""
来初始化int
成员。字符串文字""
与NULL不同。如果您提高编译器的警告级别,它应警告您将const char*
转换为int
。
将结构初始化为全零的正确方法是:
struct item createEmptyRecord = {0};
或者您可以使用memset
:
struct item createEmptyRecord;
memset(&createEmptyRecord, 0, sizeof(createEmptyRecord));