c++ float* array as reference to std::vector
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
add a comment |
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
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 afloat*
-type?
– Stephan Lechner
Nov 21 '18 at 10:52
maybe you meantvpd[0] = 23; *old = *refasd;
?
– M.M
Nov 21 '18 at 10:52
add a comment |
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
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
c++ arrays reference stdvector
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 afloat*
-type?
– Stephan Lechner
Nov 21 '18 at 10:52
maybe you meantvpd[0] = 23; *old = *refasd;
?
– M.M
Nov 21 '18 at 10:52
add a comment |
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 afloat*
-type?
– Stephan Lechner
Nov 21 '18 at 10:52
maybe you meantvpd[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
add a comment |
5 Answers
5
active
oldest
votes
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
.
1
And just as I was about to hit submit..
– StoryTeller
Nov 21 '18 at 10:46
add a comment |
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;
2
Plus one from the future. Doesspan
own the memory, i.e. is thedelete
intentionally missing?
– Bathsheba
Nov 21 '18 at 11:11
Span does not own the memory if I'm not mistaken, so yeah, adelete
is missing.
– rubenvb
Nov 21 '18 at 11:18
add a comment |
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 new
ed 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.
1
I'm not sure you needstd::transform
. IIRCstd::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
add a comment |
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
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
|
show 10 more comments
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
.
1
And just as I was about to hit submit..
– StoryTeller
Nov 21 '18 at 10:46
add a comment |
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
.
1
And just as I was about to hit submit..
– StoryTeller
Nov 21 '18 at 10:46
add a comment |
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
.
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
.
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
add a comment |
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
add a comment |
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;
2
Plus one from the future. Doesspan
own the memory, i.e. is thedelete
intentionally missing?
– Bathsheba
Nov 21 '18 at 11:11
Span does not own the memory if I'm not mistaken, so yeah, adelete
is missing.
– rubenvb
Nov 21 '18 at 11:18
add a comment |
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;
2
Plus one from the future. Doesspan
own the memory, i.e. is thedelete
intentionally missing?
– Bathsheba
Nov 21 '18 at 11:11
Span does not own the memory if I'm not mistaken, so yeah, adelete
is missing.
– rubenvb
Nov 21 '18 at 11:18
add a comment |
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;
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;
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. Doesspan
own the memory, i.e. is thedelete
intentionally missing?
– Bathsheba
Nov 21 '18 at 11:11
Span does not own the memory if I'm not mistaken, so yeah, adelete
is missing.
– rubenvb
Nov 21 '18 at 11:18
add a comment |
2
Plus one from the future. Doesspan
own the memory, i.e. is thedelete
intentionally missing?
– Bathsheba
Nov 21 '18 at 11:11
Span does not own the memory if I'm not mistaken, so yeah, adelete
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
add a comment |
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 new
ed 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.
1
I'm not sure you needstd::transform
. IIRCstd::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
add a comment |
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 new
ed 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.
1
I'm not sure you needstd::transform
. IIRCstd::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
add a comment |
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 new
ed 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.
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 new
ed 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.
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 needstd::transform
. IIRCstd::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
add a comment |
1
I'm not sure you needstd::transform
. IIRCstd::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
add a comment |
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
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
|
show 10 more comments
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
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
|
show 10 more comments
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
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
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
|
show 10 more comments
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
|
show 10 more comments
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 '18 at 10:58
MikaelMikael
111
111
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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