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 7570385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:24:42+00:00 2026-05-30T15:24:42+00:00

I have a code that has a large number of malloc s and device-specific

  • 0

I have a code that has a large number of mallocs and device-specific API mallocs (I’m programming on a GPU, so cudaMalloc).

Basically my end of my beginning of my code is a big smorgasbord of allocation calls, while my closing section is deallocation calls.

As I’ve encapsulated my global data in structures, the deallocations are quite long, but at least I can break them into a separate function. On the other hand, I would like a shorter solution. Additionally an automatic deallocator would reduce the risk of memory leaks created if I forget to explicitly write the deallocation in the global allocator function.

I was wondering whether it’d be possible to write some sort of templated class wrapper that can allow me to “register” variables during the malloc/cudaMalloc process, and then at the end of simulation do a mass loop-based deallocation (deregistration). To be clear I don’t want to type out individual deallocations (free/cudaFrees), because again this is long and undesirable, and the assumption would be that anything I register won’t be deallocated until the device simulation is complete and main is terminating.

A benefit here is that if I register a new simulation duration variable, it will automatically deallocate, so there’s no danger of me forgetting do deallocate it and creating a memory leak.

Is such a wrapper possible?

Would you suggest doing it?

If so, how?

Thanks in advance!

  • 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-05-30T15:24:43+00:00Added an answer on May 30, 2026 at 3:24 pm

    An idea:

    Create both functions, one that allocates memory and provides valid pointers after register them in a “list” of allocated pointers. In the second method, loop this list and deallocate all pointers:

    // ask for new allocated pointer that will be registered automatically in list of pointers.
    pointer1 = allocatePointer(size, listOfPointers);
    pointer2 = allocatePointer(size, listOfPointers);
    
    ...
    // deallocate all pointers
    deallocatePointers(listOfPointers);
    

    Even, you may use different listOfPointers depending of your simulation scope:

    listOfPointer1 = getNewListOfPointers();
    listOfPointer2 = getNewListOfPointers();
    ....
    p1 = allocatePointer(size, listOfPointer1);
    p2 = allocatePointer(size, listOfPointer2);
    ...
    deallocatePointers(listOfPointers1);
    ...
    deallocatePointers(listOfPointers2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very large codebase (read: thousands of modules) that has code shared
You're stepping through C/C++ code and have just called a Win32 API that has
I have a composite control that has a large number of properties that can
I have this code that has one button that let's me choose an entry
I have a code base that has been used as an ASP.Net web application.
I have some older (broken) code that has a join using a *= table1.ID
I have a standalone Java app that has some licensing code that I want
I have a simple jQuery code that runs on a web page that has
I have written a PHP function to take a video embed code that has
I have a HTML file that has code similar to the following. <table> <tr>

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.