c++ float* array as reference to std::vector












4















I want to create a std::vector<float> vpd which will be a reference to float*.



float * old = new float[6];
for (int i = 0; i < 6; i++)
{
old[i] = i;
}
vector<float> vpd(6);
auto refasd = &*vpd.begin();
*refasd = *old;
vpd[0] = 23;
cout << old[0] << endl;


How should I modify the code, if I want get 23 from cout?










share|improve this question

























  • Are you maybe looking for this: stackoverflow.com/questions/2434196/…

    – uceumern
    Nov 21 '18 at 10:52











  • Is it about telling the vector to use your "preallocated" buffer instead of the one a vector manages internally, or is it just that you want to refer to to the vector's internal buffer in terms of a float*-type?

    – Stephan Lechner
    Nov 21 '18 at 10:52













  • maybe you meant vpd[0] = 23; *old = *refasd; ?

    – M.M
    Nov 21 '18 at 10:52
















4















I want to create a std::vector<float> vpd which will be a reference to float*.



float * old = new float[6];
for (int i = 0; i < 6; i++)
{
old[i] = i;
}
vector<float> vpd(6);
auto refasd = &*vpd.begin();
*refasd = *old;
vpd[0] = 23;
cout << old[0] << endl;


How should I modify the code, if I want get 23 from cout?










share|improve this question

























  • Are you maybe looking for this: stackoverflow.com/questions/2434196/…

    – uceumern
    Nov 21 '18 at 10:52











  • Is it about telling the vector to use your "preallocated" buffer instead of the one a vector manages internally, or is it just that you want to refer to to the vector's internal buffer in terms of a float*-type?

    – Stephan Lechner
    Nov 21 '18 at 10:52













  • maybe you meant vpd[0] = 23; *old = *refasd; ?

    – M.M
    Nov 21 '18 at 10:52














4












4








4


1






I want to create a std::vector<float> vpd which will be a reference to float*.



float * old = new float[6];
for (int i = 0; i < 6; i++)
{
old[i] = i;
}
vector<float> vpd(6);
auto refasd = &*vpd.begin();
*refasd = *old;
vpd[0] = 23;
cout << old[0] << endl;


How should I modify the code, if I want get 23 from cout?










share|improve this question
















I want to create a std::vector<float> vpd which will be a reference to float*.



float * old = new float[6];
for (int i = 0; i < 6; i++)
{
old[i] = i;
}
vector<float> vpd(6);
auto refasd = &*vpd.begin();
*refasd = *old;
vpd[0] = 23;
cout << old[0] << endl;


How should I modify the code, if I want get 23 from cout?







c++ arrays reference stdvector






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 10:50









DeiDei

6,20153355




6,20153355










asked Nov 21 '18 at 10:39









Artur DomasikArtur Domasik

212




212













  • Are you maybe looking for this: stackoverflow.com/questions/2434196/…

    – uceumern
    Nov 21 '18 at 10:52











  • Is it about telling the vector to use your "preallocated" buffer instead of the one a vector manages internally, or is it just that you want to refer to to the vector's internal buffer in terms of a float*-type?

    – Stephan Lechner
    Nov 21 '18 at 10:52













  • maybe you meant vpd[0] = 23; *old = *refasd; ?

    – M.M
    Nov 21 '18 at 10:52



















  • Are you maybe looking for this: stackoverflow.com/questions/2434196/…

    – uceumern
    Nov 21 '18 at 10:52











  • Is it about telling the vector to use your "preallocated" buffer instead of the one a vector manages internally, or is it just that you want to refer to to the vector's internal buffer in terms of a float*-type?

    – Stephan Lechner
    Nov 21 '18 at 10:52













  • maybe you meant vpd[0] = 23; *old = *refasd; ?

    – M.M
    Nov 21 '18 at 10:52

















Are you maybe looking for this: stackoverflow.com/questions/2434196/…

– uceumern
Nov 21 '18 at 10:52





Are you maybe looking for this: stackoverflow.com/questions/2434196/…

– uceumern
Nov 21 '18 at 10:52













Is it about telling the vector to use your "preallocated" buffer instead of the one a vector manages internally, or is it just that you want to refer to to the vector's internal buffer in terms of a float*-type?

– Stephan Lechner
Nov 21 '18 at 10:52







Is it about telling the vector to use your "preallocated" buffer instead of the one a vector manages internally, or is it just that you want to refer to to the vector's internal buffer in terms of a float*-type?

– Stephan Lechner
Nov 21 '18 at 10:52















maybe you meant vpd[0] = 23; *old = *refasd; ?

– M.M
Nov 21 '18 at 10:52





maybe you meant vpd[0] = 23; *old = *refasd; ?

– M.M
Nov 21 '18 at 10:52












5 Answers
5






active

oldest

votes


















5














You can't. std::vector is not designed to take ownership of a raw pointer.

Maybe you can make do with std::unique_ptr<float>, but the better solution is to directly use std::vector.






share|improve this answer



















  • 1





    And just as I was about to hit submit..

    – StoryTeller
    Nov 21 '18 at 10:46



















5














As alternative, you might use std::span (C++20)



float* old = new float[6];
std::iota(old, old + 6, 0);
std::span<float> vpd(old, 6);
vpd[0] = 23;
std::cout << old[0] << std::endl;
delete old;





share|improve this answer





















  • 2





    Plus one from the future. Does span own the memory, i.e. is the delete intentionally missing?

    – Bathsheba
    Nov 21 '18 at 11:11











  • Span does not own the memory if I'm not mistaken, so yeah, a delete is missing.

    – rubenvb
    Nov 21 '18 at 11:18



















4














You could also create a vector of std::reference_wrapper objects that refer to the original float array - may that be std::vector<float> or a newed float*. An example:



vector<std::reference_wrapper<float>> vpd(old, old + 6); // ¹

vpd[0].get() = 23.f;

cout << old[0] << endl; // prints 23


¹) Thanks to @StoryTeller for pointing out that vpd can be directly initialized.






share|improve this answer





















  • 1





    I'm not sure you need std::transform. IIRC std::vector<std::reference_wrapper<float>> vpd(old, old + 6); should work out of the box.

    – StoryTeller
    Nov 21 '18 at 10:55











  • @StoryTeller True, that's a nice improvement!

    – lubgr
    Nov 21 '18 at 10:58



















2














As std::vector has its own memory structure you cannot map vector<float> or even vector<float*> to an array of float. However you can map each vector item to an array one.



float* old = new float[6];
for (int i = 0; i < 6; i++)
{
old[i] = i;
}
vector<float*> vpd(6);
int i = 0;
for (auto it = vpd.begin(); it != vpd.end(); it++)
{
*it = &old[i++];
}
*vpd[0] = 23;
*vpd[2] = 45;
cout << old[0] << endl << old[2] << endl;


Output



23
45





share|improve this answer


























  • You ruined your cache locality by doing this, though

    – Lightness Races in Orbit
    Nov 21 '18 at 11:17











  • @LightnessRacesinOrbit cold you please explain in more details?

    – serge
    Nov 21 '18 at 11:58











  • gameprogrammingpatterns.com/data-locality.html

    – Lightness Races in Orbit
    Nov 21 '18 at 12:47











  • @LightnessRacesinOrbit the link is cool in design context however I still don't see the relation with reusing of some memory piece accessed by pointers.

    – serge
    Nov 21 '18 at 13:25






  • 1





    Now you're talking about the cost of creating those elements, not the cost of accessing them. But the answer is still yes.

    – Lightness Races in Orbit
    Nov 21 '18 at 13:52



















0














Depending on version of C++ you'll have some options (if I understand the problem):
if you have an older c++ version then c++11 I would in this case declear std::vector as:



// then you really need to make sure u delete the memory as well
std::vector<float*> vpd(6);


If however you got c++11 or higher I would use either std::share_ptr or std::unique_ptr depending on if you would like to share the memory space or not. Either this will ensure the memory is deleted by it self without having to do a "delete float*;" which is nice.
You can read about std::shared_ptr at: https://en.cppreference.com/w/cpp/memory/shared_ptr and std::unique_ptr at: https://en.cppreference.com/w/cpp/memory/unique_ptr



// For unique_ptr
std::vector<std::unique_ptr<float>> vpd(6);
// for std::shared_ptr
std::vector<std::shared_ptr<float>> vpd(6);


I would say that if you can then use unique_ptr rather then shared_ptr due to shared_ptr has som extra komplexitity within to be make sure the memory not used before deleteing memory space.






share|improve this answer























    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%2f53410262%2fc-float-array-as-reference-to-stdvectorfloat%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    You can't. std::vector is not designed to take ownership of a raw pointer.

    Maybe you can make do with std::unique_ptr<float>, but the better solution is to directly use std::vector.






    share|improve this answer



















    • 1





      And just as I was about to hit submit..

      – StoryTeller
      Nov 21 '18 at 10:46
















    5














    You can't. std::vector is not designed to take ownership of a raw pointer.

    Maybe you can make do with std::unique_ptr<float>, but the better solution is to directly use std::vector.






    share|improve this answer



















    • 1





      And just as I was about to hit submit..

      – StoryTeller
      Nov 21 '18 at 10:46














    5












    5








    5







    You can't. std::vector is not designed to take ownership of a raw pointer.

    Maybe you can make do with std::unique_ptr<float>, but the better solution is to directly use std::vector.






    share|improve this answer













    You can't. std::vector is not designed to take ownership of a raw pointer.

    Maybe you can make do with std::unique_ptr<float>, but the better solution is to directly use std::vector.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 10:45









    QuentinQuentin

    46.4k589146




    46.4k589146








    • 1





      And just as I was about to hit submit..

      – StoryTeller
      Nov 21 '18 at 10:46














    • 1





      And just as I was about to hit submit..

      – StoryTeller
      Nov 21 '18 at 10:46








    1




    1





    And just as I was about to hit submit..

    – StoryTeller
    Nov 21 '18 at 10:46





    And just as I was about to hit submit..

    – StoryTeller
    Nov 21 '18 at 10:46













    5














    As alternative, you might use std::span (C++20)



    float* old = new float[6];
    std::iota(old, old + 6, 0);
    std::span<float> vpd(old, 6);
    vpd[0] = 23;
    std::cout << old[0] << std::endl;
    delete old;





    share|improve this answer





















    • 2





      Plus one from the future. Does span own the memory, i.e. is the delete intentionally missing?

      – Bathsheba
      Nov 21 '18 at 11:11











    • Span does not own the memory if I'm not mistaken, so yeah, a delete is missing.

      – rubenvb
      Nov 21 '18 at 11:18
















    5














    As alternative, you might use std::span (C++20)



    float* old = new float[6];
    std::iota(old, old + 6, 0);
    std::span<float> vpd(old, 6);
    vpd[0] = 23;
    std::cout << old[0] << std::endl;
    delete old;





    share|improve this answer





















    • 2





      Plus one from the future. Does span own the memory, i.e. is the delete intentionally missing?

      – Bathsheba
      Nov 21 '18 at 11:11











    • Span does not own the memory if I'm not mistaken, so yeah, a delete is missing.

      – rubenvb
      Nov 21 '18 at 11:18














    5












    5








    5







    As alternative, you might use std::span (C++20)



    float* old = new float[6];
    std::iota(old, old + 6, 0);
    std::span<float> vpd(old, 6);
    vpd[0] = 23;
    std::cout << old[0] << std::endl;
    delete old;





    share|improve this answer















    As alternative, you might use std::span (C++20)



    float* old = new float[6];
    std::iota(old, old + 6, 0);
    std::span<float> vpd(old, 6);
    vpd[0] = 23;
    std::cout << old[0] << std::endl;
    delete old;






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 11:18









    Bathsheba

    180k27254383




    180k27254383










    answered Nov 21 '18 at 11:08









    Jarod42Jarod42

    118k12103189




    118k12103189








    • 2





      Plus one from the future. Does span own the memory, i.e. is the delete intentionally missing?

      – Bathsheba
      Nov 21 '18 at 11:11











    • Span does not own the memory if I'm not mistaken, so yeah, a delete is missing.

      – rubenvb
      Nov 21 '18 at 11:18














    • 2





      Plus one from the future. Does span own the memory, i.e. is the delete intentionally missing?

      – Bathsheba
      Nov 21 '18 at 11:11











    • Span does not own the memory if I'm not mistaken, so yeah, a delete is missing.

      – rubenvb
      Nov 21 '18 at 11:18








    2




    2





    Plus one from the future. Does span own the memory, i.e. is the delete intentionally missing?

    – Bathsheba
    Nov 21 '18 at 11:11





    Plus one from the future. Does span own the memory, i.e. is the delete intentionally missing?

    – Bathsheba
    Nov 21 '18 at 11:11













    Span does not own the memory if I'm not mistaken, so yeah, a delete is missing.

    – rubenvb
    Nov 21 '18 at 11:18





    Span does not own the memory if I'm not mistaken, so yeah, a delete is missing.

    – rubenvb
    Nov 21 '18 at 11:18











    4














    You could also create a vector of std::reference_wrapper objects that refer to the original float array - may that be std::vector<float> or a newed float*. An example:



    vector<std::reference_wrapper<float>> vpd(old, old + 6); // ¹

    vpd[0].get() = 23.f;

    cout << old[0] << endl; // prints 23


    ¹) Thanks to @StoryTeller for pointing out that vpd can be directly initialized.






    share|improve this answer





















    • 1





      I'm not sure you need std::transform. IIRC std::vector<std::reference_wrapper<float>> vpd(old, old + 6); should work out of the box.

      – StoryTeller
      Nov 21 '18 at 10:55











    • @StoryTeller True, that's a nice improvement!

      – lubgr
      Nov 21 '18 at 10:58
















    4














    You could also create a vector of std::reference_wrapper objects that refer to the original float array - may that be std::vector<float> or a newed float*. An example:



    vector<std::reference_wrapper<float>> vpd(old, old + 6); // ¹

    vpd[0].get() = 23.f;

    cout << old[0] << endl; // prints 23


    ¹) Thanks to @StoryTeller for pointing out that vpd can be directly initialized.






    share|improve this answer





















    • 1





      I'm not sure you need std::transform. IIRC std::vector<std::reference_wrapper<float>> vpd(old, old + 6); should work out of the box.

      – StoryTeller
      Nov 21 '18 at 10:55











    • @StoryTeller True, that's a nice improvement!

      – lubgr
      Nov 21 '18 at 10:58














    4












    4








    4







    You could also create a vector of std::reference_wrapper objects that refer to the original float array - may that be std::vector<float> or a newed float*. An example:



    vector<std::reference_wrapper<float>> vpd(old, old + 6); // ¹

    vpd[0].get() = 23.f;

    cout << old[0] << endl; // prints 23


    ¹) Thanks to @StoryTeller for pointing out that vpd can be directly initialized.






    share|improve this answer















    You could also create a vector of std::reference_wrapper objects that refer to the original float array - may that be std::vector<float> or a newed float*. An example:



    vector<std::reference_wrapper<float>> vpd(old, old + 6); // ¹

    vpd[0].get() = 23.f;

    cout << old[0] << endl; // prints 23


    ¹) Thanks to @StoryTeller for pointing out that vpd can be directly initialized.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 11:00

























    answered Nov 21 '18 at 10:49









    lubgrlubgr

    13.2k31850




    13.2k31850








    • 1





      I'm not sure you need std::transform. IIRC std::vector<std::reference_wrapper<float>> vpd(old, old + 6); should work out of the box.

      – StoryTeller
      Nov 21 '18 at 10:55











    • @StoryTeller True, that's a nice improvement!

      – lubgr
      Nov 21 '18 at 10:58














    • 1





      I'm not sure you need std::transform. IIRC std::vector<std::reference_wrapper<float>> vpd(old, old + 6); should work out of the box.

      – StoryTeller
      Nov 21 '18 at 10:55











    • @StoryTeller True, that's a nice improvement!

      – lubgr
      Nov 21 '18 at 10:58








    1




    1





    I'm not sure you need std::transform. IIRC std::vector<std::reference_wrapper<float>> vpd(old, old + 6); should work out of the box.

    – StoryTeller
    Nov 21 '18 at 10:55





    I'm not sure you need std::transform. IIRC std::vector<std::reference_wrapper<float>> vpd(old, old + 6); should work out of the box.

    – StoryTeller
    Nov 21 '18 at 10:55













    @StoryTeller True, that's a nice improvement!

    – lubgr
    Nov 21 '18 at 10:58





    @StoryTeller True, that's a nice improvement!

    – lubgr
    Nov 21 '18 at 10:58











    2














    As std::vector has its own memory structure you cannot map vector<float> or even vector<float*> to an array of float. However you can map each vector item to an array one.



    float* old = new float[6];
    for (int i = 0; i < 6; i++)
    {
    old[i] = i;
    }
    vector<float*> vpd(6);
    int i = 0;
    for (auto it = vpd.begin(); it != vpd.end(); it++)
    {
    *it = &old[i++];
    }
    *vpd[0] = 23;
    *vpd[2] = 45;
    cout << old[0] << endl << old[2] << endl;


    Output



    23
    45





    share|improve this answer


























    • You ruined your cache locality by doing this, though

      – Lightness Races in Orbit
      Nov 21 '18 at 11:17











    • @LightnessRacesinOrbit cold you please explain in more details?

      – serge
      Nov 21 '18 at 11:58











    • gameprogrammingpatterns.com/data-locality.html

      – Lightness Races in Orbit
      Nov 21 '18 at 12:47











    • @LightnessRacesinOrbit the link is cool in design context however I still don't see the relation with reusing of some memory piece accessed by pointers.

      – serge
      Nov 21 '18 at 13:25






    • 1





      Now you're talking about the cost of creating those elements, not the cost of accessing them. But the answer is still yes.

      – Lightness Races in Orbit
      Nov 21 '18 at 13:52
















    2














    As std::vector has its own memory structure you cannot map vector<float> or even vector<float*> to an array of float. However you can map each vector item to an array one.



    float* old = new float[6];
    for (int i = 0; i < 6; i++)
    {
    old[i] = i;
    }
    vector<float*> vpd(6);
    int i = 0;
    for (auto it = vpd.begin(); it != vpd.end(); it++)
    {
    *it = &old[i++];
    }
    *vpd[0] = 23;
    *vpd[2] = 45;
    cout << old[0] << endl << old[2] << endl;


    Output



    23
    45





    share|improve this answer


























    • You ruined your cache locality by doing this, though

      – Lightness Races in Orbit
      Nov 21 '18 at 11:17











    • @LightnessRacesinOrbit cold you please explain in more details?

      – serge
      Nov 21 '18 at 11:58











    • gameprogrammingpatterns.com/data-locality.html

      – Lightness Races in Orbit
      Nov 21 '18 at 12:47











    • @LightnessRacesinOrbit the link is cool in design context however I still don't see the relation with reusing of some memory piece accessed by pointers.

      – serge
      Nov 21 '18 at 13:25






    • 1





      Now you're talking about the cost of creating those elements, not the cost of accessing them. But the answer is still yes.

      – Lightness Races in Orbit
      Nov 21 '18 at 13:52














    2












    2








    2







    As std::vector has its own memory structure you cannot map vector<float> or even vector<float*> to an array of float. However you can map each vector item to an array one.



    float* old = new float[6];
    for (int i = 0; i < 6; i++)
    {
    old[i] = i;
    }
    vector<float*> vpd(6);
    int i = 0;
    for (auto it = vpd.begin(); it != vpd.end(); it++)
    {
    *it = &old[i++];
    }
    *vpd[0] = 23;
    *vpd[2] = 45;
    cout << old[0] << endl << old[2] << endl;


    Output



    23
    45





    share|improve this answer















    As std::vector has its own memory structure you cannot map vector<float> or even vector<float*> to an array of float. However you can map each vector item to an array one.



    float* old = new float[6];
    for (int i = 0; i < 6; i++)
    {
    old[i] = i;
    }
    vector<float*> vpd(6);
    int i = 0;
    for (auto it = vpd.begin(); it != vpd.end(); it++)
    {
    *it = &old[i++];
    }
    *vpd[0] = 23;
    *vpd[2] = 45;
    cout << old[0] << endl << old[2] << endl;


    Output



    23
    45






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 11:56

























    answered Nov 21 '18 at 11:00









    sergeserge

    70148




    70148













    • You ruined your cache locality by doing this, though

      – Lightness Races in Orbit
      Nov 21 '18 at 11:17











    • @LightnessRacesinOrbit cold you please explain in more details?

      – serge
      Nov 21 '18 at 11:58











    • gameprogrammingpatterns.com/data-locality.html

      – Lightness Races in Orbit
      Nov 21 '18 at 12:47











    • @LightnessRacesinOrbit the link is cool in design context however I still don't see the relation with reusing of some memory piece accessed by pointers.

      – serge
      Nov 21 '18 at 13:25






    • 1





      Now you're talking about the cost of creating those elements, not the cost of accessing them. But the answer is still yes.

      – Lightness Races in Orbit
      Nov 21 '18 at 13:52



















    • You ruined your cache locality by doing this, though

      – Lightness Races in Orbit
      Nov 21 '18 at 11:17











    • @LightnessRacesinOrbit cold you please explain in more details?

      – serge
      Nov 21 '18 at 11:58











    • gameprogrammingpatterns.com/data-locality.html

      – Lightness Races in Orbit
      Nov 21 '18 at 12:47











    • @LightnessRacesinOrbit the link is cool in design context however I still don't see the relation with reusing of some memory piece accessed by pointers.

      – serge
      Nov 21 '18 at 13:25






    • 1





      Now you're talking about the cost of creating those elements, not the cost of accessing them. But the answer is still yes.

      – Lightness Races in Orbit
      Nov 21 '18 at 13:52

















    You ruined your cache locality by doing this, though

    – Lightness Races in Orbit
    Nov 21 '18 at 11:17





    You ruined your cache locality by doing this, though

    – Lightness Races in Orbit
    Nov 21 '18 at 11:17













    @LightnessRacesinOrbit cold you please explain in more details?

    – serge
    Nov 21 '18 at 11:58





    @LightnessRacesinOrbit cold you please explain in more details?

    – serge
    Nov 21 '18 at 11:58













    gameprogrammingpatterns.com/data-locality.html

    – Lightness Races in Orbit
    Nov 21 '18 at 12:47





    gameprogrammingpatterns.com/data-locality.html

    – Lightness Races in Orbit
    Nov 21 '18 at 12:47













    @LightnessRacesinOrbit the link is cool in design context however I still don't see the relation with reusing of some memory piece accessed by pointers.

    – serge
    Nov 21 '18 at 13:25





    @LightnessRacesinOrbit the link is cool in design context however I still don't see the relation with reusing of some memory piece accessed by pointers.

    – serge
    Nov 21 '18 at 13:25




    1




    1





    Now you're talking about the cost of creating those elements, not the cost of accessing them. But the answer is still yes.

    – Lightness Races in Orbit
    Nov 21 '18 at 13:52





    Now you're talking about the cost of creating those elements, not the cost of accessing them. But the answer is still yes.

    – Lightness Races in Orbit
    Nov 21 '18 at 13:52











    0














    Depending on version of C++ you'll have some options (if I understand the problem):
    if you have an older c++ version then c++11 I would in this case declear std::vector as:



    // then you really need to make sure u delete the memory as well
    std::vector<float*> vpd(6);


    If however you got c++11 or higher I would use either std::share_ptr or std::unique_ptr depending on if you would like to share the memory space or not. Either this will ensure the memory is deleted by it self without having to do a "delete float*;" which is nice.
    You can read about std::shared_ptr at: https://en.cppreference.com/w/cpp/memory/shared_ptr and std::unique_ptr at: https://en.cppreference.com/w/cpp/memory/unique_ptr



    // For unique_ptr
    std::vector<std::unique_ptr<float>> vpd(6);
    // for std::shared_ptr
    std::vector<std::shared_ptr<float>> vpd(6);


    I would say that if you can then use unique_ptr rather then shared_ptr due to shared_ptr has som extra komplexitity within to be make sure the memory not used before deleteing memory space.






    share|improve this answer




























      0














      Depending on version of C++ you'll have some options (if I understand the problem):
      if you have an older c++ version then c++11 I would in this case declear std::vector as:



      // then you really need to make sure u delete the memory as well
      std::vector<float*> vpd(6);


      If however you got c++11 or higher I would use either std::share_ptr or std::unique_ptr depending on if you would like to share the memory space or not. Either this will ensure the memory is deleted by it self without having to do a "delete float*;" which is nice.
      You can read about std::shared_ptr at: https://en.cppreference.com/w/cpp/memory/shared_ptr and std::unique_ptr at: https://en.cppreference.com/w/cpp/memory/unique_ptr



      // For unique_ptr
      std::vector<std::unique_ptr<float>> vpd(6);
      // for std::shared_ptr
      std::vector<std::shared_ptr<float>> vpd(6);


      I would say that if you can then use unique_ptr rather then shared_ptr due to shared_ptr has som extra komplexitity within to be make sure the memory not used before deleteing memory space.






      share|improve this answer


























        0












        0








        0







        Depending on version of C++ you'll have some options (if I understand the problem):
        if you have an older c++ version then c++11 I would in this case declear std::vector as:



        // then you really need to make sure u delete the memory as well
        std::vector<float*> vpd(6);


        If however you got c++11 or higher I would use either std::share_ptr or std::unique_ptr depending on if you would like to share the memory space or not. Either this will ensure the memory is deleted by it self without having to do a "delete float*;" which is nice.
        You can read about std::shared_ptr at: https://en.cppreference.com/w/cpp/memory/shared_ptr and std::unique_ptr at: https://en.cppreference.com/w/cpp/memory/unique_ptr



        // For unique_ptr
        std::vector<std::unique_ptr<float>> vpd(6);
        // for std::shared_ptr
        std::vector<std::shared_ptr<float>> vpd(6);


        I would say that if you can then use unique_ptr rather then shared_ptr due to shared_ptr has som extra komplexitity within to be make sure the memory not used before deleteing memory space.






        share|improve this answer













        Depending on version of C++ you'll have some options (if I understand the problem):
        if you have an older c++ version then c++11 I would in this case declear std::vector as:



        // then you really need to make sure u delete the memory as well
        std::vector<float*> vpd(6);


        If however you got c++11 or higher I would use either std::share_ptr or std::unique_ptr depending on if you would like to share the memory space or not. Either this will ensure the memory is deleted by it self without having to do a "delete float*;" which is nice.
        You can read about std::shared_ptr at: https://en.cppreference.com/w/cpp/memory/shared_ptr and std::unique_ptr at: https://en.cppreference.com/w/cpp/memory/unique_ptr



        // For unique_ptr
        std::vector<std::unique_ptr<float>> vpd(6);
        // for std::shared_ptr
        std::vector<std::shared_ptr<float>> vpd(6);


        I would say that if you can then use unique_ptr rather then shared_ptr due to shared_ptr has som extra komplexitity within to be make sure the memory not used before deleteing memory space.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 10:58









        MikaelMikael

        111




        111






























            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%2f53410262%2fc-float-array-as-reference-to-stdvectorfloat%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

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?