# Error when allocating mapped memory after allocating other memory

**URL:** https://forums.developer.nvidia.com/t/error-when-allocating-mapped-memory-after-allocating-other-memory/17207
**Category:** CUDA Programming and Performance
**Created:** [June 16, 2010, 10:33am UTC](https://forums.developer.nvidia.com/t/error-when-allocating-mapped-memory-after-allocating-other-memory/17207 "2010-06-16T10:33:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![weichertv](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@weichertv](https://forums.developer.nvidia.com/u/weichertv)
#### Post date: [June 16, 2010, 10:33am UTC](https://forums.developer.nvidia.com/t/error-when-allocating-mapped-memory-after-allocating-other-memory/17207/1 "2010-06-16T10:33:51Z")

</div>

Hello!

I have encountered a weird problem and I wonder whether this is a bug or my bad:

I am trying to allocate mapped memory. That works without a problem, but if I allocated some host memory before (using cudaHostAlloc) I am getting an error. Here is some code that causes the problem:

```auto
#include <stdio.h>

int main() {

	cudaError_t err;

	int *host;

	int host_size = 1073741824;

	cudaSetDevice(0);

	err = cudaHostAlloc((void **) &host, host_size, cudaHostAllocDefault);

	printf("cudaHostAlloc returned %d: %s\n", err, cudaGetErrorString(err));

	cudaFree(host);

	err = cudaHostAlloc((void **) &host, host_size, cudaHostAllocMapped);

	printf("cudaHostAlloc returned %d: %s\n", err, cudaGetErrorString(err));

	cudaFree(host);

	

	return 0;

}

```

The output on my system is

> [@](#):
>
> cudaHostAlloc returned 0: no error
> 
> cudaHostAlloc returned 30: unknown error

No error occurs if the flags of the second cudaHostAlloc are set to cudaHostAllocPortable, cudaHostAllocWriteCombined or cudaHostAllocDefault.

Is there something wrong with my setup? I am using the toolkit version 3.0 on Debian lenny.

Thanks!

Volker

---

<div class="post-metadata">

### Author: ![karhelvasi](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@karhelvasi](https://forums.developer.nvidia.com/u/karhelvasi)
#### Post date: [June 29, 2010, 3:28am UTC](https://forums.developer.nvidia.com/t/error-when-allocating-mapped-memory-after-allocating-other-memory/17207/2 "2010-06-29T03:28:27Z")

</div>

Use

cudaFreeHost

> [@](#):
>
> Hello!
> 
> I have encountered a weird problem and I wonder whether this is a bug or my bad:
> 
> I am trying to allocate mapped memory. That works without a problem, but if I allocated some host memory before (using cudaHostAlloc) I am getting an error. Here is some code that causes the problem:
> 
> ```auto
> #include <stdio.h>
> 
> int main() {
> 
> cudaError_t err;
> 
> int *host;
> 
> int host_size = 1073741824;
> 
> cudaSetDevice(0);
> 
> err = cudaHostAlloc((void **) &host, host_size, cudaHostAllocDefault);
> 
> printf("cudaHostAlloc returned %d: %s\n", err, cudaGetErrorString(err));
> 
> cudaFree(host);
> 
> err = cudaHostAlloc((void **) &host, host_size, cudaHostAllocMapped);
> 
> printf("cudaHostAlloc returned %d: %s\n", err, cudaGetErrorString(err));
> 
> cudaFree(host);
> 
> 	
> 
> return 0;
> 
> }
> 
> ```
> 
> The output on my system is
> 
> No error occurs if the flags of the second cudaHostAlloc are set to cudaHostAllocPortable, cudaHostAllocWriteCombined or cudaHostAllocDefault.
> 
> Is there something wrong with my setup? I am using the toolkit version 3.0 on Debian lenny.
> 
> Thanks!
> 
> Volker

---

<div class="post-metadata">

### Author: ![karhelvasi](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@karhelvasi](https://forums.developer.nvidia.com/u/karhelvasi)
#### Post date: [June 29, 2010, 3:28am UTC](https://forums.developer.nvidia.com/t/error-when-allocating-mapped-memory-after-allocating-other-memory/17207/3 "2010-06-29T03:28:27Z")

</div>

Use

cudaFreeHost

> [@](#):
>
> Hello!
> 
> I have encountered a weird problem and I wonder whether this is a bug or my bad:
> 
> I am trying to allocate mapped memory. That works without a problem, but if I allocated some host memory before (using cudaHostAlloc) I am getting an error. Here is some code that causes the problem:
> 
> ```auto
> #include <stdio.h>
> 
> int main() {
> 
> cudaError_t err;
> 
> int *host;
> 
> int host_size = 1073741824;
> 
> cudaSetDevice(0);
> 
> err = cudaHostAlloc((void **) &host, host_size, cudaHostAllocDefault);
> 
> printf("cudaHostAlloc returned %d: %s\n", err, cudaGetErrorString(err));
> 
> cudaFree(host);
> 
> err = cudaHostAlloc((void **) &host, host_size, cudaHostAllocMapped);
> 
> printf("cudaHostAlloc returned %d: %s\n", err, cudaGetErrorString(err));
> 
> cudaFree(host);
> 
> 	
> 
> return 0;
> 
> }
> 
> ```
> 
> The output on my system is
> 
> No error occurs if the flags of the second cudaHostAlloc are set to cudaHostAllocPortable, cudaHostAllocWriteCombined or cudaHostAllocDefault.
> 
> Is there something wrong with my setup? I am using the toolkit version 3.0 on Debian lenny.
> 
> Thanks!
> 
> Volker

---

<div class="post-metadata">

### Author: ![mirkel](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mirkel](https://forums.developer.nvidia.com/u/mirkel)
#### Post date: [July 4, 2010, 1:54pm UTC](https://forums.developer.nvidia.com/t/error-when-allocating-mapped-memory-after-allocating-other-memory/17207/4 "2010-07-04T13:54:36Z")

</div>

> [@](#):
>
> Use
> 
> cudaFreeHost

Hi,

I am also facing the same problem but with ‘cudaHostAllocPortable’ flag (and I am using ‘cudaFreeHost()’ ).

the error says " unknown error"
