From b662ac9469d946a3d0608ef635cf8ed37f584d25 Mon Sep 17 00:00:00 2001 From: Stathis Kamperis Date: Wed, 17 Oct 2007 09:01:15 +0300 Subject: [PATCH] Add 2D array dynamic allocation test case --- malloc/test2.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 malloc/test2.c diff --git a/malloc/test2.c b/malloc/test2.c new file mode 100644 index 0000000..83f0286 --- /dev/null +++ b/malloc/test2.c @@ -0,0 +1,57 @@ +#include +#include +#include /* for time() in srand() */ + +#include "mpool.h" + +int main(int argc, char *argv[]) +{ + mpool_t *mpool; + int i, j, **array; + + /* Check argument count */ + if (argc != 3) { + fprintf(stderr, "usage: %s nrows ncols\n", argv[0]); + exit(EXIT_FAILURE); + } + + /* Initialize memory pool with 1024 bytes */ + if (mpool_init(&mpool, 10, 5) == MPOOL_ENOMEM) { + fprintf(stderr, "Not enough memory\n"); + exit(EXIT_FAILURE); + } + + /* Initialize random number generator */ + srand(time(NULL)); + + /* Allocate memory for rows */ + if ((array = mpool_alloc(mpool, atoi(argv[1]) * sizeof *array)) == NULL) { + fprintf(stderr, "No available block in pool\n"); + mpool_destroy(mpool); + exit(EXIT_FAILURE); + } + + /* Allocate memory for columns */ + for (i = 0; i < atoi(argv[1]); i++) { + if ((array[i] = mpool_alloc(mpool, atoi(argv[2]) * sizeof **array)) == NULL) { + fprintf(stderr, "No available block in pool\n"); + mpool_destroy(mpool); + exit(EXIT_FAILURE); + } + } + + /* Fill in matrix with random values and print it */ + for (i = 0; i < atoi(argv[1]); i++) { + for (j = 0; j < atoi(argv[2]); j++) { + array[i][j] = rand() % 100); + printf("%2d\t", array[i][j]); + } + printf("\n"); + } + + + /* Destroy memory pool and free all resources */ + mpool_destroy(mpool); + + return EXIT_SUCCESS; +} -- 2.11.4.GIT