{
"cells": [
{
"cell_type": "markdown",
"id": "d65e03c8",
"metadata": {},
"source": [
"# vo001_AdoptOrOwnMemory\n",
"In this tutorial we learn how the RVec class can be used to\n",
"adopt existing memory or allocate some.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Danilo Piparo \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 19, 2024 at 07:22 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "9e0d670b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:22:39.760863Z",
"iopub.status.busy": "2024-03-19T19:22:39.760565Z",
"iopub.status.idle": "2024-03-19T19:22:39.783060Z",
"shell.execute_reply": "2024-03-19T19:22:39.781871Z"
}
},
"outputs": [],
"source": [
"%%cpp -d"
]
},
{
"cell_type": "markdown",
"id": "d5fb92ac",
"metadata": {},
"source": [
"We use this class for didactic purposes: upon copy, a line is printed to the terminal."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "776df73c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:22:39.792159Z",
"iopub.status.busy": "2024-03-19T19:22:39.791788Z",
"iopub.status.idle": "2024-03-19T19:22:40.631811Z",
"shell.execute_reply": "2024-03-19T19:22:40.630351Z"
}
},
"outputs": [],
"source": [
"class UponCopyPrinter {\n",
"public:\n",
" UponCopyPrinter() = default;\n",
" UponCopyPrinter(UponCopyPrinter &&) = default;\n",
" UponCopyPrinter(const UponCopyPrinter &) { std::cout << \"Invoking copy c'tor!\" << std::endl; }\n",
"};"
]
},
{
"cell_type": "markdown",
"id": "d37c7537",
"metadata": {},
"source": [
"One of the essential features of RVec is its ability of adopting and owning memory."
]
},
{
"cell_type": "markdown",
"id": "350c3a11",
"metadata": {},
"source": [
"Let's create an RVec of UponCopyPrinter instances. We expect no printout:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b9869444",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:22:40.637189Z",
"iopub.status.busy": "2024-03-19T19:22:40.636829Z",
"iopub.status.idle": "2024-03-19T19:22:41.050295Z",
"shell.execute_reply": "2024-03-19T19:22:41.049188Z"
}
},
"outputs": [],
"source": [
"ROOT::RVec v(3);"
]
},
{
"cell_type": "markdown",
"id": "83ef0700",
"metadata": {},
"source": [
"Let's adopt the memory from v into v2. We expect no printout:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3b7825a4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:22:41.060831Z",
"iopub.status.busy": "2024-03-19T19:22:41.060464Z",
"iopub.status.idle": "2024-03-19T19:22:41.285802Z",
"shell.execute_reply": "2024-03-19T19:22:41.284420Z"
}
},
"outputs": [],
"source": [
"ROOT::RVec v2(v.data(), v.size());"
]
},
{
"cell_type": "markdown",
"id": "f42a7437",
"metadata": {},
"source": [
"OK, let's check the addresses of the memory associated to the two RVecs It is the same!"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "611338cf",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:22:41.292494Z",
"iopub.status.busy": "2024-03-19T19:22:41.292154Z",
"iopub.status.idle": "2024-03-19T19:22:41.520230Z",
"shell.execute_reply": "2024-03-19T19:22:41.518767Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0x7ff30000c010 and 0x7ff30000c010\n"
]
}
],
"source": [
"std::cout << v.data() << \" and \" << v2.data() << std::endl;"
]
},
{
"cell_type": "markdown",
"id": "d1e21511",
"metadata": {},
"source": [
"Now, upon reallocation, the RVec stops adopting the memory and starts owning it. And yes,\n",
"a copy is triggered. Indeed internally the storage of the RVec is an std::vector. Moreover,\n",
"the interface of the RVec is very, very similar to the one of std::vector: you have already\n",
"noticed it when the `data()` method was invoked, right?"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "fcabd67e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:22:41.525427Z",
"iopub.status.busy": "2024-03-19T19:22:41.525094Z",
"iopub.status.idle": "2024-03-19T19:22:41.752218Z",
"shell.execute_reply": "2024-03-19T19:22:41.750429Z"
}
},
"outputs": [],
"source": [
"v2.push_back(UponCopyPrinter());"
]
},
{
"cell_type": "markdown",
"id": "777ff72d",
"metadata": {},
"source": [
"Of course, now the addresses are different."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ee8c293e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:22:41.757836Z",
"iopub.status.busy": "2024-03-19T19:22:41.757454Z",
"iopub.status.idle": "2024-03-19T19:22:41.983746Z",
"shell.execute_reply": "2024-03-19T19:22:41.982733Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0x7ff30000c010 and 0x7ff2ed25c0f0\n"
]
}
],
"source": [
"std::cout << v.data() << \" and \" << v2.data() << std::endl;"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}