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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:23:46+00:00 2026-06-05T23:23:46+00:00

I am writing host code for a CUDA program, so I am stuck using

  • 0

I am writing host code for a CUDA program, so I am stuck using standard C functions. I am having a problem with initializing the elements of an integer array using the memset function. I was under the impression you could use memset to initialize an integer array to, for example, all 4s like this:

int num_elements = 10;
int* array_example = (int*)malloc(num_elements * sizeof(int));
memset(array_example, 4, sizeof(array_example));

But when I do this, it sets each byte, not each int, to 4. If I say:

memset(array_example, 4, 1);

I get a 4 in the first integer and if I say:

memset(array_example, 4, 2);

I get 1024 in the first integer and 0 in the second. I understand the memset function sets the number of bytes specified in the third parameter to 4, but is there any way to use memset to set each integer to 4 instead of each byte? Otherwise, am I stuck using a for loop? My GPU has a low compute capability so I don’t have access to some of the nicer CUDA additions that allow more C++ usage.

  • 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-05T23:23:48+00:00Added an answer on June 5, 2026 at 11:23 pm

    If you know the size of a table and want to set each element to the certain value you can always write:

    int array[10] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
    

    If you use gcc you can also do this in this way:

    int array[10] = {[0 ... 9] = 4};
    

    When you have to dynamically allocate the array I doubt that there is any alternative solution then just using a simple loop.

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

Sidebar

Related Questions

I'm writing some plugin code in a dll that is called by a host
Writing a python program, and I came up with this error while using the
I am writing some code for a linux program I am working on that
I am writing a simple database application. The program should get database information (host,name,user,pass)
I am writing a standalone code for Sending mail from java. In this program
I'm writing a PowerShell script that will execute commands on a remote host using
I am writing c++ code for a telnet client. I am having problems getting
I was writing a code which requires a large 'int' array to be allocated
I'm writing an app which plays host to a series of plug-ins. Those plug-ins
Writing Classic ASP code in JScript has a lot going for it: more humane

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.