Cross-platform C SDK logo

Cross-platform C SDK

Pointers

❮ Back
Next ❯
This page has been automatically translated using the Google Translate API services. We are working on improving texts. Thank you for your understanding and patience.

Safe pointers manipulation.


Functions

voidptr_get (...)
voidptr_dget (...)
voidptr_dget_no_null (...)
voidptr_assign (...)
voidptr_destopt (...)
voidptr_copyopt (...)

The sewer library provides macros and functions for "safe" pointers manipulation. By "safe" we mean the fact that the SDK will detect improper pointer access just before a segment violation occurs. Does it make sense to detect a segment violation if the program is going to crash anyway? Pre-detection plays a very important role when running automated tests. Before the inevitable process closing, it will leave a note in the execution log.txt, indicating the reason for the crash.

  • Use ptr_get to get the content of a pointer.
  • 1
    2
    3
    4
    5
    6
    7
    8
    
    // v2 = NULL
    // Segmentation fault
    V2Df v1 = *v2;
    
    // "v2 is NULL in file::line" 
    // will be record in log.txt
    // and then, Segmentation fault
    V2Df v1 = ptr_get(v2, V2Df);  
    

ptr_get ()

Access to the content of the pointer (dereference), verifying previously that it is not NULL.

void
ptr_get(type *ptr,
        type);
1
2
3
4
5
6
void compute(const V2Df *v1, const V2Df *v2)
{
    /* Safer than t = *v1; */
    V2Df t = ptr_get(v1, V2Df);
    ...
}
ptr

Pointer.

type

Pointer type.


ptr_dget ()

Access the content of a double pointer, invalidating it later.

void
ptr_dget(type **ptr,
         type);
1
2
3
4
5
6
7
8
9
Ctrl *create(Model **model, View **view)
{
    Ctrl *ctrl = heap_new(Ctrl);
    ctrl->model = ptr_dget(model, Model);
    ctrl->view = ptr_dget(view, View);
    // *model = NULL
    // *view = NULL
    return ctrl;
}
ptr

Double pointer.

type

Pointer type.


ptr_dget_no_null ()

Like ptr_dget, but the content of the double pointer (*dptr) can not be NULL.

void
ptr_dget_no_null(type **ptr,
                 type);
1
2
3
4
5
6
7
8
Ctrl *create(Model **model, View **view)
{
    // *model and *view can't be NULL
    Ctrl *ctrl = heap_new(Ctrl);
    ctrl->model = ptr_dget_no_null(model, Model);
    ctrl->view = ptr_dget_no_null(view, View);
    return ctrl;
}
ptr

Double pointer.

type

Pointer type.


ptr_assign ()

Assign content from one pointer to another, if the destination is not NULL.

void
ptr_assign(dest,
           src);
dest

Destination pointer.

src

Source pointer.


ptr_destopt ()

Destroy an object if not NULL.

void
ptr_destopt(FPtr_destroy func_destroy,
            type dptr,
            type);
1
2
3
4
5
6
cassert_no_null(dptr);
if (*dptr != NULL)
{
    func_destroy(*dptr);
    *dptr = NULL;
}
func_destroy

Destructor.

dptr

Double pointer to the object to destroy.

type

Object type.


ptr_copyopt ()

Copy the object if not NULL.

void
ptr_copyopt(FPtr_copy func_copy,
            type ptr,
            type);
1
2
3
4
if (ptr != NULL)
    return func_copy(ptr);
else
    return NULL;
func_copy

Copy constructor.

ptr

Object to copy (source).

type

Object type.

❮ Back
Next ❯