fork(1) download
  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. #define AAS_STRICT
  5.  
  6. #ifdef AAS_STRICT
  7. typedef struct { char a; } *aas_t;
  8. #else
  9. typedef char *aas_t;
  10. #endif
  11.  
  12. #define AAS_DYNAMIC 'D'
  13. #define AAS_STATIC 'S'
  14.  
  15. #define AAS_STATIC_PREFIX "S"
  16. #define AAS_CONST_STR(str) ((aas_t) ((AAS_STATIC_PREFIX str) + 1))
  17.  
  18. aas_t aas_allocate(size_t count) {
  19. char* buffer = malloc(count + 2);
  20. if(buffer == NULL)
  21. return NULL;
  22.  
  23. *buffer = AAS_DYNAMIC;
  24.  
  25. return (aas_t) buffer + 1;
  26. }
  27.  
  28. void aas_free(aas_t aas) {
  29. if(aas != NULL) {
  30. char* buffer = ((char*) aas) - 1;
  31. if(*buffer == AAS_DYNAMIC) {
  32. free(buffer);
  33. }
  34. }
  35. }
  36.  
  37. int main() {
  38. char* s1 = AAS_CONST_STR("test1");
  39. char* s2 = aas_allocate(10);
  40.  
  41. strcpy(s2, "test2");
  42.  
  43. aas_free(s1);
  44. aas_free(s2);
  45. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'main':
prog.c:16:33: error: initialization from incompatible pointer type [-Werror]
 #define AAS_CONST_STR(str)      ((aas_t) ((AAS_STATIC_PREFIX str) + 1))
                                 ^
prog.c:38:16: note: in expansion of macro 'AAS_CONST_STR'
     char* s1 = AAS_CONST_STR("test1");
                ^
prog.c:39:16: error: initialization from incompatible pointer type [-Werror]
     char* s2 = aas_allocate(10);
                ^
prog.c:43:14: error: passing argument 1 of 'aas_free' from incompatible pointer type [-Werror]
     aas_free(s1);
              ^
prog.c:28:6: note: expected 'aas_t' but argument is of type 'char *'
 void aas_free(aas_t aas) {
      ^
prog.c:44:14: error: passing argument 1 of 'aas_free' from incompatible pointer type [-Werror]
     aas_free(s2);
              ^
prog.c:28:6: note: expected 'aas_t' but argument is of type 'char *'
 void aas_free(aas_t aas) {
      ^
cc1: all warnings being treated as errors
stdout
Standard output is empty