# Memory Management

**URL:** https://openfhe.discourse.group/t/memory-management/452
**Category:** Library Questions
**Tags:** questions
**Created:** [April 6, 2023, 11:45am UTC](https://openfhe.discourse.group/t/memory-management/452 "2023-04-06T11:45:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![LHenke](https://avatars.discourse-cdn.com/v4/letter/l/c0e974/32.png) [@LHenke](https://openfhe.discourse.group/u/LHenke)
#### Post date: [April 6, 2023, 11:45am UTC](https://openfhe.discourse.group/t/memory-management/452/1 "2023-04-06T11:45:08Z")

</div>

Hi all,

I have a question about how the pointers on OpenFHE objects work. So as far as I understood it, an object of classes like `Ciphertext<DCRTPoly>` or `Cryptocontext<DCRTPoly>` are really (shared?) pointers to implemented objects. This means, that if I call an object of that kind by value and not by reference, it does not use a the huge amount of memory a large ciphertext might use. Now my question is how for example the `EvalAdd` or `EvalMult` methods handle that. So if I have the code snippet

```auto
Ciphertext<DCRTPoly> calculateMean(Ciphertext<DCRTPoly> input, CryptoContext<DCRTPoly> context, unsigned int N) {
    input = context->EvalSum(input, N);
    input = context->EvalMult(input, (double) 1./N);

    return input;
}

```

does the `EvalSumm` method then allocate memory to the resulting object and keep it till the next assignment of `input` variable, or when is that memory freed?

Thanks in advance and cheers

PS: Sorry if this is more of a C++ question than an OpenFHE question 🙂

---

<div class="post-metadata">

### Author: ![jbates-duality](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jbates-duality](https://openfhe.discourse.group/u/jbates-duality)
#### Post date: [April 7, 2023, 2:25pm UTC](https://openfhe.discourse.group/t/memory-management/452/2 "2023-04-07T14:25:49Z")

</div>

Good day.

Your intuition is correct in that the way that you are using the variable _input_ in your code will cause the contents of the local result object to be newly allocated within the Eval\*() operations, and assigned to the shared pointer _input_ each time the assignment operator is used.

Further, this code will act as one would expect when passing by value, in that the caller’s version of _input_ will remain unchanged when calculateMean() returns.

When passing by value in this manner, the reference count of the shared pointer is incremented when it is copied, in order to be placed on the stack. When making the first assignment, that reference count will be decremented when the new version of _input_ is created by the assignment operator.

While this is mostly a C++ question, it is informative to the community at large. We are happy to help foster understanding of our use of modern C++ features within OpenFHE. If you have further questions, please feel free to ask them.

Regards,

Jack Bates  
Duality

---

<div class="post-metadata">

### Author: ![LHenke](https://avatars.discourse-cdn.com/v4/letter/l/c0e974/32.png) [@LHenke](https://openfhe.discourse.group/u/LHenke)
#### Post date: [April 11, 2023, 8:49am UTC](https://openfhe.discourse.group/t/memory-management/452/3 "2023-04-11T08:49:23Z")

</div>

Thanks a lot for the answer! So this means that it is not really practicable to call OpenFHE objects with `const <objectname>&` in order to save memory, if a copy of the variable doesn’t need to be changed within a function or method, since it only copies the size of the pointer variable to memory?
