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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:06:00+00:00 2026-05-28T03:06:00+00:00

How much information is copied/shared when I assign one array variable to another array

  • 0

How much information is copied/shared when I assign one array variable to another array variable?

int[] a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
int[] b = a;
a[0] = 42;
writefln("%s %s", a[0], b[0]);   // 42 42

Apparently, a and b share the same payload, because 42 is printed twice.

a ~= 10;
writefln("%s %s", a.length, b.length);   // 11 10

Appending to a does not change b, so the length does not seem to part of the payload?

b = a;
a ~= 11;
b ~= 42;
writefln("%s %s", a[11], b[11]);   // 11 42

Could a conforming D implementation also print 42 42? Could b ~= 42 overwrite the 11 inside a?

When exactly are a and b detached from each other? Is D performing some COW in the background?

  • 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-28T03:06:00+00:00Added an answer on May 28, 2026 at 3:06 am

    “Arrays” in D don’t really exist.

    Slices do.

    Slices are just a pointer and a length. So when you assign them to each other, the pointer and the length get copied. If you modify the target data, then it’ll be visible in all instances of the slices — but if you enlarge one slice, the other one will still be using its old length.

    You normally can’t “shrink” the actual length of the array in memory (although you can certainly reduce the slice’s length, so it ‘sees’ less data), so that doesn’t cause issues.

    Hope that explains what’s going on.

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

Sidebar

Related Questions

I have an app that has too much information in one spot so I
I can't find much information around the web about it. Has anyone used both
I can't find much information on const_cast . The only info I could find
I'd like to list as much information about all the threads currently running in
been searching a lot information about IoC/DI development, but I haven't found much information
I'd be much obliged for information regarding how can I get the System Identification
I need to log information about how much RAM the user has. My first
There's lots of information about how cached execution plans in SQL Server eliminate much
I haven't found much on thin packs, and the man pages' information is rather
I've begun playing with MBeans for exposing some information about an app. Pretty much

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.