//Smart pointer structure with built-in length and blocksize
typedef struct SMPTR_H_VAR
{
UINT H_SMPTR_LEN;
UINT H_SMPTR_BSZ;
} SMPTR_H_VAR;
typedef struct SMPTR
{
void *MBLOC;
SMPTR_H_VAR *shv;
const UINT *const BLOCL;
const UINT *const BLOCSZ;
} SMPTR;
//Smart pointer strucure 'constructor'
SMPTR *_SMPTRC(UINT var_sz, UINT var_num)
{
/*
// SMPTR structure
//
// Memory block casted to void *
// SMPTR_H_VAR structure
// const UINT *const BLOCL variable, used as a reference length variable for the 'Memory block' above
// const UINT *const BLOCSZ variable, used as a reference size variable, 'in bytes', for the size of the 'Memory block' above
*/
//Creation and initialization is done dynamically, to prevent the rise of bad pointers.
SMPTR *s = (SMPTR *)malloc(sizeof(SMPTR));
SMPTR_H_VAR *shv = (SMPTR_H_VAR *)malloc(sizeof(SMPTR_H_VAR));
//SMPTR_H_VAR variables are set through the SMPTR pointer
s->shv;
s->shv->H_SMPTR_LEN = var_num;
s->shv->H_SMPTR_BSZ = var_sz * var_num;
s->MBLOC = malloc(var_sz * var_num);
s->BLOCL = &shv.H_SMPTR_LEN;
s->BLOCSZ = &shv.H_SMPTR_BSZ;
return s;
}
In this code, the SMPTR s is returned as a pointer, I want to return this variable as a non-pointer. Here I create a structure in dynamic memory, so that it will never go out of scope. That is why there are a lot of pointers everywhere.