Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:09:32+00:00 2026-06-12T07:09:32+00:00

is it valid to free device allocated memory from a host function? I’m writing

  • 0

is it valid to free device allocated memory from a host function?
I’m writing some C++ class that should be used on host and device. My constructor and destructor are something like this:

class myClass {
public:
__host__ __device__ myClass() {
#if defined(__CUDA_ARCH__)
  data = (char*)malloc(DATA_SIZE);
#else
  cudaMalloc(&data,DATA_SIZE);
#endif
}

__host__ __device__ ~myClass() {
#if defined(__CUDA_ARCH__)
  free(data);
#else
  cudaFree(data);
#endif
}

private:
  char* data;
}

The above code compiles and i didn’t get an error if i construct a class on the device and free it on the host. But this case is not documented in the CUDA developer papers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:09:33+00:00Added an answer on June 12, 2026 at 7:09 am

    For both CUDA 4.2 and CUDA 5.0 RC, the CUDA C Programmer’s guide mentions in section B.17 that: “Memory allocated via malloc() cannot be freed using the runtime (i.e., by calling any
    of the free memory functions from Device Memory). ” (This particular text taken from the CUDA 5.0 RC document. In the original doc, Device Memory is a hyperlink to section 3.2.2) The CUDA 4.2 document can be found here (it has similar wording.) I’m wondering if: 1.) Things are actually happening the way you think. Depending on how you allocate the variable in the kernel code, I think it could go out of scope at the completion of the kernel, which would implicitly call your (device-side) destructor. 2.) a call to cudaFree with an invalid pointer (perhaps because it is a device pointer, or perhaps because it has already been freed) simply gets ignored. Without your code to play with, it’s all just speculation. But if you’re checking for errors and not getting one, then it may be getting ignored.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Theoretically I can say that free(ptr); free(ptr); is a memory corruption since we are
Currently working on a flashchat that connects to red5. The webapp used: http://www.freedevelopment.net/articles/free-red5chat.html Users
I have a valid SAML 2 token from my application IdP: When I try
i get a valid fd object from a caller. How can i find out
I copied a multidimensional array from java, but i am having some problems to
I have downloaded a free astrology script in PHP. The script receives some data
The pseudo code is in notes but I need to define a function that
I know that an invalid pointer leads to undefined behaviour but how does free
Imagine I have the following free function and functor: void myFreeFunction(void) { cout <<
i'm making a memory allocation/deallocation function it's simple inline void safedealloc ( void *mem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.