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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:54:15+00:00 2026-06-12T08:54:15+00:00

I have a quite simple question but I don’t get it. I’m working with

  • 0

I have a quite simple question but I don’t get it. I’m working with the 4.2 cuda.

I have the following code:

in my cudaClass.h:

unsigned char *dev_trimapExpanded;

in my cudaClass.cpp:

void cudaClass::expansionTrimap() {
    printf("dev_trimapExpanded %d before function \n", dev_trimapExpanded);
    //Call cuda function
    cudaError_t cudaStatus = expansionTrimapCuda(dev_trimapExpanded, width, height);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "expansionTrimapCuda failed!\n");
    }

    printf("dev_trimapExpanded %d after function \n", dev_trimapExpanded);
}

in my file kernel.cu i have :

cudaError_t expansionTrimapCuda(unsigned char *dev_trimapExpanded, size_t width, size_t height)
{
    size_t size = width*height;

    cudaError_t cudaStatus;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        return cudaStatus;
    }

    printf("dev_trimapExpanded %d before cudaMalloc \n", dev_trimapExpanded);
    cudaStatus = cudaMalloc((void**)&dev_trimapExpanded, size);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc dev_trimapExpanded failed!");
        return cudaStatus;
    }
    printf("dev_trimapExpanded %d after cudaMalloc \n", dev_trimapExpanded);

    return cudaStatus;
}

The output is:

dev_trimapExpanded 0 before function
dev_trimapExpanded 0 before cudaMalloc
dev_trimapExpanded 93323264 after cudaMalloc
dev_trimapExpanded 0 after function

The address of my pointer is 0 before the cudaMalloc call which is normal but I don’t understand why the address is 0 after the function. The address of the pointer dev_trimapExpanded shouldn’t change. Is the scope of the cudaMalloc only in the function ? The memory is not free neither. How can I keep trace of my global memory between different kernel call ?

  • 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-12T08:54:16+00:00Added an answer on June 12, 2026 at 8:54 am

    The behaviour is absolutely correct, your problem is, that you are passing the pointer “by value” – not “by reference” as you are calling the function. So what happens right now is, as the function is called youre pointer is copied, the value of the copy is changed inside the function, but as the function is finished the old pointer is not affected at all (since all the changes happend only to the copy).

    Try this for you function header instead:

    cudaError_t expansionTrimapCuda(unsigned char *& dev_trimapExpanded, ... )
    

    See also: Pointer to Pointer / Reference to Pointer

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

Sidebar

Related Questions

My question is I think quite simple but I don't think the answer will
The question is quite simple : I have a controller plugin for Zend Framework.
Pretty simple question. I'm quite certain I have the Class, method, codebehind, etc linked
I have a problem that is quite simple to understand, but for me it
I am stuck with something quite simple but really annoying: I have an xml
This should be quite simple, but I can't work out the syntax. I have
I don't really know if the title is correct, but the question is quite
Unfortunately, the answer to this question isn't quite as simple as it sounds... but
I have a quite simple question to ask: I need to put a small
This sounds like a simple question, but I don't know how to search for

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.