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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:19:24+00:00 2026-05-25T02:19:24+00:00

Is it not possible to use memset on an array of integers? I tried

  • 0

Is it not possible to use memset on an array of integers? I tried the following memset call and didn’t get the correct integer values in the int array.

int arr[5];
memset (arr, -1, sizeof(arr)/sizeof(int));

Values I got are:

arr[0] = -1
arr[1] = 255
arr[2] = 0
arr[3] = 0
arr[4] = 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-25T02:19:25+00:00Added an answer on May 25, 2026 at 2:19 am

    Just change to memset (arr, -1, sizeof(arr));

    Note that for other values than 0 and -1 this would not work since memset sets the byte values for the block of memory that starts at the variable indicated by *ptr for the following num bytes.

    void * memset ( void * ptr, int value, size_t num );
    

    And since int is represented on more than one byte, you will not get the desired value for the integers in your array.

    Exceptions:

    • 0 is an exception since, if you set all the bytes to 0, the value will be zero
    • -1 is another exception since, as Patrick highlighted -1 is 0xff (=255) in int8_t and 0xffffffff in int32_t

    The reason you got:

    arr[0] = -1
    arr[1] = 255
    arr[2] = 0
    arr[3] = 0
    arr[4] = 0
    

    Is because, in your case, the length of an int is 4 bytes (32 bit representation), the length of your array in bytes being 20 (=5*4), and you only set 5 bytes to -1 (=255) instead of 20.

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

Sidebar

Related Questions

It's not possible to use @Value on a static variable. @Value(${some.value}) static private int
Why is it not possible to use enum values as strings in a switch
I know that most people say, OH! It's not possible, use php or whatever...
Is it possible to use the @Html.EditorForModel() method but not pollute my Domain entities
Is it possible to use reflection to know what the code is doing, not
Is it possible to use MySQL local? I mean NOT at a server. I
Short version : Is it possible or not to use impersonation in ASP.NET to
I just wonder if it is possible to only use CSS but not javascript
Is it possible in C# to use UTF-32 characters not in Plane 0 as
Possible Duplicate: Why does this intentionally incorrect use of strcpy not fail horribly? Below

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.