# String vs Array of Char

**URL:** <https://community.particle.io/t/string-vs-array-of-char/9816>\
**Category:** General\
**Created:** [February 5, 2015, 10:22am UTC](https://community.particle.io/t/string-vs-array-of-char/9816 "2015-02-05T10:22:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![softmeter](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@softmeter](https://community.particle.io/u/softmeter)\
**Post date:** [February 5, 2015, 10:22am UTC](https://community.particle.io/t/string-vs-array-of-char/9816/1 "2015-02-05T10:22:44Z")

</div>

I’ve read a lot about the String implementation for Spark.

Something sounds good (dynamic allocation),

Something sounds hard (HEAP Fragmentation),

Something sounds strange (garbage collection) for 20kbyte of RAM.

On the other side, firmeware functions like publish etc. are supporting the String implementation.

So, finally:

Is it safe to use String (also on the long run) or not ?  
Are there functions to get the current HEAPSize ?

Thx to all for supporting this great platform

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [February 5, 2015, 1:20pm UTC](https://community.particle.io/t/string-vs-array-of-char/9816/2 "2015-02-05T13:20:33Z")

</div>

As for the current heap size you might like to have a look in this thread and the referenced source files.

> **[Can Spark Core's malloc implementation fail gracefully?](https://community.particle.io/t/can-spark-cores-malloc-implementation-fail-gracefully/8707)**
>
> Hi everyone, I have a number of questions about memory allocation on the Core. In principle, when malloc is unable to satisfy a request, it’s supposed to fail by returning a null pointer - will the Core’s malloc implementation ever do this? In...

As for the risks connected with the use of `String` it greatly depends on your particular usage. But to know what might prove risky and what would be safe, a look into the current implementation of `String` might give you a feeling.

[https://github.com/spark/firmware/blob/master/src/spark\_wiring\_string.cpp](https://github.com/spark/firmware/blob/master/src/spark_wiring_string.cpp)

When I mentioned garbage collection in the other thread I more exactly should have said somthing like "heap defragmentation/compression".  
If you use a lot of `String` objects of which you (or implicit reallocation) free some and some others not, you might run out of blocks big enough to create/modify a `String` object despite having enough free space but only scattered all over the heap.  
And since user mem, stack and heap share the Cores 20kB you'll sometimes hit the limits without reason - as it seems. But knowing about possible pitfalls, will safe you some head-scratching 😉

---

<div class="post-metadata">

**Author:** ![softmeter](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@softmeter](https://community.particle.io/u/softmeter)\
**Post date:** [February 5, 2015, 1:45pm UTC](https://community.particle.io/t/string-vs-array-of-char/9816/3 "2015-02-05T13:45:45Z")

</div>

Thanx for your answer. I’ve tried the function \_sbrk or this one:

```auto
uint32_t freeMemoryAvailable(void)
{
    extern char _end;
    char *current_heap_end = &_end;
    char *current_stack_pointer = (char *)__get_MSP();

    return (current_stack_pointer - current_heap_end);
}

```

but they return always the same number of bytes (the same like the compiler). We are using String very often to save data and send them to a Server using HTTPClient Lib. But the memory size does not change over time ?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [February 6, 2015, 2:45pm UTC](https://community.particle.io/t/string-vs-array-of-char/9816/4 "2015-02-06T14:45:58Z")

</div>

Free mem estimation is going on here

> **[Measuring Memory on your Spark](https://community.particle.io/t/measuring-memory-on-your-spark/9844)**
>
> Hi, i’ve had a hard time finding my way to do complex things with the Spark, cause of the very limited amount of memory. I want to share my findings with you. There a two types of dynamical Memory (HEAP and Stack). The Heap starts on the...
