Very briefly, we are trying to write some allocation routines (of type unsigned char) where each allocated block has some control information associated with it. We are not trying to write a full fledged memory manager but have some specific requirement
A sample of our control structure
typedef struct _control_data
{
u8 is_segment;
:
:
:
struct _control_data* next;
}control_data;
When the user calls alloc for size 40, we will allocate
unsigned char* data_ptr = (unsigned char*)malloc(sizeof(control_data) + size);
return(&data_ptr[sizeof(control_data]);
Later the user will pass the pointer returned during alloc and we want to access the control information.
void do_some_processing(unsigned char* data_ptr)
{
struct control_data* c_ptr = (data_ptr - sizeof(control_data));
c_ptr->is_segment = TRUE;
c_ptr->next = NULL;
}
Is the above access legal and portable?