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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:21:32+00:00 2026-06-16T05:21:32+00:00

I have the following code: import std.stdio; int main(string[] args) { int[3] my_array =

  • 0

I have the following code:

import std.stdio;

int main(string[] args)
{
   int[3] my_array = [1, 2];
   return 0;
}

This compiles fine, and then aborts when executed, giving this error:

arrays_init
object.Exception@src/rt/arraycat.d(31): lengths don't match for array copy
----------------
arrays_init(_Dmain+0x64) [0x416bbc]
arrays_init(extern (C) int rt.dmain2.main(int, char**).void runMain()+0x1c) [0x418c5c]
arrays_init(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate())+0x2a) [0x4185d6]
arrays_init(extern (C) int rt.dmain2.main(int, char**).void runAll()+0x3b) [0x418ca3]
arrays_init(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate())+0x2a) [0x4185d6]
arrays_init(main+0xd1) [0x418561]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed) [0x7f60bc41d30d]

This runs fine if the array literal has 3 items, so apparently the array literal has to match the size of the static array. But shouldn’t this give a compile error since the size of both can be calculated at compile time?

  • 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-16T05:21:33+00:00Added an answer on June 16, 2026 at 5:21 am

    Array literals are of type T[], i.e. they are dynamic arrays that are unaware of their size at compile time.

    Your code compiles for the same reason this compiles:

    void foo(int[] xs)
    {
        int[3] ys = xs;
        ...
    }
    

    The compiler simply doesn’t know how big xs is.

    In your case, the compiler could know at compile time because all the information is there, but it would be going above and beyond what the compiler is obligated to do. Interpreting the code strictly, there is no type mismatch, so it compiles.

    Another side effect of array literals being dynamic arrays is that the code you have will actually allocate memory. It allocates the dynamic array on the heap, copies into the static array, and then you have to wait for a garbage collection cycle before the memory is reclaimed. This can be a cause of bad performance if you initialise arrays like that in a tight loop.

    Again, the compiler could avoid the allocation, but DMD at least does not in the current version (2.060).

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

Sidebar

Related Questions

I have the following code in D import std.stdio; class Thing { // Fields
I have the following code: import string import random d =[random.choice(string.uppercase) for x in
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
I have the following code: import string def translate_non_alphanumerics(to_translate, translate_to='_'): not_letters_or_digits = u'!#%\'()*+,-./:;<=>?@[\]^_`{|}~' translate_table
I have following Java code: import java.io.*; class Global{ public static int a =
I have the following code: #import <Foundation/Foundation.h> #import ServerRequest.h // works even though this
I can't figure this out. I have the following code: import java.util.ArrayList; import java.util.List;
I have the following code: import operator def stagger(l, w): if len(l)>=w: return [tuple(l[0:w])]+stagger(l[1:],
I have the following code in a header included in main.mm: 1. virtual int
I have the following working code: import datetime d = 2012-12-19 f = map(int,

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.