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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:54:56+00:00 2026-06-07T14:54:56+00:00

Please look at the below code which does a simple char assignment __global__ void

  • 0

Please look at the below code which does a simple char assignment

__global__ void seehowpointerwork(char* gpuHello, char* finalPoint){

    char* temp;
    bool found = false;
    for(int i = 0 ; i < 11; i++){
        if(gpuHello[i] == ' '){
            temp = &gpuHello[i+1];
            found = true;

            break;
        }
    }
    bool sth = found;
    finalPoint = temp;

}
int main()
{
    // Testing one concept;
    string hello = "Hello World";
    char* gpuHello;
    cudaMalloc((void**)&gpuHello, 11 * sizeof(char));
    cudaMemcpy(gpuHello, hello.c_str(), 11 * sizeof(char), cudaMemcpyHostToDevice);
    char* didItFind;
    char* whatIsIt = (char*)malloc(5 * sizeof(char));
    seehowpointerwork<<<1,1>>>(gpuHello, didItFind);
    cudaMemcpy(whatIsIt,didItFind, 5 * sizeof(char), cudaMemcpyDeviceToHost);
    cout<<"The pointer points to : " << whatIsIt;
    return 0;
}

I really dont understand that when i print whatIsIt, why does it not print “World” as the answer but just prints some random string.

EDIT
Update version after accouting for null characters as pointed out

__global__ void seehowpointerwork(char* gpuHello, char* finalPoint){

    char* temp;
    bool found = false;
    for(int i = 0 ; i < 11; i++){
        if(gpuHello[i] == ' '){
            temp = gpuHello;
            found = true;

            break;
        }
    }
    bool sth = found;
    finalPoint = temp;

}
int main()
{
    // Testing one concept;
    string hello = "Hello World";
    char* gpuHello;
    cudaMalloc((void**)&gpuHello, 12 * sizeof(char));
    cudaMemcpy(gpuHello, hello.c_str(), 12 * sizeof(char), cudaMemcpyHostToDevice);
    char* didItFind;
    char* whatIsIt = (char*)malloc(6 * sizeof(char));
    seehowpointerwork<<<1,1>>>(gpuHello, didItFind);
    cudaMemcpy(whatIsIt,didItFind, 6 * sizeof(char), cudaMemcpyDeviceToHost);
    cout<<"The pointer points to : " << whatIsIt;
    return 0;
}
  • 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-07T14:54:58+00:00Added an answer on June 7, 2026 at 2:54 pm

    You must pass finalPoint by reference, not by value if you want to have the kernel operate the way you have defined it. Perhaps something like this:

    #include <cstdio>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    __global__ void seehowpointerwork(char * gpuHello, char ** finalPoint){
    
        char* temp;
        for(int i = 0 ; i < 11; i++){
            if(gpuHello[i] == ' '){
                temp = &gpuHello[i+1];
                break;
            }
        }
        *finalPoint = temp;
    }
    
    inline void gpuAssert(cudaError_t code, char *file, int line, 
                     bool abort=true)
    {  
       if (code != cudaSuccess) {
          printf("GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
          if (abort) exit(code);
       }
    }
    #define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
    
    int main()
    {
        string hello = "Hello World";
        char* gpuHello;
        gpuErrchk( cudaMalloc((void**)&gpuHello, 11 * sizeof(char)) );
        gpuErrchk( cudaMemcpy(gpuHello, hello.data(), 11 * sizeof(char), cudaMemcpyHostToDevice) );
        char ** didItFinda, * didItFindb;
        gpuErrchk( cudaMalloc((void **)&didItFinda, sizeof(char *)) );
        char* whatIsIt = (char*)malloc(5 * sizeof(char));
        seehowpointerwork<<<1,1>>>(gpuHello, didItFinda);
        gpuErrchk( cudaPeekAtLastError() );
        gpuErrchk( cudaMemcpy(&didItFindb, didItFinda, sizeof(char *), cudaMemcpyDeviceToHost) );
        gpuErrchk( cudaMemcpy(whatIsIt, didItFindb, 5 * sizeof(char), cudaMemcpyDeviceToHost) );
        cout<<"The pointer points to : " << whatIsIt << endl;
        return 0;
    }
    

    When compiled and run, this version produces:

    $ nvcc -arch=sm_12 -Xptxas="-v" programmer.cu 
    ptxas info    : Compiling entry function '_Z17seehowpointerworkPcPS_' for 'sm_12'
    ptxas info    : Used 4 registers, 8+16 bytes smem, 8 bytes cmem[1]
    
    $ ./a.out 
    The pointer points to : World
    

    As it stands, the device to host copy will be failing, because didItFind is not a valid device pointer – you passed it by value to the kernel, so its value on the host cannot be modified by the kernel. The code above contains sufficient error checking to find this sort of problem – you should always check the return status of every API call.

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

Sidebar

Related Questions

please look at the simple php webpage code below how can modify my code
Please look at the below code snippet and let me know how the out
Look please below this codes throw me : FormatException was unhandled by user code
Please have a look at the code below: import string from collections import defaultdict
Please have a look on the code below .. <div id=click_me>Save</div> <div id=blocks_sortable> <div
Could you please have a look at my code below. #!C:\Perl\bin\perl.exe use strict; use
Please look at the following code: char* test ( ) { char word[20]; printf
Please take a look at this below line of code in JSF <h:inputText id=name
Please take a look at the code snippet below. I want the 2nd MongoDB
Please look at my below code. Is there any leaks or issues in my

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.