Why does Box need 16 bytes in memory, but a referenced slice needs only 8? (on x64 machine) Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Why does Rust borrow checker reject this code?How do I implement a struct that takes a generic vector with lifetime annotations?Clone not invoked for Moved value?Which library to use for Weak referencesClosing stdout or stdinWhat is the use of into_boxed_slice() methods?Reverse order of a reference to immutable array sliceHow to prepend a slice to a VecWhy does storing to and loading from an AVX2 256bit vector have different results in debug and release mode?Why does a function taking a reference to a generic care that the generic is Sized?

Why doesn't the university give past final exams' answers?

Did war bonds have better investment alternatives during WWII?

Raising a bilingual kid. When should we introduce the majority language?

Why isn't everyone flabbergasted about Bran's "gift"?

Does a Draconic Bloodline sorcerer's doubled proficiency bonus for Charisma checks against dragons apply to all dragon types or only the chosen one?

Not within Jobscope - Aggravated injury

How to begin with a paragraph in latex

What helicopter has the most rotor blades?

TV series episode where humans nuke aliens before decrypting their message that states they come in peace

Will I lose my paid in full property

What happened to Viserion in Season 7?

Can gravitational waves pass through a black hole?

How to keep bees out of canned beverages?

What is the evidence that custom checks in Northern Ireland are going to result in violence?

The 'gros' functor from schemes into (strictly) locally ringed topoi

Why did Europeans not widely domesticate foxes?

What's parked in Mil Moscow helicopter plant?

How would it unbalance gameplay to rule that Weapon Master allows for picking a fighting style?

Marquee sign letters

How do I deal with an erroneously large refund?

How did Elite on the NES work?

Processing ADC conversion result: DMA vs Processor Registers

All ASCII characters with a given bit count

Has a Nobel Peace laureate ever been accused of war crimes?



Why does Box need 16 bytes in memory, but a referenced slice needs only 8? (on x64 machine)



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Why does Rust borrow checker reject this code?How do I implement a struct that takes a generic vector with lifetime annotations?Clone not invoked for Moved value?Which library to use for Weak referencesClosing stdout or stdinWhat is the use of into_boxed_slice() methods?Reverse order of a reference to immutable array sliceHow to prepend a slice to a VecWhy does storing to and loading from an AVX2 256bit vector have different results in debug and release mode?Why does a function taking a reference to a generic care that the generic is Sized?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








11















Consider:



fn main() 
// Prints 8, 8, 16
println!(
", , ",
std::mem::size_of::<Box<i8>>(),
std::mem::size_of::<Box<&[i8]>>(),
std::mem::size_of::<Box<[i8]>>(),
);



Why do owned slices take 16 bytes, but referenced slices take only 8?










share|improve this question
























  • Playground: play.rust-lang.org/…

    – French Boiethios
    7 hours ago






  • 2





    Previous discussion: i.stack.imgur.com/Xt6L3.png

    – hellow
    7 hours ago

















11















Consider:



fn main() 
// Prints 8, 8, 16
println!(
", , ",
std::mem::size_of::<Box<i8>>(),
std::mem::size_of::<Box<&[i8]>>(),
std::mem::size_of::<Box<[i8]>>(),
);



Why do owned slices take 16 bytes, but referenced slices take only 8?










share|improve this question
























  • Playground: play.rust-lang.org/…

    – French Boiethios
    7 hours ago






  • 2





    Previous discussion: i.stack.imgur.com/Xt6L3.png

    – hellow
    7 hours ago













11












11








11








Consider:



fn main() 
// Prints 8, 8, 16
println!(
", , ",
std::mem::size_of::<Box<i8>>(),
std::mem::size_of::<Box<&[i8]>>(),
std::mem::size_of::<Box<[i8]>>(),
);



Why do owned slices take 16 bytes, but referenced slices take only 8?










share|improve this question
















Consider:



fn main() 
// Prints 8, 8, 16
println!(
", , ",
std::mem::size_of::<Box<i8>>(),
std::mem::size_of::<Box<&[i8]>>(),
std::mem::size_of::<Box<[i8]>>(),
);



Why do owned slices take 16 bytes, but referenced slices take only 8?







rust






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 46 mins ago









Tim Diekmann

3,57291940




3,57291940










asked 8 hours ago









aminamin

1,29312042




1,29312042












  • Playground: play.rust-lang.org/…

    – French Boiethios
    7 hours ago






  • 2





    Previous discussion: i.stack.imgur.com/Xt6L3.png

    – hellow
    7 hours ago

















  • Playground: play.rust-lang.org/…

    – French Boiethios
    7 hours ago






  • 2





    Previous discussion: i.stack.imgur.com/Xt6L3.png

    – hellow
    7 hours ago
















Playground: play.rust-lang.org/…

– French Boiethios
7 hours ago





Playground: play.rust-lang.org/…

– French Boiethios
7 hours ago




2




2





Previous discussion: i.stack.imgur.com/Xt6L3.png

– hellow
7 hours ago





Previous discussion: i.stack.imgur.com/Xt6L3.png

– hellow
7 hours ago












2 Answers
2






active

oldest

votes


















13














Box<T> is basically *const T (Actually it's a newtype around Unique<T>, which itself is a NonNull<T> with PhantomData<T> (for dropck), but let's stick to *const T for simplicity).



A pointer in Rust normally has the same size as size_of::<usize>() except when T is a dynamically sized type (DST). Currently, a Box<DST> is 2 * size_of::<usize>() in size (the exact representation is not stable at the time of writing). A pointer to a DST is called FatPtr.



Currently, there are two kinds of DSTs: Slices and traits. A FatPtr to a slice is defined like this:



#[repr(C)]
struct FatPtr<T>
data: *const T,
len: usize,



For a trait pointer, len is replaced by a pointer to the vtable.




  • Box<i8>: i8 is a sized type => basically the same as *const i8 => 8 bytes in size (on 64 bit)


  • Box<[i8]>: [i8] is a DST => basically the same as FatPtr<T> => 16 bytes in size (on 64 bit)


  • Box<&[i8]>: &[i8] is not a DST. It's basically the same as *const FatPtr<T> => 8 bytes in size (on 64 bit)





share|improve this answer
































    2














    The size of a reference depends on the "sizeness" of the referenced type:



    • A reference to a sized type is a single pointer to the memory address.


    • A reference to an unsized type is a pointer to the memory and the size of the pointed datum. That's what is called a fat pointer:



      #[repr(C)]
      struct FatPtr<T>
      data: *const T,
      len: usize,



    A Box is a special kind of pointer that points to the heap, but it is a pointer anyway.



    Knowing that, you understand that:




    • Box<i8> is 8 bytes because i8 is sized,


    • Box<&[i8]> is 8 bytes because a reference is sized,


    • Box<[i8]> is 8 bytes because a slice is unsized





    share|improve this answer























    • Box<[i8]> is 16 in your own playground link

      – Stargateur
      56 mins ago











    • @Stargateur I guess this was a typo :)

      – Tim Diekmann
      44 mins ago











    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55814114%2fwhy-does-boxt-need-16-bytes-in-memory-but-a-referenced-slice-needs-only-8%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    13














    Box<T> is basically *const T (Actually it's a newtype around Unique<T>, which itself is a NonNull<T> with PhantomData<T> (for dropck), but let's stick to *const T for simplicity).



    A pointer in Rust normally has the same size as size_of::<usize>() except when T is a dynamically sized type (DST). Currently, a Box<DST> is 2 * size_of::<usize>() in size (the exact representation is not stable at the time of writing). A pointer to a DST is called FatPtr.



    Currently, there are two kinds of DSTs: Slices and traits. A FatPtr to a slice is defined like this:



    #[repr(C)]
    struct FatPtr<T>
    data: *const T,
    len: usize,



    For a trait pointer, len is replaced by a pointer to the vtable.




    • Box<i8>: i8 is a sized type => basically the same as *const i8 => 8 bytes in size (on 64 bit)


    • Box<[i8]>: [i8] is a DST => basically the same as FatPtr<T> => 16 bytes in size (on 64 bit)


    • Box<&[i8]>: &[i8] is not a DST. It's basically the same as *const FatPtr<T> => 8 bytes in size (on 64 bit)





    share|improve this answer





























      13














      Box<T> is basically *const T (Actually it's a newtype around Unique<T>, which itself is a NonNull<T> with PhantomData<T> (for dropck), but let's stick to *const T for simplicity).



      A pointer in Rust normally has the same size as size_of::<usize>() except when T is a dynamically sized type (DST). Currently, a Box<DST> is 2 * size_of::<usize>() in size (the exact representation is not stable at the time of writing). A pointer to a DST is called FatPtr.



      Currently, there are two kinds of DSTs: Slices and traits. A FatPtr to a slice is defined like this:



      #[repr(C)]
      struct FatPtr<T>
      data: *const T,
      len: usize,



      For a trait pointer, len is replaced by a pointer to the vtable.




      • Box<i8>: i8 is a sized type => basically the same as *const i8 => 8 bytes in size (on 64 bit)


      • Box<[i8]>: [i8] is a DST => basically the same as FatPtr<T> => 16 bytes in size (on 64 bit)


      • Box<&[i8]>: &[i8] is not a DST. It's basically the same as *const FatPtr<T> => 8 bytes in size (on 64 bit)





      share|improve this answer



























        13












        13








        13







        Box<T> is basically *const T (Actually it's a newtype around Unique<T>, which itself is a NonNull<T> with PhantomData<T> (for dropck), but let's stick to *const T for simplicity).



        A pointer in Rust normally has the same size as size_of::<usize>() except when T is a dynamically sized type (DST). Currently, a Box<DST> is 2 * size_of::<usize>() in size (the exact representation is not stable at the time of writing). A pointer to a DST is called FatPtr.



        Currently, there are two kinds of DSTs: Slices and traits. A FatPtr to a slice is defined like this:



        #[repr(C)]
        struct FatPtr<T>
        data: *const T,
        len: usize,



        For a trait pointer, len is replaced by a pointer to the vtable.




        • Box<i8>: i8 is a sized type => basically the same as *const i8 => 8 bytes in size (on 64 bit)


        • Box<[i8]>: [i8] is a DST => basically the same as FatPtr<T> => 16 bytes in size (on 64 bit)


        • Box<&[i8]>: &[i8] is not a DST. It's basically the same as *const FatPtr<T> => 8 bytes in size (on 64 bit)





        share|improve this answer















        Box<T> is basically *const T (Actually it's a newtype around Unique<T>, which itself is a NonNull<T> with PhantomData<T> (for dropck), but let's stick to *const T for simplicity).



        A pointer in Rust normally has the same size as size_of::<usize>() except when T is a dynamically sized type (DST). Currently, a Box<DST> is 2 * size_of::<usize>() in size (the exact representation is not stable at the time of writing). A pointer to a DST is called FatPtr.



        Currently, there are two kinds of DSTs: Slices and traits. A FatPtr to a slice is defined like this:



        #[repr(C)]
        struct FatPtr<T>
        data: *const T,
        len: usize,



        For a trait pointer, len is replaced by a pointer to the vtable.




        • Box<i8>: i8 is a sized type => basically the same as *const i8 => 8 bytes in size (on 64 bit)


        • Box<[i8]>: [i8] is a DST => basically the same as FatPtr<T> => 16 bytes in size (on 64 bit)


        • Box<&[i8]>: &[i8] is not a DST. It's basically the same as *const FatPtr<T> => 8 bytes in size (on 64 bit)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 47 mins ago









        Peter Mortensen

        14k1987114




        14k1987114










        answered 7 hours ago









        Tim DiekmannTim Diekmann

        3,57291940




        3,57291940























            2














            The size of a reference depends on the "sizeness" of the referenced type:



            • A reference to a sized type is a single pointer to the memory address.


            • A reference to an unsized type is a pointer to the memory and the size of the pointed datum. That's what is called a fat pointer:



              #[repr(C)]
              struct FatPtr<T>
              data: *const T,
              len: usize,



            A Box is a special kind of pointer that points to the heap, but it is a pointer anyway.



            Knowing that, you understand that:




            • Box<i8> is 8 bytes because i8 is sized,


            • Box<&[i8]> is 8 bytes because a reference is sized,


            • Box<[i8]> is 8 bytes because a slice is unsized





            share|improve this answer























            • Box<[i8]> is 16 in your own playground link

              – Stargateur
              56 mins ago











            • @Stargateur I guess this was a typo :)

              – Tim Diekmann
              44 mins ago















            2














            The size of a reference depends on the "sizeness" of the referenced type:



            • A reference to a sized type is a single pointer to the memory address.


            • A reference to an unsized type is a pointer to the memory and the size of the pointed datum. That's what is called a fat pointer:



              #[repr(C)]
              struct FatPtr<T>
              data: *const T,
              len: usize,



            A Box is a special kind of pointer that points to the heap, but it is a pointer anyway.



            Knowing that, you understand that:




            • Box<i8> is 8 bytes because i8 is sized,


            • Box<&[i8]> is 8 bytes because a reference is sized,


            • Box<[i8]> is 8 bytes because a slice is unsized





            share|improve this answer























            • Box<[i8]> is 16 in your own playground link

              – Stargateur
              56 mins ago











            • @Stargateur I guess this was a typo :)

              – Tim Diekmann
              44 mins ago













            2












            2








            2







            The size of a reference depends on the "sizeness" of the referenced type:



            • A reference to a sized type is a single pointer to the memory address.


            • A reference to an unsized type is a pointer to the memory and the size of the pointed datum. That's what is called a fat pointer:



              #[repr(C)]
              struct FatPtr<T>
              data: *const T,
              len: usize,



            A Box is a special kind of pointer that points to the heap, but it is a pointer anyway.



            Knowing that, you understand that:




            • Box<i8> is 8 bytes because i8 is sized,


            • Box<&[i8]> is 8 bytes because a reference is sized,


            • Box<[i8]> is 8 bytes because a slice is unsized





            share|improve this answer













            The size of a reference depends on the "sizeness" of the referenced type:



            • A reference to a sized type is a single pointer to the memory address.


            • A reference to an unsized type is a pointer to the memory and the size of the pointed datum. That's what is called a fat pointer:



              #[repr(C)]
              struct FatPtr<T>
              data: *const T,
              len: usize,



            A Box is a special kind of pointer that points to the heap, but it is a pointer anyway.



            Knowing that, you understand that:




            • Box<i8> is 8 bytes because i8 is sized,


            • Box<&[i8]> is 8 bytes because a reference is sized,


            • Box<[i8]> is 8 bytes because a slice is unsized






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 7 hours ago









            French BoiethiosFrench Boiethios

            11.4k44081




            11.4k44081












            • Box<[i8]> is 16 in your own playground link

              – Stargateur
              56 mins ago











            • @Stargateur I guess this was a typo :)

              – Tim Diekmann
              44 mins ago

















            • Box<[i8]> is 16 in your own playground link

              – Stargateur
              56 mins ago











            • @Stargateur I guess this was a typo :)

              – Tim Diekmann
              44 mins ago
















            Box<[i8]> is 16 in your own playground link

            – Stargateur
            56 mins ago





            Box<[i8]> is 16 in your own playground link

            – Stargateur
            56 mins ago













            @Stargateur I guess this was a typo :)

            – Tim Diekmann
            44 mins ago





            @Stargateur I guess this was a typo :)

            – Tim Diekmann
            44 mins ago

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55814114%2fwhy-does-boxt-need-16-bytes-in-memory-but-a-referenced-slice-needs-only-8%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Ружовы пелікан Змест Знешні выгляд | Пашырэнне | Асаблівасці біялогіі | Літаратура | НавігацыяДагледжаная версіяправерана1 зменаДагледжаная версіяправерана1 змена/ 22697590 Сістэматыкана ВіківідахВыявына Вікісховішчы174693363011049382

            ValueError: Error when checking input: expected conv2d_13_input to have shape (3, 150, 150) but got array with shape (150, 150, 3)2019 Community Moderator ElectionError when checking : expected dense_1_input to have shape (None, 5) but got array with shape (200, 1)Error 'Expected 2D array, got 1D array instead:'ValueError: Error when checking input: expected lstm_41_input to have 3 dimensions, but got array with shape (40000,100)ValueError: Error when checking target: expected dense_1 to have shape (7,) but got array with shape (1,)ValueError: Error when checking target: expected dense_2 to have shape (1,) but got array with shape (0,)Keras exception: ValueError: Error when checking input: expected conv2d_1_input to have shape (150, 150, 3) but got array with shape (256, 256, 3)Steps taking too long to completewhen checking input: expected dense_1_input to have shape (13328,) but got array with shape (317,)ValueError: Error when checking target: expected dense_3 to have shape (None, 1) but got array with shape (7715, 40000)Keras exception: Error when checking input: expected dense_input to have shape (2,) but got array with shape (1,)

            Illegal assignment from SObject to ContactFetching String, Id from Map - Illegal Assignment Id to Field / ObjectError: Compile Error: Illegal assignment from String to BooleanError: List has no rows for assignment to SObjectError on Test Class - System.QueryException: List has no rows for assignment to SObjectRemote action problemDML requires SObject or SObject list type error“Illegal assignment from List to List”Test Class Fail: Batch Class: System.QueryException: List has no rows for assignment to SObjectMapping to a user'List has no rows for assignment to SObject' Mystery