This code example shows the usage of the Memory Allocation API. It can also be found at speect/engine/examples/base/utils/mem_example.c, and be compiled with the WANT_EXAMPLES build option.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <stdlib.h> /* for abort() */
#include "speect.h"
/* Memory Handling Examples */
int main(void)
{
int *counter;
char **cpoint1;
char **cpoint2;
/*
* note that TYPE can be either a reference to a
* specific variable or to a data type. Internally
* the macro uses the sizeof() operator.
*/
counter = S_MALLOC(counter, 10);
counter[4] = 0;
S_FREE(counter);
counter = S_MALLOC(int, 10);
counter[1] = 0;
S_FREE(counter);
cpoint1 = S_CALLOC(char*, 2);
cpoint1 = S_REALLOC(cpoint1, char*, 5);
S_FREE(cpoint1);
cpoint2 = S_CALLOC(char*, 12);
S_FREE(cpoint2);
/*
* SPCT_FREE sets the pointed to variable to NULL
* therefore this test should not abort.
*/
if (cpoint1 != cpoint2)
abort();
return 0;
}
|