Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I avoid std::vector<> to initialize all its elements?
    primarykey
    data
    text
    <p><strong>EDIT:</strong> I edited both the question and its title to be more precise.</p> <p>Considering the following source code:</p> <pre><code>#include &lt;vector&gt; struct xyz { xyz() { } // empty constructor, but the compiler doesn't care xyz(const xyz&amp; o): v(o.v) { } xyz&amp; operator=(const xyz&amp; o) { v=o.v; return *this; } int v; // &lt;will be initialized to int(), which means 0 }; std::vector&lt;xyz&gt; test() { return std::vector&lt;xyz&gt;(1024); // will do a memset() :-( } </code></pre> <p>...how can I avoid the memory allocated by the vector&lt;> to be initialized with copies of its first element, which is a O(n) operation I'd rather skip for the sake of speed, since my default constructor does nothing ?</p> <p>A g++ specific solution will do, if no generic one exists (but I couldn't find any attribute to do that).</p> <p><strong>EDIT</strong>: generated code follows (command line: arm-elf-g++-4.5 -O3 -S -fno-verbose-asm -o - test.cpp | arm-elf-c++filt | grep -vE '^[[:space:]]+[.@].*$' )</p> <pre><code>test(): mov r3, #0 stmfd sp!, {r4, lr} mov r4, r0 str r3, [r0, #0] str r3, [r0, #4] str r3, [r0, #8] mov r0, #4096 bl operator new(unsigned long) add r1, r0, #4096 add r2, r0, #4080 str r0, [r4, #0] stmib r4, {r0, r1} add r2, r2, #12 b .L4 @ .L8: @ add r0, r0, #4 @ .L4: @ cmp r0, #0 @ fill the memory movne r3, #0 @ strne r3, [r0, #0] @ cmp r0, r2 @ bne .L8 @ str r1, [r4, #4] mov r0, r4 ldmfd sp!, {r4, pc} </code></pre> <p><strong>EDIT:</strong> For the sake of completeness, here is the assembly for x86_64:</p> <pre><code>.globl test() test(): LFB450: pushq %rbp LCFI0: movq %rsp, %rbp LCFI1: pushq %rbx LCFI2: movq %rdi, %rbx subq $8, %rsp LCFI3: movq $0, (%rdi) movq $0, 8(%rdi) movq $0, 16(%rdi) movl $4096, %edi call operator new(unsigned long) leaq 4096(%rax), %rcx movq %rax, (%rbx) movq %rax, 8(%rbx) leaq 4092(%rax), %rdx movq %rcx, 16(%rbx) jmp L4 @ L8: @ addq $4, %rax @ L4: @ testq %rax, %rax @ memory-filling loop je L2 @ movl $0, (%rax) @ L2: @ cmpq %rdx, %rax @ jne L8 @ movq %rcx, 8(%rbx) movq %rbx, %rax addq $8, %rsp popq %rbx leave LCFI4: ret LFE450: EH_frame1: LSCIE1: LECIE1: LSFDE1: LASFDE1: LEFDE1: </code></pre> <p><strong>EDIT:</strong> I think the conclusion is to not use <code>std::vector&lt;&gt;</code> when you want to avoid unneeded initialization. I ended up unrolling my own templated container, which performs better (and has specialized versions for neon and armv7).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload