# Need help understanding the char buffer of particle publish and subscribe

**URL:** <https://community.particle.io/t/need-help-understanding-the-char-buffer-of-particle-publish-and-subscribe/50563>\
**Category:** Troubleshooting\
**Created:** [June 20, 2019, 10:35pm UTC](https://community.particle.io/t/need-help-understanding-the-char-buffer-of-particle-publish-and-subscribe/50563 "2019-06-20T22:35:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![poolnoodle](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@poolnoodle](https://community.particle.io/u/poolnoodle)\
**Post date:** [June 20, 2019, 10:35pm UTC](https://community.particle.io/t/need-help-understanding-the-char-buffer-of-particle-publish-and-subscribe/50563/1 "2019-06-20T22:35:23Z")

</div>

From the reference

> **NOTE 2:** `Particle.publish()` and the `Particle.subscribe()` handler(s) share the same buffer. As such, calling `Particle.publish()` within a `Particle.subscribe()` handler will wipe the subscribe buffer! In these cases, copying the subscribe buffer's content to a separate char buffer prior to calling `Particle.publish()` is recommended.

I think my code has this problem but I don't get what does it mean by copying the subscribe buffer's content to a separate char buffer. The only char buffer i used in my code is the buffer for sprintf.  
Can anyone give me an example of how this can be achieved?

---

<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:** [June 21, 2019, 7:18am UTC](https://community.particle.io/t/need-help-understanding-the-char-buffer-of-particle-publish-and-subscribe/50563/2 "2019-06-21T07:18:24Z")

</div>

> [@poolnoodle](#):
>
> I think my code has this problem but I don’t get what does it mean by copying the subscribe buffer’s content to a separate char buffer.

A rather simple version would be this

```cpp
void subHandler(const char* event, const char* data) {
  char buf[strlen(data)+1]; // big enough to hold the string including zero-terminator
  strcpy(buf, data); // copy data into buffer
  ...
}

```

---

<div class="post-metadata">

**Author:** ![poolnoodle](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@poolnoodle](https://community.particle.io/u/poolnoodle)\
**Post date:** [June 21, 2019, 4:31pm UTC](https://community.particle.io/t/need-help-understanding-the-char-buffer-of-particle-publish-and-subscribe/50563/3 "2019-06-21T16:31:09Z")

</div>

Thank you! This did fix one of the cause of the crash 🙂
