diff -r e7dfcf229625 -r 0f83916c1639 docs/src/modules.md --- a/docs/src/modules.md Sat May 12 14:56:17 2018 +0200 +++ b/docs/src/modules.md Sun May 13 17:34:06 2018 +0200 @@ -403,12 +403,55 @@ This concrete implementation of an UCX Allocator allows you to grab some amount of memory which is then handled as a stack. Please note, that the term *stack* only refers to the behavior of this -allocator. You may still choose if you want to use stack or heap memory +allocator. You may still choose to use either stack or heap memory for the underlying space. - A typical use case is an algorithm where you need to allocate and free large amounts of memory very frequently. +The following code sample shows how to initialize a stack and push and pop +simple data. +```C + const size_t len = 1024; + char space[len]; + UcxStack stack; + ucx_stack_init(&stack, space, len); + + int i = 42; + float f = 3.14f; + const char* str = "Hello!"; + size_t strn = 7; + + /* push the integer */ + ucx_stack_push(&stack, sizeof(int), &i); + + /* push the float and rember the address */ + float* remember = ucx_stack_push(&stack, sizeof(float), &f); + + /* push the string with zero terminator */ + ucx_stack_push(&stack, strn, str); + + /* if we forget, how big an element was, we can ask the stack */ + printf("Length of string: %zu\n", ucx_stack_topsize(&stack)-1); + + /* retrieve the string as sstr_t, without zero terminator! */ + sstr_t s; + s.length = ucx_stack_topsize(&stack)-1; + s.ptr = malloc(s.length); + ucx_stack_popn(&stack, s.ptr, s.length); + printf("%" PRIsstr "\n", SFMT(s)); + + /* print the float directly from the stack and free it */ + printf("Float: %f\n", *remember); + ucx_stack_free(&stack, remember); + + /* the last element is the integer */ + int j; + ucx_stack_pop(&stack, &j); + printf("Integer: %d\n", j); +``` + + + ## String *Header file:* [string.h](api/string_8h.html)