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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:06:14+00:00 2026-05-24T05:06:14+00:00

I have a CUDA application I’m working on with an array of Objects; each

  • 0

I have a CUDA application I’m working on with an array of Objects; each object has a pointer to an array of std::pair<int, double>. I’m trying to cudaMemcpy the array of objects over, then cudaMemcpy the array of pairs to each of the objects, however this is giving me all kinds of grief. It crashes attempting to copy to the inner array; I don’t understand how to move this over…

#include <cuda.h>

#include <cuda_runtime.h>

#include <iostream>

using namespace std;

class Object
{
public:
    int id;
    float something;
    std::pair<int, float> *somePairs;
};

Object *objects;

void initObjects()
{
    objects = new Object[10];

    for( int idx = 0; idx < 10; idx++ )
    {
        objects[idx].id = idx;
        objects[idx].something = (float) idx;
    objects[idx].somePairs = new std::pair<int, float>[10];

        for ( int jdx = 10; jdx < 10; jdx++ )
        {
           objects[idx].somePairs[jdx] = std::pair<int, float>( jdx, (float) jdx );
        }

    }
}



void cudaMemcpyObjects()
{
     Object *devObjects;

     cudaMalloc( &devObjects, sizeof(Object) * 10 );
     cudaMemcpy( devObjects, objects, sizeof(Object) * 10, cudaMemcpyHostToDevice );

     for ( int idx = 0; idx < 10; idx++ )
     {
         size_t pairSetSize = sizeof(std::pair<int, float>) * 10;

         // CRASH HERE ... v
         cudaMalloc( &(devObjects[idx].somePairs), pairSetSize );
         cudaMemcpy( devObjects[idx].somePairs, objects[idx].somePairs,
                     sizeof( std::pair<int, float> ) * 10, cudaMemcpyHostToDevice );

     }


}


int main()
{
    initObjects();
    cudaMemcpyObjects();
    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-05-24T05:06:17+00:00Added an answer on May 24, 2026 at 5:06 am

    My CUDA experience is only in its infancy, but I believe the error is like this:

    cudaMalloc is a host function that wants to write the pointer into host memory. However, you are passing to it a pointer in device memory!

    To fix this, you should first create the device pointers and fill them into your host object structure, and only then copy the whole thing over to the device, and also copy the individual pairs over to the device as well.

    Schematically:

    struct Bar;
    
    struct Foo
    {
      int tag;
      Bar * bp;
    };
    
    void setup()
    {
      Foo * hFoo = new Foo[10];
    
      Foo * dFoo;
      cudaMalloc(dFoo, sizeof(Foo) * 10);
    
      for (size_t i = 0; i != 10; ++i)
      {
        Bar * dBar;
        cudaMalloc(&dbar, sizeof(Bar));
    
        Bar b;  // automatic temporary -- we never keep a host copy of this
        cudaMemcpy(dBar, &b, sizeof(Bar));
    
        hFoo[i].bp = dBar;    // this is already a device pointer!
      }
    
      cudaMemcpy(dFoo, hFoo, sizeof(Foo) * 10);
    }
    

    On the return, don’t forget that the Foo::bp are device pointers that you still need to copy back one by one!

    It would probably be easier to just have one self-contained class that you can move in one go, but that may not be practical, or desirable for reasons of memory locality. You have to thing carefully about this. If the member is just a pair, why not put the two items in the main class directly?

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

Sidebar

Related Questions

I have a data object that looks like this: { 'node-16': { 'tags': ['cuda'],
I am working on CUDA and I have a problem related to thread synchronization.
I have a CUDA application that on one computer (with a GTX 275) works
I have CUDA 2.1 installed on my machine and it has a graphic card
Do you have any experience working with CUDA on an enabled GPU that uses
I'm trying to accelerate this database search application with CUDA, and I'm working on
I'm working on translating a CUDA application ( this if you must know )
I have a cuda program like this : for (int i=0;i<100000;i++) { if (i%2
say I have a cuda kernel __global__ foo (int a, int b) { ...
In a CUDA kernel, I have code similar to the following. I am trying

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.