Comments (6)
Hi @lpipes,
Can you provide a snippet of code with your desired usage? I might be able to identify an issue. There should be no problems using this library how you have described.
As always, I would highly recommend compiling C code with -Wall
and -Werror
compiler options so the compiler identifies obvious issues.
from hashmap.
Thank you! It's probably something I don't understand. I tried to make this MWE but now it won't compile. I tried to compile with gcc -g -pg -w -o main main.c findABC.c hashmap.c
global.h:
#include "hashmap.h"
#include "hashmap_base.h"
typedef struct leafMap{
char* name;
int root;
int node;
}leafMap;
extern struct hashmap_base map;
main.c:
#include <stdbool.h>
#include <stdint.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "hashmap.h"
#include "hashmap_base.h"
#include "global.h"
struct hashmap_base map;
int main()
{
HASHMAP(char, struct leafMap) map;
hashmap_init(&map,hashmap_hash_string, strcmp);
struct leafMap *l;
l = malloc(sizeof(leafMap));
l->name=malloc(10*sizeof(char));
strcpy(l->name,"ABC");
l->root=12;
l->node=5;
hashmap_put(&map,l->name,l);
findABC();
free(l->name);
free(l);
}
and findABC.c:
#include <stdbool.h>
#include <stdint.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "hashmap.h"
#include "hashmap_base.h"
#include "global.h"
void findABC(){
struct leafMap *l;
l=hashmap_get(&map,"ABC");
printf("Key: %s, Root: %d, Node: %d\n", l->name, l->root, l->node);
}
from hashmap.
I'm able to compile and run this code correctly but not the previous code. I compiled with gcc -g -pg -w -o main main.c hashmap.c
global.h:
#include "hashmap.h"
#include "hashmap_base.h"
typedef struct leafMap{
char* name;
int root;
int node;
}leafMap;
main.c:
#include <stdbool.h>
#include <stdint.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "hashmap.h"
#include "hashmap_base.h"
#include "global.h"
HASHMAP(char, struct leafMap) map;
void findABC(){
struct leafMap *l;
l=hashmap_get(&map,"ABC");
printf("Key: %s, Root: %d, Node: %d\n", l->name, l->root, l->node);
}
int main()
{
hashmap_init(&map,hashmap_hash_string, strcmp);
struct leafMap *l;
l = malloc(sizeof(leafMap));
l->name=malloc(10*sizeof(char));
strcpy(l->name,"ABC");
l->root=12;
l->node=5;
hashmap_put(&map,l->name,l);
findABC();
free(l->name);
free(l);
}
./main
Key: ABC, Root: 12, Node: 5
from hashmap.
Your application scenario is the same as mine. I use global variables and use locking. I also have similar problems. I once suspected that it was my own use problem.
from hashmap.
I use this to replace the redundant map table of C++. Now hashmap supports adding, deleting, and querying, but it has not been modified.
from hashmap.
A few notes:
- In the original code, the
FindABC()
declaration was not visible tomain.c
. The second (working) version moved the function tomain.c
where it was visible. - There is no need to include
hashmap_base.h
. Just includehashmap.h
. - Don't forget to call
hashmap_cleanup()
to free memory used by the data structure when you're done using it.
from hashmap.
Related Issues (15)
- Is this hashmap thread-safe? HOT 1
- A way to use a pointer as key? without compiler warnings HOT 2
- modifying key interface requirements HOT 4
- table pointer is NULL HOT 8
- Suggestion: Consider add this library to clib https://github.com/clibs/clib? HOT 4
- Traversal error while adding HOT 1
- Add save and load from file HOT 1
- incovenient function type in hashmap_set_key_alloc_funcs HOT 1
- Multiple errors with typeof HOT 1
- -Wdiscarded-qualifiers at hahsmap.h:29:16 HOT 2
- Incompatibility with the c2x standard
- Random iteration? HOT 1
- Declaration of non-local variable in 'for' loop HOT 5
- How to build in nodebug mode HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hashmap.