How to store a pointer to an array in an array? [closed]











up vote
-4
down vote

favorite












in my program I have a class with the following constructor:



pp = new Particle*[maxN]


and inside one function I have:



// create the new particle and fill with data
pp[n] = new Particle;
pp[n]->charge = charge;
pp[n]->px = px;
pp[n]->py = py;
pp[n]->pz = pz;

// store the new particle pointer in the array
//...


The problem is that I don't know how to write the code for the last instruction, i.e.



// store the new particle pointer in the array


Can you help me?










share|improve this question















closed as unclear what you're asking by πάντα ῥεῖ, Killzone Kid, Peter Ruderman, Matthieu Brucher, Makyen Nov 19 at 19:29


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 2




    Your code has already created the new Particle and saved the pointer to it inside the array (pp[n] = new Particle;), it doesn't make sense to ask how to store it there afterwards. Also you should not use pointers, arrays and dynamic memory allocation (new) for this. Use std::vector if there is no particular reason not to, especially as beginner.
    – eukaryota
    Nov 19 at 18:17












  • pp[n] = new Particle; <- here, you are already doing this. Also the title of your question and what you're asking differ
    – Killzone Kid
    Nov 19 at 18:19












  • 1) Stop using manual memory management. 2) Use std::array or std::vector. 3) Please post a Minimal, Complete, and Verifiable example. Also, smart pointers are a (very useful/good) thing. Please don't write C++98 code in 2018.
    – Jesper Juhl
    Nov 19 at 18:53












  • The person who wrote the comments expected you to create the object first, Particle* p = new Particle; p->charge = ..., and then store that pointer, pp[n] = p;. Since you deviated from the expectations, you got stuck.
    – molbdnilo
    Nov 19 at 19:54










  • eukaryota, Jesper Juhl this code is part of an exercise that asks to use those specific things. Thanks anyway for the advice :) @molbdnilo thanks, this is the answer I was looking for
    – shot22
    Nov 19 at 20:56

















up vote
-4
down vote

favorite












in my program I have a class with the following constructor:



pp = new Particle*[maxN]


and inside one function I have:



// create the new particle and fill with data
pp[n] = new Particle;
pp[n]->charge = charge;
pp[n]->px = px;
pp[n]->py = py;
pp[n]->pz = pz;

// store the new particle pointer in the array
//...


The problem is that I don't know how to write the code for the last instruction, i.e.



// store the new particle pointer in the array


Can you help me?










share|improve this question















closed as unclear what you're asking by πάντα ῥεῖ, Killzone Kid, Peter Ruderman, Matthieu Brucher, Makyen Nov 19 at 19:29


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 2




    Your code has already created the new Particle and saved the pointer to it inside the array (pp[n] = new Particle;), it doesn't make sense to ask how to store it there afterwards. Also you should not use pointers, arrays and dynamic memory allocation (new) for this. Use std::vector if there is no particular reason not to, especially as beginner.
    – eukaryota
    Nov 19 at 18:17












  • pp[n] = new Particle; <- here, you are already doing this. Also the title of your question and what you're asking differ
    – Killzone Kid
    Nov 19 at 18:19












  • 1) Stop using manual memory management. 2) Use std::array or std::vector. 3) Please post a Minimal, Complete, and Verifiable example. Also, smart pointers are a (very useful/good) thing. Please don't write C++98 code in 2018.
    – Jesper Juhl
    Nov 19 at 18:53












  • The person who wrote the comments expected you to create the object first, Particle* p = new Particle; p->charge = ..., and then store that pointer, pp[n] = p;. Since you deviated from the expectations, you got stuck.
    – molbdnilo
    Nov 19 at 19:54










  • eukaryota, Jesper Juhl this code is part of an exercise that asks to use those specific things. Thanks anyway for the advice :) @molbdnilo thanks, this is the answer I was looking for
    – shot22
    Nov 19 at 20:56















up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











in my program I have a class with the following constructor:



pp = new Particle*[maxN]


and inside one function I have:



// create the new particle and fill with data
pp[n] = new Particle;
pp[n]->charge = charge;
pp[n]->px = px;
pp[n]->py = py;
pp[n]->pz = pz;

// store the new particle pointer in the array
//...


The problem is that I don't know how to write the code for the last instruction, i.e.



// store the new particle pointer in the array


Can you help me?










share|improve this question















in my program I have a class with the following constructor:



pp = new Particle*[maxN]


and inside one function I have:



// create the new particle and fill with data
pp[n] = new Particle;
pp[n]->charge = charge;
pp[n]->px = px;
pp[n]->py = py;
pp[n]->pz = pz;

// store the new particle pointer in the array
//...


The problem is that I don't know how to write the code for the last instruction, i.e.



// store the new particle pointer in the array


Can you help me?







c++ arrays class pointers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 18:12

























asked Nov 19 at 18:05









shot22

12




12




closed as unclear what you're asking by πάντα ῥεῖ, Killzone Kid, Peter Ruderman, Matthieu Brucher, Makyen Nov 19 at 19:29


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by πάντα ῥεῖ, Killzone Kid, Peter Ruderman, Matthieu Brucher, Makyen Nov 19 at 19:29


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    Your code has already created the new Particle and saved the pointer to it inside the array (pp[n] = new Particle;), it doesn't make sense to ask how to store it there afterwards. Also you should not use pointers, arrays and dynamic memory allocation (new) for this. Use std::vector if there is no particular reason not to, especially as beginner.
    – eukaryota
    Nov 19 at 18:17












  • pp[n] = new Particle; <- here, you are already doing this. Also the title of your question and what you're asking differ
    – Killzone Kid
    Nov 19 at 18:19












  • 1) Stop using manual memory management. 2) Use std::array or std::vector. 3) Please post a Minimal, Complete, and Verifiable example. Also, smart pointers are a (very useful/good) thing. Please don't write C++98 code in 2018.
    – Jesper Juhl
    Nov 19 at 18:53












  • The person who wrote the comments expected you to create the object first, Particle* p = new Particle; p->charge = ..., and then store that pointer, pp[n] = p;. Since you deviated from the expectations, you got stuck.
    – molbdnilo
    Nov 19 at 19:54










  • eukaryota, Jesper Juhl this code is part of an exercise that asks to use those specific things. Thanks anyway for the advice :) @molbdnilo thanks, this is the answer I was looking for
    – shot22
    Nov 19 at 20:56
















  • 2




    Your code has already created the new Particle and saved the pointer to it inside the array (pp[n] = new Particle;), it doesn't make sense to ask how to store it there afterwards. Also you should not use pointers, arrays and dynamic memory allocation (new) for this. Use std::vector if there is no particular reason not to, especially as beginner.
    – eukaryota
    Nov 19 at 18:17












  • pp[n] = new Particle; <- here, you are already doing this. Also the title of your question and what you're asking differ
    – Killzone Kid
    Nov 19 at 18:19












  • 1) Stop using manual memory management. 2) Use std::array or std::vector. 3) Please post a Minimal, Complete, and Verifiable example. Also, smart pointers are a (very useful/good) thing. Please don't write C++98 code in 2018.
    – Jesper Juhl
    Nov 19 at 18:53












  • The person who wrote the comments expected you to create the object first, Particle* p = new Particle; p->charge = ..., and then store that pointer, pp[n] = p;. Since you deviated from the expectations, you got stuck.
    – molbdnilo
    Nov 19 at 19:54










  • eukaryota, Jesper Juhl this code is part of an exercise that asks to use those specific things. Thanks anyway for the advice :) @molbdnilo thanks, this is the answer I was looking for
    – shot22
    Nov 19 at 20:56










2




2




Your code has already created the new Particle and saved the pointer to it inside the array (pp[n] = new Particle;), it doesn't make sense to ask how to store it there afterwards. Also you should not use pointers, arrays and dynamic memory allocation (new) for this. Use std::vector if there is no particular reason not to, especially as beginner.
– eukaryota
Nov 19 at 18:17






Your code has already created the new Particle and saved the pointer to it inside the array (pp[n] = new Particle;), it doesn't make sense to ask how to store it there afterwards. Also you should not use pointers, arrays and dynamic memory allocation (new) for this. Use std::vector if there is no particular reason not to, especially as beginner.
– eukaryota
Nov 19 at 18:17














pp[n] = new Particle; <- here, you are already doing this. Also the title of your question and what you're asking differ
– Killzone Kid
Nov 19 at 18:19






pp[n] = new Particle; <- here, you are already doing this. Also the title of your question and what you're asking differ
– Killzone Kid
Nov 19 at 18:19














1) Stop using manual memory management. 2) Use std::array or std::vector. 3) Please post a Minimal, Complete, and Verifiable example. Also, smart pointers are a (very useful/good) thing. Please don't write C++98 code in 2018.
– Jesper Juhl
Nov 19 at 18:53






1) Stop using manual memory management. 2) Use std::array or std::vector. 3) Please post a Minimal, Complete, and Verifiable example. Also, smart pointers are a (very useful/good) thing. Please don't write C++98 code in 2018.
– Jesper Juhl
Nov 19 at 18:53














The person who wrote the comments expected you to create the object first, Particle* p = new Particle; p->charge = ..., and then store that pointer, pp[n] = p;. Since you deviated from the expectations, you got stuck.
– molbdnilo
Nov 19 at 19:54




The person who wrote the comments expected you to create the object first, Particle* p = new Particle; p->charge = ..., and then store that pointer, pp[n] = p;. Since you deviated from the expectations, you got stuck.
– molbdnilo
Nov 19 at 19:54












eukaryota, Jesper Juhl this code is part of an exercise that asks to use those specific things. Thanks anyway for the advice :) @molbdnilo thanks, this is the answer I was looking for
– shot22
Nov 19 at 20:56






eukaryota, Jesper Juhl this code is part of an exercise that asks to use those specific things. Thanks anyway for the advice :) @molbdnilo thanks, this is the answer I was looking for
– shot22
Nov 19 at 20:56














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted











The problem is that I don't know how to write the code for the last instruction, i.e.



// store the new particle pointer in the array



You've already done that on the first line:




pp[n] = new Particle;



new Particle creates a Particle object with dynamic storage, and result of the expression is a pointer to the object. pp[n] = ... assigns the pointer at index n of the array pointed by pp.



PS. It is a bad design to have bare owning pointers within a class like this. To avoid memory leaks and undefined behaviour, it is recommended to use containers instead.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted











    The problem is that I don't know how to write the code for the last instruction, i.e.



    // store the new particle pointer in the array



    You've already done that on the first line:




    pp[n] = new Particle;



    new Particle creates a Particle object with dynamic storage, and result of the expression is a pointer to the object. pp[n] = ... assigns the pointer at index n of the array pointed by pp.



    PS. It is a bad design to have bare owning pointers within a class like this. To avoid memory leaks and undefined behaviour, it is recommended to use containers instead.






    share|improve this answer

























      up vote
      1
      down vote



      accepted











      The problem is that I don't know how to write the code for the last instruction, i.e.



      // store the new particle pointer in the array



      You've already done that on the first line:




      pp[n] = new Particle;



      new Particle creates a Particle object with dynamic storage, and result of the expression is a pointer to the object. pp[n] = ... assigns the pointer at index n of the array pointed by pp.



      PS. It is a bad design to have bare owning pointers within a class like this. To avoid memory leaks and undefined behaviour, it is recommended to use containers instead.






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted







        The problem is that I don't know how to write the code for the last instruction, i.e.



        // store the new particle pointer in the array



        You've already done that on the first line:




        pp[n] = new Particle;



        new Particle creates a Particle object with dynamic storage, and result of the expression is a pointer to the object. pp[n] = ... assigns the pointer at index n of the array pointed by pp.



        PS. It is a bad design to have bare owning pointers within a class like this. To avoid memory leaks and undefined behaviour, it is recommended to use containers instead.






        share|improve this answer













        The problem is that I don't know how to write the code for the last instruction, i.e.



        // store the new particle pointer in the array



        You've already done that on the first line:




        pp[n] = new Particle;



        new Particle creates a Particle object with dynamic storage, and result of the expression is a pointer to the object. pp[n] = ... assigns the pointer at index n of the array pointed by pp.



        PS. It is a bad design to have bare owning pointers within a class like this. To avoid memory leaks and undefined behaviour, it is recommended to use containers instead.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 18:35









        eerorika

        74.8k555116




        74.8k555116















            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'