# Allocating Pointer to Pointer inside a Kernel?

**URL:** <https://forums.developer.nvidia.com/t/allocating-pointer-to-pointer-inside-a-kernel/286660>\
**Category:** CUDA Programming and Performance\
**Created:** [March 20, 2024, 7:42am UTC](https://forums.developer.nvidia.com/t/allocating-pointer-to-pointer-inside-a-kernel/286660 "2024-03-20T07:42:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdopensky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Abdopensky](https://forums.developer.nvidia.com/u/Abdopensky)\
**Post date:** [March 20, 2024, 7:42am UTC](https://forums.developer.nvidia.com/t/allocating-pointer-to-pointer-inside-a-kernel/286660/1 "2024-03-20T07:42:20Z")

</div>

Hello,

Here is the following code where I am stuck at (-lineinfo) and I want to know if the double pointer allocation is allowed within a cuda kernel.

```auto
__global__ void LAUNCHBOUNDS(MY_KERNEL_MAX_THREADS) Kernel()
{
	double**matrix = (double**)malloc(6 * sizeof(double*)),
		**invmatrix = (double**)malloc(6 * sizeof(double*));
	for (int i = 0; i < 6; i++)
	{
		matrix[i] = (double*)malloc(6 * sizeof(double));
		invmatrix[i] = (double*)malloc(6 * sizeof(double));
	}
	matrix[0][0] = 1.0; matrix[0][1] = 1.0; matrix[0][2] = 1.0; matrix[0][3] = 1.0; matrix[0][4] = 1.0; matrix[0][5] = 1.0;
	matrix[1][0] = 1.0; matrix[1][1] = 1.0; matrix[1][2] = 1.0; matrix[1][3] = 1.0; matrix[1][4] = 1.0; matrix[1][5] = 1.0;
	matrix[2][0] = 1.0; matrix[2][1] = 1.0; matrix[2][2] = 1.0; matrix[2][3] = 1.0; matrix[2][4] = 1.0; matrix[2][5] = 1.0;
	matrix[3][0] = 1.0; matrix[3][1] = 1.0; matrix[3][2] = 1.0; matrix[3][3] = 1.0; matrix[3][4] = 1.0; matrix[3][5] = 1.0;
	matrix[4][0] = 1.0; matrix[4][1] = 1.0; matrix[4][2] = 1.0; matrix[4][3] = 1.0; matrix[4][4] = 1.0; matrix[4][5] = 1.0;
	matrix[5][0] = 1.0; matrix[5][1] = 1.0; matrix[5][2] = 1.0; matrix[5][3] = 1.0; matrix[5][4] = 1.0; matrix[5][5] = 1.0;
	for (int i = 0; i < 6; i++)
	{
		free(matrix[i]);
		free(invmatrix[i]);
	}
	free(matrix);
	free(invmatrix);
}

```

Thanks

---

<div class="post-metadata">

**Author:** ![striker159](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@striker159](https://forums.developer.nvidia.com/u/striker159)\
**Post date:** [March 20, 2024, 7:51am UTC](https://forums.developer.nvidia.com/t/allocating-pointer-to-pointer-inside-a-kernel/286660/2 "2024-03-20T07:51:45Z")

</div>

Yes, double pointer allocation is allowed in a kernel

---

<div class="post-metadata">

**Author:** ![Abdopensky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Abdopensky](https://forums.developer.nvidia.com/u/Abdopensky)\
**Post date:** [March 20, 2024, 7:55am UTC](https://forums.developer.nvidia.com/t/allocating-pointer-to-pointer-inside-a-kernel/286660/3 "2024-03-20T07:55:52Z")

</div>

Ok. But why i got a -lineinfo pointed to `matrix[i] = (double*)malloc(6 * sizeof(double));`  
. It seems to be a memory issue i got from there.

---

<div class="post-metadata">

**Author:** ![Abdopensky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Abdopensky](https://forums.developer.nvidia.com/u/Abdopensky)\
**Post date:** [March 20, 2024, 7:58am UTC](https://forums.developer.nvidia.com/t/allocating-pointer-to-pointer-inside-a-kernel/286660/4 "2024-03-20T07:58:06Z")

</div>

I tried to use `compute-sanitizer --tool memcheck AppName` and All i got as errors is : `Internal Sanitizer Error: The Sanitizer failed to handle a hardware exception.`

---

<div class="post-metadata">

**Author:** ![Abdopensky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Abdopensky](https://forums.developer.nvidia.com/u/Abdopensky)\
**Post date:** [March 20, 2024, 8:11am UTC](https://forums.developer.nvidia.com/t/allocating-pointer-to-pointer-inside-a-kernel/286660/5 "2024-03-20T08:11:20Z")

</div>

Here is what i got from Visual Studio :  
 ![image](https://global.discourse-cdn.com/nvidia/original/4X/e/6/9/e697311246ff41d734806a5f529c17b9b664c6a8.png)

---

<div class="post-metadata">

**Author:** ![striker159](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@striker159](https://forums.developer.nvidia.com/u/striker159)\
**Post date:** [March 20, 2024, 8:21am UTC](https://forums.developer.nvidia.com/t/allocating-pointer-to-pointer-inside-a-kernel/286660/6 "2024-03-20T08:21:50Z")

</div>

You need to check if malloc returns nullptr.  
The device heap has limited size. Its size can be set via `cudaDeviceSetLimit` ([CUDA Runtime API :: CUDA Toolkit Documentation](https://docs.nvidia.com/cuda/cuda-runtime-api/group __CUDART__ DEVICE.html#group __CUDART__ DEVICE_1g05956f16eaa47ef3a4efee84563ccb7d))

If you need more help, please post a minimal complete reproducer for your problem.

---

<div class="post-metadata">

**Author:** ![Abdopensky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Abdopensky](https://forums.developer.nvidia.com/u/Abdopensky)\
**Post date:** [March 20, 2024, 8:49am UTC](https://forums.developer.nvidia.com/t/allocating-pointer-to-pointer-inside-a-kernel/286660/7 "2024-03-20T08:49:53Z")

</div>

All right, i will try it later. But will the increasing limit impact the Kernel Performance?

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/system/32/68080_2.png) [@system](https://forums.developer.nvidia.com/u/system)\
**Post date:** [April 3, 2024, 8:50am UTC](https://forums.developer.nvidia.com/t/allocating-pointer-to-pointer-inside-a-kernel/286660/8 "2024-04-03T08:50:24Z")

</div>

Closed on April 3, 2024, 8:50am UTC

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