Coder Social home page Coder Social logo

Comments (6)

DavidLeeds avatar DavidLeeds commented on August 16, 2024

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.

lpipes avatar lpipes commented on August 16, 2024

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.

lpipes avatar lpipes commented on August 16, 2024

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.

fancong5201314 avatar fancong5201314 commented on August 16, 2024

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.

fancong5201314 avatar fancong5201314 commented on August 16, 2024

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.

DavidLeeds avatar DavidLeeds commented on August 16, 2024

A few notes:

  1. In the original code, the FindABC() declaration was not visible to main.c. The second (working) version moved the function to main.c where it was visible.
  2. There is no need to include hashmap_base.h. Just include hashmap.h.
  3. 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.