How to store a pointer to an array in an array? [closed]
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
closed as unclear what you're asking by πάντα ῥεῖ, Killzone Kid, Peter Ruderman, Matthieu Brucher, Makyen Nov 19 '18 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.
add a comment |
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
closed as unclear what you're asking by πάντα ῥεῖ, Killzone Kid, Peter Ruderman, Matthieu Brucher, Makyen Nov 19 '18 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. Usestd::vector
if there is no particular reason not to, especially as beginner.
– user10605163
Nov 19 '18 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 '18 at 18:19
1) Stop using manual memory management. 2) Usestd::array
orstd::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 '18 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 '18 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 '18 at 20:56
add a comment |
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
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
c++ arrays class pointers
edited Nov 19 '18 at 18:12
shot22
asked Nov 19 '18 at 18:05
shot22shot22
12
12
closed as unclear what you're asking by πάντα ῥεῖ, Killzone Kid, Peter Ruderman, Matthieu Brucher, Makyen Nov 19 '18 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 '18 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. Usestd::vector
if there is no particular reason not to, especially as beginner.
– user10605163
Nov 19 '18 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 '18 at 18:19
1) Stop using manual memory management. 2) Usestd::array
orstd::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 '18 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 '18 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 '18 at 20:56
add a comment |
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. Usestd::vector
if there is no particular reason not to, especially as beginner.
– user10605163
Nov 19 '18 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 '18 at 18:19
1) Stop using manual memory management. 2) Usestd::array
orstd::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 '18 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 '18 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 '18 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.– user10605163
Nov 19 '18 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.– user10605163
Nov 19 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 20:56
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 19 '18 at 18:35
eerorikaeerorika
77.1k556118
77.1k556118
add a comment |
add a comment |
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. Usestd::vector
if there is no particular reason not to, especially as beginner.– user10605163
Nov 19 '18 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 '18 at 18:19
1) Stop using manual memory management. 2) Use
std::array
orstd::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 '18 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 '18 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 '18 at 20:56