# Memory Allocation

You can construct an image using an internal or an external memory allocation.

## Internal memory allocation

The image object dynamically allocates and deallocates a buffer:

* The memory management is transparent.
* When the image size changes, a reallocation occurs.
* When an image object is destroyed, the buffer is deallocated.

To declare an image with an internal memory allocation:

1. Construct an image object, for instance [`EImageBW8`](../../../doxygen_cpp/classeVision_1_1EImageBW8.html) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EImageBW8.html) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EImageBW8.html), either with width and height arguments or using the [`EBaseROI::SetSize`](../../../doxygen_cpp/classeVision_1_1EBaseROI.html#a8446337051c589320cfd2ea1fcbe6d16) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EBaseROI.html#a87cfe59bbf851719b3f65d6766f71dba) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EBaseROI.html#a43f0fddfd46389cc8b95d9ccb608a909) function.
2. Access a given pixel using one of the multiple available functions.<br />
   \\\\
   For example, use [`EBaseROI::GetImagePtr`](../../../doxygen_cpp/classeVision_1_1EBaseROI.html#a8502f1e7a872f0b89edb88f536d88934) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EBaseROI.html#a412c121c9cc5631afda29a1016300386) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EBaseROI.html#a7cd6cfe09b29b127b221a826c0792e8f) to retrieve a pointer to the first byte of the pixel at the given coordinates.

## External memory allocation

Control the buffer allocation or link a third-party image in the memory buffer to an **eVision** image.

* You must specify the image size and the buffer address.
* When an image object is destroyed, the buffer is unaffected.

#### NOTE
For details, see [Image and Depth Map Buffer](../../../conventions/image_and_depth_map_buffer.html.md) and [Interfacing Third-Party Images](../../../resources/code_snippets/basic_operations/basic_types/interfacingthirdpartyimages.html.md).

To declare an image with an external memory allocation:

1. Declare an image object, for instance [`EImageBW8`](../../../doxygen_cpp/classeVision_1_1EImageBW8.html) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EImageBW8.html) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EImageBW8.html).
2. Create a suitably sized and aligned buffer.
3. Assign the buffer to the image with [`EBaseROI::SetImagePtr`](../../../doxygen_cpp/classeVision_1_1EBaseROI.html#a089b7b1556b00b4e54ac3ebf6476ab85) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EBaseROI.html#a17be1e6f0e4891ff967b42d169367e01) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EBaseROI.html#ac9e38af7bc6e045e827d1fd79f21a98e).

#### NOTE
Using the copy constructor of the `EImage` object to copy the externally allocated image does not copy the buffer.<br />
\\\\
The copied image points to the same external buffer as the original image.

#### NOTE
If your buffer rows are not aligned on 4 bytes, use [`EImageBW8::InitializeFromUnalignedBuffer`](../../../doxygen_cpp/classeVision_1_1EImageBW8.html#ab94b5b15e081e810b52ffdfd0795ee19) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EImageBW8.html#a1069a031cdda87d3b05d07b2f98b1035) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EImageBW8.html#a7f94f33cdaf140011a20e8a2d9c13d94) instead of [`EBaseROI::SetImagePtr`](../../../doxygen_cpp/classeVision_1_1EBaseROI.html#a089b7b1556b00b4e54ac3ebf6476ab85) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EBaseROI.html#a17be1e6f0e4891ff967b42d169367e01) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EBaseROI.html#ac9e38af7bc6e045e827d1fd79f21a98e).<br />
\\\\
Please note that this allocates the memory internally and copies the external buffer into the internal one instead of using the external one.
