# Handling Arrays in C++ Functions on PYNQ: Address Offsets and Memory Access in Jupyter Notebook

**URL:** <https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914>\
**Category:** Support\
**Created:** [January 9, 2025, 9:27am UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914 "2025-01-09T09:27:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saranathan](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@Saranathan](https://discuss.pynq.io/u/Saranathan)\
**Post date:** [January 9, 2025, 9:27am UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914/1 "2025-01-09T09:27:18Z")

</div>

I am using a PYNQ board to accelerate a function with arrays as input and output. I wrote the code in C++ using Vitis 2024, generated an IP block, integrated it into a block design in Vivado, generated the bitstream, and used it in a Jupyter notebook.

When I use simple examples of C++ functions with integers as input and output, I can see the register offset addresses of the variables (from S\_AXI\_LITE) in the generated TCL file. This allows me to write into the addresses from the Jupyter notebook. However, when I use arrays as input and output in my C++ code, the generated bitstream’s TCL file does not contain address offsets for the arrays, and parts of the code show “failed.”

1. How should I write my C++ code to handle arrays as input and output such that the address offsets are correctly generated and accessible in the TCL file?
2. How can I write an array into memory addresses from Python in a Jupyter notebook for this setup?

---

<div class="post-metadata">

**Author:** ![matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@matthew](https://discuss.pynq.io/u/matthew)\
**Post date:** [January 9, 2025, 8:47pm UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914/2 "2025-01-09T20:47:27Z")

</div>

Hi,  
Can you please show the block Vivado design you are using?  
Isn’t the protocol AXI\_LITE for transmiting configuration data and not variables?

---

<div class="post-metadata">

**Author:** ![Saranathan](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@Saranathan](https://discuss.pynq.io/u/Saranathan)\
**Post date:** [January 10, 2025, 4:26am UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914/3 "2025-01-10T04:26:28Z")

</div>

![Block Diagram](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/0/0840597d97b78b4915ddb59f996e8b5b17e49766.jpeg)  
I am not very familiar with the AXI-Lite protocol and would appreciate any guidance, examples, or resources for implementing this.  
I have followed an Youtube Lecture which does something like this for handling lab\_1.cpp file is "# include “lab\_1.h”

void lab\_1(data\_t A,data\_t B,data\_t C, data\_t &Y){  
#pragma HLS INTERFACE mode=s\_axilite port=A  
#pragma HLS INTERFACE mode=s\_axilite port=B  
#pragma HLS INTERFACE mode=s\_axilite port=C  
#pragma HLS INTERFACE mode=s\_axilite port=Y  
#pragma HLS INTERFACE mode=ap\_ctrl\_none port=return  
Y = 2\*A+B-C;  
}" and lab\_1.h file is "# ifndef \_LAB\_1

# define \_LAB\_1

# include \<ap\_int.h\>

typedef ap\_int\<32\> data\_t;

void lab\_1(data\_t A,data\_t B,data\_t C, data\_t &Y);

#endif  
" Using this, I can view the register addresses in the generated TCL file (via S\_AXI\_LITE) and write values into the addresses from a Jupyter notebook.

When I extend this approach to use arrays as inputs and outputs, the generated TCL file does not show address offsets for the arrays, and some parts of the file indicate "failed.

1. How can I modify my C++ code to work with arrays as inputs and outputs while ensuring address offsets for the arrays are properly generated?
2. How can I write and read arrays into/from memory addresses in Python (using Jupyter notebooks) for this setup?

---

<div class="post-metadata">

**Author:** ![matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@matthew](https://discuss.pynq.io/u/matthew)\
**Post date:** [January 10, 2025, 6:22pm UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914/4 "2025-01-10T18:22:20Z")

</div>

What is the function using arrays you want to accelerate?

Could you please provide the jupyter notebook you used to test your bitstream? And what error was displayed?

Could you please also indicates the youtube video you used to create this block in C++ via Vitis?  
Why is there a “Y=2\*A+B-C” in that code?

---

<div class="post-metadata">

**Author:** ![marioruiz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/marioruiz/32/2177_2.png) [@marioruiz](https://discuss.pynq.io/u/marioruiz)\
**Post date:** [January 13, 2025, 12:11pm UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914/5 "2025-01-13T12:11:12Z")

</div>

Hi @Saranathan,

When using arrays in you need to check the offsets in the driver (search for a `driver` directory inside of the Vitis HLS project) code that is automatically generated by Vitis HLS.

Unfortunately, this information is not embedded in the IP.

Mario

---

<div class="post-metadata">

**Author:** ![Saranathan](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@Saranathan](https://discuss.pynq.io/u/Saranathan)\
**Post date:** [January 21, 2025, 4:44am UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914/6 "2025-01-21T04:44:38Z")

</div>

Thanks @marioruiz, Now i am able to read the array’s and write into array’s.

---

<div class="post-metadata">

**Author:** ![marioruiz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/marioruiz/32/2177_2.png) [@marioruiz](https://discuss.pynq.io/u/marioruiz)\
**Post date:** [January 21, 2025, 8:01am UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914/7 "2025-01-21T08:01:55Z")

</div>

Hi @Saranathan,

If your issue is solved, please mark my answer as correct or provide the solution so someone else can benefit from it as well.

Mario

---

<div class="post-metadata">

**Author:** ![Saranathan](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@Saranathan](https://discuss.pynq.io/u/Saranathan)\
**Post date:** [January 26, 2025, 2:51pm UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914/8 "2025-01-26T14:51:31Z")

</div>

Will do, so there is the .h file in the driver folder as you mentioned, and I am able to find the base address of my Array. I work with array by using write command in python which is able to write 4 bytes of data onto the given offset address. So I write in multiple of 4 bytes into the array. If anyone have a better idea of writing into the array’s please mention it below. I have uploaded the image of the .h file for reference.

 ![image](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/1/1875918f6f7e3d8c7a6bfe64b6ed246e09a2a68d.png)

---

<div class="post-metadata">

**Author:** ![marioruiz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/marioruiz/32/2177_2.png) [@marioruiz](https://discuss.pynq.io/u/marioruiz)\
**Post date:** [January 27, 2025, 4:36pm UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914/9 "2025-01-27T16:36:35Z")

</div>

Hi @Saranathan,

The `DefaultIP` exposes the memory mapped registers in the `.mmio` numpy array.  
So, you could do `.mmio[7:10] = [<your array>]` this will still issue write and read operations under the hood.

Mario

---

<div class="post-metadata">

**Author:** ![Saranathan](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@Saranathan](https://discuss.pynq.io/u/Saranathan)\
**Post date:** [January 27, 2025, 5:02pm UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914/10 "2025-01-27T17:02:51Z")

</div>

H be d b b d

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/system/32/2_2.png) [@system](https://discuss.pynq.io/u/system)\
**Post date:** [January 30, 2025, 5:03pm UTC](https://discuss.pynq.io/t/handling-arrays-in-c-functions-on-pynq-address-offsets-and-memory-access-in-jupyter-notebook/7914/11 "2025-01-30T17:03:00Z")

</div>

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
