Giter Site home page Giter Site logo

Custom type data about map HOT 2 OPEN

rxi avatar rxi commented on June 28, 2024
Custom type data

from map.

Comments (2)

Haswf avatar Haswf commented on June 28, 2024

Hi zxyAcmen,

Declaration: I am not the author nor the maintainer of this project.
The map stores data as a Hash table. The value passed into map_set() will be copied and saved to the map. Internally, map calls map_newnode to allocate space, thus the behaviour can be found in map_newnode(). As you can seem, the passing value is copied using memcpy() and saved as node->value To answer your question, the memory allocated for the passing value won't be freed when map_deinit() is called. Only the copy in the node will be freed,

static map_node_t *map_newnode(const char *key, void *value, int vsize) {
  map_node_t *node;
  int ksize = strlen(key) + 1;
  int voffset = ksize + ((sizeof(void*) - ksize) % sizeof(void*));
  node = malloc(sizeof(*node) + voffset + vsize);
  if (!node) return NULL;
  memcpy(node + 1, key, ksize);
  node->hash = map_hash(key);
  node->value = ((char*) (node + 1)) + voffset;
  memcpy(node->value, value, vsize);
  return node;
}

声明:我不是该项目的作者和维护者。

这个Map通过哈希表来储存数据。当你把key和value传进 map_set()的时候,传进去的Value会被使用memcpy(node->value, value, vsize);复制一份存在node->value中。所以当你调用map_deinit()的时候,堆上的空间并不会被释放,只有Node中复制的那一份会被释放。

from map.

QingruiHuang avatar QingruiHuang commented on June 28, 2024

@zxyAcmen

// 正确的使用方法如下,如果涉及到动态分配的数据类型,不建议使用此库。
// map_get(&mMan, key) 所取得的数据,与最初数据是两码事,而且数据指向有耦合。

#define CRTDBG_MAP_ALLOC

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <crtdbg.h>

#include <map/map.h>

struct Man
{
char* name;
};
typedef map_t(struct Man *) map_man_t;

int main(int argc, char* argv[], char* env[])
{
map_man_t mMan;

struct Man* manHg = NULL;

struct Man* manHerry = NULL;

struct Man** man_pp = NULL;

char* key;

map_iter_t iter;

_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);

map_init(&mMan);

manHg = malloc(sizeof(struct Man));
manHg->name = malloc(100);
strcpy(manHg->name, "hg");

manHerry = malloc(sizeof(struct Man));
manHerry->name = malloc(100);
strcpy(manHerry->name, "herry");


map_set(&mMan, "hg", manHg);
map_set(&mMan, "herry", manHerry);

iter = map_iter(&mMan);
while ((key = map_next(&mMan, &iter)))
{
	man_pp = map_get(&mMan, key);

	printf("key:%s --- name:%s\n", key, (*man_pp)->name);
}



free(manHg->name);
free(manHerry->name);

free(manHg);
free(manHerry);

map_deinit(&mMan);

return 0;

}

from map.

Related Issues (10)

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.