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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:04:41+00:00 2026-06-18T08:04:41+00:00

Following sample problem: #include <iostream> using namespace std; __device__ __constant__ float* data; template<class T>

  • 0

Following sample problem:

#include <iostream>

using namespace std;
__device__ __constant__ float* data;

template<class T> void allocOnly(T* deviceDest, size_t numElem)
{
    cudaError_t errCode = cudaMalloc((void**)&deviceDest, numElem*sizeof(T));
    if(errCode != cudaSuccess) 
        cout << "Got error with code " << errCode << endl;
}

int main()
{
    float* test(0);
    allocOnly<float>(test,10);
    cout << "test = " << test << endl;

    float* test2(0);    
    cudaError_t errCode = cudaMalloc((void**)&test2, 10*sizeof(float));
    if(errCode != cudaSuccess) 
        cout << "Got error with code " << errCode << endl;
    cout << "test2 = " << test2 << endl;

    return 0;
}

compiled with nvcc test.cu -o testBin

returns

test = 0
test2 = 0x310100

Why is test not modified when called through template function, cudaMalloc is supposed to modify it to be a pointer to the newly allocated device memory!

  • 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-18T08:04:42+00:00Added an answer on June 18, 2026 at 8:04 am

    The pointer is not being modified because cudaMalloc in the function allocOnly is allocating memory to the argument deviceTest which is local to the function allocOnly. You can modify the function allocOnly to allocate memory as follows:

    template<class T> void allocOnly(T** deviceDest, size_t numElem)
    {
        cudaError_t errCode = cudaMalloc((void**)deviceDest, numElem*sizeof(T));
        if(errCode != cudaSuccess) 
            cout << "Got error with code " << errCode << endl;
    }
    

    Inside the main function:

    int main()
    {
        float* test(0);
        allocOnly<float>(&test,10);
        cout << "test = " << test << endl;
        .
        .
        .
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following example: #include <iostream> #include <iostream> #include <type_traits> template<typename Type, template<typename> class
Given this code sample: complex.h : #ifndef COMPLEX_H #define COMPLEX_H #include <iostream> class Complex
I have the following code: #include <memory> class Foo; typedef std::tr1::shared_ptr<Foo> pFoo_t; class DoSomething
My problem is a bit more complex than using the following simple JavaScript code:
I am using the following function to create a simple accordion. The problem is
Hi following code sample seems to have some problem or either it has to
I have the following problem. I create a class, and store the pointer to
This problem has been haunting me for weeks. I am using MS vs2010. #include
Consider the following class member: std::vector<sim_mob::Lane *> IncomingLanes_; the above container shall store the
i have an problem with antlr. I have the following simple grammar: grammar bxk;

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.