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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:42:55+00:00 2026-06-02T15:42:55+00:00

Jumping straight to code, this is what I would like to do: size_t len

  • 0

Jumping straight to code, this is what I would like to do:

size_t len = obj->someLengthFunctionThatReturnsTypeSizeT();
array<int>^ a = gcnew array<int>(len);

When I try this, I get the error

conversion from size_t to int, possible loss of data

Is there a way I can get this code to compile without explicitly casting to int? I find it odd that I can’t initialize an array to this size, especially because there is a LongLength property (and how could you get a length as a long – bigger than int – if you can only initialize a length as an int?).

Thanks!

P.S.: I did find this article that says that it may be impractical to allocate an array that is truly size_t, but I don’t think that is a concern. The point is that the length I would like to initialize to is stored in a size_t variable.

  • 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-02T15:42:58+00:00Added an answer on June 2, 2026 at 3:42 pm

    Managed arrays are implemented for using Int32 as indices, there is no way around that. You cannot allocate arrays larger than Int32.MaxValue.

    You could use the static method Array::CreateInstance (the overload that takes a Type and an array of Int64), and then cast the resulting System::Array to the appropriate actual array type (e.g. array<int>^). Note that the passed values must not be larger than Int32.MaxValue. And you would still need to cast.

    So you have at least two options. Either casting:

    // Would truncate the value if it is too large
    array<int>^ a = gcnew array<int>((int)len);
    

    or this (no need to cast len, but the result of CreateInstance):

    // Throws an ArgumentOutOfRangeException if len is too large
    array<int>^ a = (array<int>^)Array::CreateInstance(int::typeid, len);  
    

    Personally, i find the first better. You still might want to check the actual size of len so that you don’t run into any of the mentioned errors.

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

Sidebar

Related Questions

I would really like to stop the menu items jumping around on this site
How do you I stop my links like this: <a href=# onClick=submitComment()> From jumping
It seems like I would be jumping through hoops to move data from MySQL
My debugger keep jumping into debugging the IL instead of jumping to debug code
I know that this is jumping the gun slightly as it is early days
C language allows jumping inside loop. What would be the use of doing so?
More and more I realized that without jumping into the code details, I cannot
I am writing some jQuery code that involves jumping from several functions. In one
I'm trying to make a SeekBar move more smoothly than just jumping straight to
I'm having some trouble prevent mod_deflate from jumping in on this scenario: user running

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.