I am trying to implement a unit cache structure and use calloc
method(dynamic allocation) to give it memory space. I used the type-defined cache_unit
as the type of the pointer to receive casted return type from calloc
. Normally after calloc
, all the bits in the allocated area should be 0. However my output is like random numbers instead of 0 and there're two warning messages.
Here's my code:
#include<stdio.h>
#include<stdlib.h>
#include <cstdint>
int main() {
typedef struct {
bool enabled;
uint8_t block[10];
int access_time;
} cache_unit;
cache_unit* cache = (cache_unit*) calloc(2, sizeof(cache_unit));
printf("%x ", *cache);
return 0;
}
With printf("%x ", *cache);
, "%x"
expects a matching unsigned
, not a cache_unit
.
Try
printf("%d\n", cache->access_time);