Browse Source

added memory.h & memory.c with safeFree macro as safe wrapper around the free() function; use safeFree() instead of free()

master
T. Meissner 9 years ago
parent
commit
1ad6f920d0
8 changed files with 61 additions and 32 deletions
  1. +9
    -6
      21st_century_c/chapter11/Makefile
  2. +15
    -11
      21st_century_c/chapter11/dict.c
  3. +13
    -2
      21st_century_c/chapter11/dict_use.c
  4. +8
    -10
      21st_century_c/chapter11/keyval.c
  5. +10
    -0
      21st_century_c/chapter11/memory.c
  6. +3
    -0
      21st_century_c/chapter11/memory.h
  7. +1
    -1
      21st_century_c/chapter11/seamlesstwo.c
  8. +2
    -2
      21st_century_c/chapter11/simple_cplx.c

+ 9
- 6
21st_century_c/chapter11/Makefile View File

@ -1,4 +1,7 @@
CFLAGS = -Wall -O0 --std=c11 -g -lm
CFLAGS = -Wall -Wextra --std=c11 -g -lm -ftest-coverage -fprofile-arcs
.PHONY : all
all : simple_cplx seamlessone seamlesstwo dict_use
simple_cplx : cplx.h complex.c simple_cplx.c
@ -10,13 +13,10 @@ seamlessone : seamlessone.c
seamlesstwo : seamlesstwo.c
$(CC) $(CFLAGS) -fms-extensions -Wno-microsoft $@.c -o $@
dict_use : keyval.h keyval.c dict.h dict.c dict_use.c
$(CC) $(CFLAGS) keyval.c dict.c $@.c -o $@
dict_use : memory.h memory.c keyval.h keyval.c dict.h dict.c dict_use.c
$(CC) $(CFLAGS) memory.c keyval.c dict.c $@.c -o $@
.PHONY : all
all : simple_cplx seamlessone seamlesstwo dict_use
.PHONY : check
check : *.h *.c
cppcheck --enable=warning --enable=style *.c
@ -28,3 +28,6 @@ clean :
rm -f seamlesstwo
rm -f dict_use
rm -rf *.dSYM
rm -rf *.gcno
rm -rf *.gcda
rm -rf *.gcov

+ 15
- 11
21st_century_c/chapter11/dict.c View File

@ -1,5 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "memory.h"
#include "dict.h"
@ -14,12 +16,11 @@ dictionary *dictionary_new(void) {
dictionary_not_found = &dnf;
}
dictionary *out = malloc(sizeof(dictionary));
if(out != NULL) {
*out = (dictionary) {};
} else {
if(out == NULL) {
fprintf(stderr, "Out of memory.\n");
abort();
}
*out = (dictionary) {};
return out;
}
@ -29,14 +30,13 @@ static void dictionary_add_keyval(dictionary *in, keyval *kv) {
in->length++;
keyval **tmp = realloc(in->pairs, sizeof(keyval*) * in->length);
if(tmp != NULL) {
in->pairs = tmp;
tmp = NULL;
in->pairs[in->length-1] = kv;
} else {
if(tmp == NULL) {
fprintf(stderr, "Out of memory.\n");
abort();
}
in->pairs = tmp;
tmp = NULL;
in->pairs[in->length-1] = kv;
}
@ -47,7 +47,11 @@ void dictionary_add(dictionary *in, char *key, void *value) {
fprintf(stderr, "NULL is not a valid key.\n");
abort();
}
dictionary_add_keyval(in, keyval_new(key, value));
if(dictionary_find(in, key) == dictionary_not_found) {
dictionary_add_keyval(in, keyval_new(key, value));
} else {
printf("Key '%s' already exists!\n", key);
}
}
@ -80,8 +84,8 @@ void dictionary_free(dictionary *in) {
for (size_t i = 0; i < in->length; i++) {
keyval_free(in->pairs[i]);
}
free(in->pairs);
free(in);
safeFree(in->pairs);
safeFree(in);
}

+ 13
- 2
21st_century_c/chapter11/dict_use.c View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include "dict.h"
#include "memory.h"
@ -13,11 +14,21 @@ int main() {
dictionary_add(d, "an int", &zero);
dictionary_add(d, "a float", &one);
dictionary_add(d, "a string", &two);
for (size_t i = 0; i < 10; i++) {
dictionary_add(d, "a string", &two);
}
printf("The integer I recorded was: %i\n", *(int*) dictionary_find(d, "an int"));
printf("The string I recorded was: %s\n", (char*) dictionary_find(d, "a string"));
dictionary *new_d = dictionary_copy(d);
dictionary_free(d);
}
unsigned int three = 3;
dictionary_add(new_d, "an uint", &three);
dictionary_free(new_d);
}

+ 8
- 10
21st_century_c/chapter11/keyval.c View File

@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "memory.h"
#include "keyval.h"
@ -10,12 +12,11 @@ Create a new key/value pair.
*/
keyval *keyval_new(char *key, void *value) {
keyval *out = malloc(sizeof(keyval));
if(out != NULL) {
*out = (keyval){.key = key, .value = value};
} else {
if (out == NULL) {
fprintf(stderr, "Out of memory.\n");
abort();
}
*out = (keyval){.key = key, .value = value};
return out;
}
@ -25,12 +26,11 @@ the values in the old pair, not copies of their data.
*/
keyval *keyval_copy(const keyval *in) {
keyval *out = malloc(sizeof(keyval));
if(out != NULL) {
*out = *in;
} else {
if (out == NULL) {
fprintf(stderr, "Out of memory.\n");
abort();
}
*out = *in;
return out;
}
@ -39,9 +39,7 @@ keyval *keyval_copy(const keyval *in) {
Free a existing key/value pair.
*/
void keyval_free(keyval *in) {
if(in != NULL) {
free(in);
}
safeFree(in);
}
@ -49,5 +47,5 @@ void keyval_free(keyval *in) {
Check if key of key/value pair matches given key.
*/
int keyval_matches(const keyval *in, const char *key) {
return !strcasecmp(in->key, key);
return !strcmp(in->key, key);
}

+ 10
- 0
21st_century_c/chapter11/memory.c View File

@ -0,0 +1,10 @@
#include <stdlib.h>
void saferFree(void **p) {
if (p != NULL && *p != NULL) {
free(*p);
p = NULL;
}
}

+ 3
- 0
21st_century_c/chapter11/memory.h View File

@ -0,0 +1,3 @@
void saferFree(void **p);
#define safeFree(p) saferFree((void **) &(p));

+ 1
- 1
21st_century_c/chapter11/seamlesstwo.c View File

@ -38,4 +38,4 @@ int main() {
double xylength = length(p.p2);
printf("Its projection onto the XY plane is %g units from the origin\n", xylength);
}
}

+ 2
- 2
21st_century_c/chapter11/simple_cplx.c View File

@ -5,7 +5,7 @@
int main() {
int complex a = 1 + 2I;
complex int a = 1 + 2I;
complex double b = 2 + I;
gsl_complex c = gsl_cplx_from_c99(a);
@ -28,4 +28,4 @@ int main() {
printf("v dot (1+2i) again\n");
gsl_vector_complex_print(dot(v, c));
}
}

Loading…
Cancel
Save