I wrote a program that automatically allocates memory for a structure and then sets a field within that structure equal to zero.
I thought, instead of setting only that member equal to zero, why not zero-out the entire block? In comes memset().
After using this function my program no longer worked. Once I attempted to assign a value to a field within the structure contained in that memory block the program crashed with a segmentation fault error.
So, this leads me to wonder - when a block of memory is allocated via malloc()/AUTO, is there other information stored in this location that is necessary for accessing and using this space?
typedef struct{ NodeT *nodes[MAX_SIZE]; int count; } stackT; stackT stack; stackT* ptr = &stack; void init(stackT* stackPtr){ //stackPtr->count = 0; memset(stackPtr, 0, sizeof(stackptr)); }