I was wondering if the 'SLINK_*' and 'SLIST_*' macros, which
implement a simple singly-linked list, will produce _any_ possible
undefined behavior:
____________________________
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
/* Single-Link API
____________________________________*/
typedef struct slink_s slink;
struct slink_s {
slink* next;
};
#define SLINK_STATICINIT() {0}
#define SLINK_GET_NEXT(mp_this) ( \
(mp_this)->next \
)
#define SLINK_SET_NEXT(mp_this, mp_link) ( \
(mp_this)->next = (mp_link) \
)
/* Single-List API
____________________________________*/
typedef struct slist_s slist;
struct slist_s {
slink front;
};
/* static init */
#define SLIST_STATICINIT() { \
SLINK_STATICINIT() \
}
/* returns the front of the list */
#define SLIST_GET_FRONT(mp_this) ( \
SLINK_GET_NEXT(&(mp_this)->front) \
)
/* returns the mp_link parameter */
#define SLIST_PUSH_FRONT(mp_this, mp_link) ( \
SLINK_SET_NEXT(mp_link, SLIST_GET_FRONT(mp_this)), \
SLINK_SET_NEXT(&(mp_this)->front, mp_link) \
)
/* returns 0 on failure */
#define SLIST_POP_FRONT(mp_this, mp_plink) ( \
*(mp_plink) = SLIST_GET_FRONT(mp_this), \
*(mp_plink) \
? SLINK_SET_NEXT(&(mp_this)->front, \
SLINK_GET_NEXT(*(mp_plink))), 1 \
: 0 \
)
/* Test Application
____________________________________*/
#define TEST_DEPTH() 5
static int exit_prompt(int const, char const* const);
static slist g_list = SLIST_STATICINIT();
int main(void) {
int t;
for(t = 0; t < TEST_DEPTH(); ++t) {
int i;
slink* _this;
#define LIST_DEPTH() (t + 5)
for(i = 0; i < LIST_DEPTH(); ++i) {
slink* cmp = 0;
_this = malloc(sizeof(*_this));
cmp = SLIST_PUSH_FRONT(&g_list, _this);
printf("(%p/%p/%p)-SLIST_PUSH_FRONT(...)\n",
(void*)_this,
(void*)SLIST_GET_FRONT(&g_list),
(void*)SLINK_GET_NEXT(_this));
assert(_this == cmp);
}
printf("(%i)-FILLED! Press <ENTERTo Continue...\n", t);
getchar();
_this = SLIST_GET_FRONT(&g_list);
while(_this) {
printf("(%p)-SLIST_GET_FRONT(...)\n", (void*)_this);
_this = SLINK_GET_NEXT(_this);
}
printf("(%i)-ITERATED! Press <ENTERTo Continue...\n", t);
getchar();
while(SLIST_POP_FRONT(&g_list, &_this)) {
printf("(%p)-SLIST_POP_FRONT(...)\n", (void*)_this);
free(_this);
}
printf("(%i)-FLUSHED! Press <ENTERTo Continue...\n", t);
getchar();
puts("\n----------------------------------");
}
assert(! SLIST_GET_FRONT(&g_list));
return exit_prompt(
0, "\n\n\n\
_______________________\npress <ENTERto exit...\n");
}
int exit_prompt(int const status, char const* const buf) {
printf("%s", buf);
getchar();
return status;
}
____________________________
Thank you in advance.