Skip to content

How do i allocate array on free store using new? #740

Closed Answered by hsutter
jituderaps asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks, good question!

You could always do this:

//  Example 1 - works, but does extra init+copy work
main: () = {
    pi: std::unique_ptr<std::array<int,5>>;
    pi = new<std::array<int, 5>>();
    pi* = (1, 2, 3, 4, 5);
    for pi* do (i) std::cout << i;
}

Or this:

//  Example 2 - also works, but does extra init+copy work
main: () = {
    pi: std::unique_ptr<std::array<int,5>>;
    pi = new<std::array<int, 5>>( :std::array = (1, 2, 3, 4, 5) );
    for pi* do (i) std::cout << i;
}

But both of those involve default-initializing all the array elements, and then deep-copying them all.

I've now enabled this to work as expected (commit e6a1cc3)-- I thought this would already have worked bec…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@JohelEGP
Comment options

Answer selected by hsutter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants