# How to add element(int64 key and as\_map value) to as\_map?

**URL:** <https://discuss.aerospike.com/t/how-to-add-element-int64-key-and-as-map-value-to-as-map/1018>\
**Category:** C Client Library\
**Created:** [March 5, 2015, 11:46am UTC](https://discuss.aerospike.com/t/how-to-add-element-int64-key-and-as-map-value-to-as-map/1018 "2015-03-05T11:46:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Liu\_Ld](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/liu_ld/32/334_2.png) [@Liu\_Ld](https://discuss.aerospike.com/u/Liu_Ld)\
**Post date:** [March 5, 2015, 11:46am UTC](https://discuss.aerospike.com/t/how-to-add-element-int64-key-and-as-map-value-to-as-map/1018/1 "2015-03-05T11:46:52Z")

</div>

Something like as\_intmap\_set\_map(p\_map, 12 , p\_value)?

---

<div class="post-metadata">

**Author:** ![wchu](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@wchu](https://discuss.aerospike.com/u/wchu)\
**Post date:** [March 6, 2015, 5:51am UTC](https://discuss.aerospike.com/t/how-to-add-element-int64-key-and-as-map-value-to-as-map/1018/2 "2015-03-06T05:51:13Z")

</div>

as\_map has a generic key, value function, which can take any type

int as\_hashmap\_set(as\_hashmap \* map, const as\_val \* key, const as\_val \* val);

---

<div class="post-metadata">

**Author:** ![Liu\_Ld](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/liu_ld/32/334_2.png) [@Liu\_Ld](https://discuss.aerospike.com/u/Liu_Ld)\
**Post date:** [March 6, 2015, 6:11am UTC](https://discuss.aerospike.com/t/how-to-add-element-int64-key-and-as-map-value-to-as-map/1018/3 "2015-03-06T06:11:54Z")

</div>

Code below:

static inline int as\_int64map\_set\_int64(as\_map \* m, int64\_t k, int64\_t v)

{

```
 return as_util_hook(set, 1, m, (as_val *) as_integer_new(k), (as_val *) as_integer_new(v));

```

}

static inline int64\_t as\_int64map\_get\_int64(as\_map \* m, int64\_t k)

{

```
 as_integer key;

 as_val * v = as_util_hook(get, NULL, m, (as_val *) as_integer_init(&key, k));

 as_integer * i = as_integer_fromval(v);

 return i ? as_integer_toint(i) : 0;

```

}

---

<div class="post-metadata">

**Author:** ![wchu](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@wchu](https://discuss.aerospike.com/u/wchu)\
**Post date:** [March 7, 2015, 6:45am UTC](https://discuss.aerospike.com/t/how-to-add-element-int64-key-and-as-map-value-to-as-map/1018/4 "2015-03-07T06:45:04Z")

</div>

The added helper functions are good. Alternatively, using existing API:

```
// Setting an integer
as_map_set(m, (as_val *) as_integer_new(1), (as_val *) as_integer_new(10000));

// Getting an integer
as_integer_init(&key, 1);
as_integer_get((as_integer *)as_map_get(m, (as_val *)&key)));

```

---

<div class="post-metadata">

**Author:** ![Liu\_Ld](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/liu_ld/32/334_2.png) [@Liu\_Ld](https://discuss.aerospike.com/u/Liu_Ld)\
**Post date:** [March 7, 2015, 8:09am UTC](https://discuss.aerospike.com/t/how-to-add-element-int64-key-and-as-map-value-to-as-map/1018/5 "2015-03-07T08:09:51Z")

</div>

Thanks for your reply.

---

<div class="post-metadata">

**Author:** ![serp](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@serp](https://discuss.aerospike.com/u/serp)\
**Post date:** [July 19, 2018, 2:20am UTC](https://discuss.aerospike.com/t/how-to-add-element-int64-key-and-as-map-value-to-as-map/1018/6 "2018-07-19T02:20:56Z")

</div>

Do I assume correctly that if this example is used as-is, it would result in memory leak, since as\_integer\_new allocates an as\_integer on heap, and it must be then freed with as\_integer\_destroy, which is missing from this example? And if so, it would also mean that in order to be able to call as\_integer\_destroy, you HAVE to store the as\_integer (or a pointer to it) in some variable (like the “key” above). Is this correct?

---

<div class="post-metadata">

**Author:** ![Brian](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/brian/32/2211_2.png) [@Brian](https://discuss.aerospike.com/u/Brian)\
**Post date:** [July 19, 2018, 5:09pm UTC](https://discuss.aerospike.com/t/how-to-add-element-int64-key-and-as-map-value-to-as-map/1018/7 "2018-07-19T17:09:09Z")

</div>

as\_hashmap takes ownership of keys/values and destroys them when the entry is removed. There will be no memory leak if the hashmap itself is eventually destroyed.
