← cproj

realloc works

be8cdcdd7a9c8b06530fbfd4617d16b708123651 · ovidiucs · Jun 24, 2019 20:52

+35 −33 · 3 files changed

  • README.md +15 −11
  • mylib/src/myhash.c +9 −11
  • mylib/test/t_hash.c +11 −11
diff --git a/README.md b/README.md
index 0b3b90e..6dbf239 100644
--- a/README.md
+++ b/README.md
@@ -1,11 +1,15 @@
-http://scc-forge.lancaster.ac.uk/open/char/debug/assert
-https://easyprog.online/blog/sort_par.html
-http://www.jera.com/techinfo/jtns/jtn002.html#Source_Code
-https://gcc.gnu.org/onlinedocs/cpp/
-http://users.cs.cf.ac.uk/Dave.Marshall/C/
-http://www.jera.com/techinfo/jtns/jtn002.html#Source_Code
-http://users.cs.cf.ac.uk/Dave.Marshall/C/
-https://hci.uni-hannover.de/files/prog1script/script.html
-http://dunkels.com/adam/pt/index.html
-http://bluefox.engineering/object-oriented-programming-in-c-with-examples/
-http://github.com/josuemnb/Run
+[‘C’ Hyper-Reference,](http://scc-forge.lancaster.ac.uk/open/char/debug/assert)
+
+[Multithreaded Quicksort in Go and in C](https://easyprog.online/blog/sort_par.html)
+
+[JTN002 - MinUnit -- a minimal unit testing framework for C](http://www.jera.com/techinfo/jtns/jtn002.html#Source_Code)
+
+[The C Preprocessor](https://gcc.gnu.org/onlinedocs/cpp/)
+
+[UNIX System Calls and Subroutines using C](http://users.cs.cf.ac.uk/Dave.Marshall/C/)
+
+[Design Recipes in C](https://hci.uni-hannover.de/files/prog1script/script.html)
+
+[Protothreads](http://dunkels.com/adam/pt/index.html)
+
+[Introduction to programming in C, and system programming (processes, threads, synchronization, networking and more!)](http://cs241.cs.illinois.edu/coursebook/)
diff --git a/mylib/src/myhash.c b/mylib/src/myhash.c
index 7fb5a45..babffbc 100644
--- a/mylib/src/myhash.c
+++ b/mylib/src/myhash.c
@@ -67,7 +67,7 @@ HashTable *h_create ( unsigned int size ) {
 // Find a key in a given linked list
 
 static HashNode *h_findKey(HashNode *hNode, char *key) {
-	
+
 	while (hNode != NULL) {
 		if(strcmp(hNode->h_key,key) == 0) {
 
@@ -76,12 +76,12 @@ static HashNode *h_findKey(HashNode *hNode, char *key) {
 	}
 	return hNode;
 }
- 
+
 // Insert a key-value pair into a hash table
 
 HashNode *h_insert (HashTable *hTable, char *key, char *value) {
 	HashNode *newNode;
-	// result hash will give same index and that index will already have 
+	// result hash will give same index and that index will already have
 	// a node if there was a previous write here
 	size_t resultHash = hash(key,hTable->h_size);
 
@@ -93,7 +93,7 @@ HashNode *h_insert (HashTable *hTable, char *key, char *value) {
 
 
 	// Executes only if the newNode is NULL-not visited before
-    if ( newNode == NULL ) {	
+    if ( newNode == NULL ) {
 		if ( (newNode = (HashNode *) malloc(sizeof(HashNode) ) ) == NULL ||
 				(newNode->h_key = strdup(key) ) == NULL ||
 				(newNode->h_value = strdup(value) ) == NULL ) {
@@ -107,12 +107,10 @@ HashNode *h_insert (HashTable *hTable, char *key, char *value) {
 	 	hTable->h_items[resultHash] = newNode;
      } else {
  		// Keys are the same , reeuse key and assign a different value + 1 for '/0'
-		// does not work as the value was not assigned from the argument to h_value
-     	// newNode->h_value = (char *) realloc((void *) oldNodePtr->h_value, strlen(value)+1);
-		 free(newNode->h_value);
-		 newNode->h_value = strdup(value);
+			newNode->h_value = (char *) realloc((void *) newNode->h_value, strlen(value)+1);
+			newNode->h_value = strcpy(newNode->h_value,value);
      }
-			
+
 
 
 	return newNode;
@@ -129,11 +127,11 @@ HashNode *h_search (HashTable *hTable, char *key) {
 
 	//1. call hash function with key and table size
 	size_t resultHash = hash(key,hTable->h_size);
-	// the node to the hashed array index of the key 
+	// the node to the hashed array index of the key
 	newNode = hTable->h_items[resultHash];
 	// call findkey on the value found to get the key
 	newNode = h_findKey(newNode, key);
 
 	// return the pointer
 	return newNode;
-}
\ No newline at end of file
+}
diff --git a/mylib/test/t_hash.c b/mylib/test/t_hash.c
index 8fca3fe..6e65924 100644
--- a/mylib/test/t_hash.c
+++ b/mylib/test/t_hash.c
@@ -7,16 +7,16 @@
 // test h_search - call on same keys and verify that is same result as the initial call to insert
 
 static void dumpTable (HashTable *table) {
-	// Scope - take a hashTable and iterate through each array element with each 
+	// Scope - take a hashTable and iterate through each array element with each
 	// element containing pointers to has nodes; in essence containing a linked list
 
-	// First eleemnt in array of pointers will be the first node 
+	// First eleemnt in array of pointers will be the first node
 	// HashNode **hNodePtr == &hNodePtr[i]
 	HashNode **hNodePtr = table->h_items;
-	// The last element of the array of pointer will be the size defined in the 
+	// The last element of the array of pointer will be the size defined in the
 	HashNode **hNodePtrZ = hNodePtr+ table->h_size;
 	// A new nash node - can be null or something else
-	HashNode *node;	
+	HashNode *node;
 
 	do {
 		// Derefenrce one level and set it to node
@@ -26,7 +26,7 @@ static void dumpTable (HashTable *table) {
 				fprintf(stderr, "De-ref the pointer at %p we have our nosde linked list with values:\n\
 					h_next:  Address: %p\n\
 					h_key:   Address: %p, Value: %s\n\
-					h_value: Address: %p, Value: %s\n", 
+					h_value: Address: %p, Value: %s\n",
 					(void *)node,
 					(void *)node->h_next,
 					(void *)node->h_key,
@@ -40,7 +40,7 @@ static void dumpTable (HashTable *table) {
 			node = node->h_next;
 		}
 	// Increase by one element inside the array (move by one array bucket )
-	} while (++hNodePtr < hNodePtrZ); 
+	} while (++hNodePtr < hNodePtrZ);
 }
 
 // insert multpile keys
@@ -48,16 +48,16 @@ static void dumpTable (HashTable *table) {
 int main(int argc, char** argv){
 	HashTable *tableResult = h_create(10);
 	assert(tableResult != NULL);
-	
+
 	HashNode  *nodeResult = h_insert(tableResult, "400", "101");
 	assert(nodeResult != NULL);
-	
+
 	HashNode  *nodeResult2 = h_insert(tableResult, "401", "102");
-	
-	HashNode  *nodeResult3 = h_insert(tableResult, "400", "103");
+
+	HashNode  *nodeResult3 = h_insert(tableResult, "400", "1034444444444444");
 
 	HashNode *searchResult = h_search(tableResult,"400");
-	
+
 	dumpTable(tableResult);
 #if 0
 	int i = 0;