Created
August 13, 2012 05:21
-
-
Save DavidPu/3337153 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define _GNU_SOURCE | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <search.h> | |
| #define ARRAY_SZ (10965116) | |
| #if 0 | |
| void *__real_malloc (size_t); | |
| /* This function wraps the real malloc */ | |
| void * __wrap_malloc (size_t size) | |
| { | |
| void *lptr = __real_malloc(size); | |
| printf("Malloc: %lu bytes @%p\n", size, lptr); | |
| return lptr; | |
| } | |
| #endif | |
| void | |
| action(const void *nodep, const VISIT which, const int depth) | |
| { | |
| int *datap; | |
| switch (which) { | |
| case preorder: | |
| break; | |
| case postorder: | |
| datap = *(int **) nodep; | |
| printf("%6d\n", *datap); | |
| break; | |
| case endorder: | |
| break; | |
| case leaf: | |
| datap = *(int **) nodep; | |
| printf("%6d\n", *datap); | |
| break; | |
| } | |
| } | |
| static inline int compare(const void *key, const void *val) | |
| { | |
| return *(int*)key - *(int*)val; | |
| } | |
| static void free_node(void *nodep) {} | |
| int main(int argc, char *argv[]) | |
| { | |
| int *keys = malloc(sizeof(int) * ARRAY_SZ); | |
| int i; | |
| void *root = NULL; | |
| if (!keys) { | |
| printf("malloc failed.\n"); | |
| return -1; | |
| } | |
| srand(time(0)); | |
| for (i = 0; i < ARRAY_SZ; i++) { | |
| keys[i] = i;//rand(); | |
| tsearch(&keys[i], &root, compare); | |
| } | |
| //twalk(root, action); | |
| free(keys); | |
| tdestroy(root, free_node); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment