Right way to remove an element in std::setWhat's the best way to trim std::string?What is the easiest way to initialize a std::vector with hardcoded elements?RGBApixel memory issue with EasyBMP c++pointer being freed was not allocated for pointer assignmenthow to delete myself with referencexcode cocos2d arc multiple errors(C++ Error: pointer being freed was not allocated) for linked listsC++ memory allocation error“Pointer being freed not allocated error”Run time memory allocation error
Why would an invisible personal shield be necessary?
Can I attune a Circlet of Human Perfection to my animated skeletons to allow them to blend in and speak?
What would the United Kingdom's "optimal" Brexit deal look like?
Wrapping IMemoryCache with SemaphoreSlim
How to season a character?
Is The Venice Syndrome documentary cover photo real?
Why did House of Representatives need to condemn Trumps Tweets?
Why did Windows 95 crash the whole system but newer Windows only crashed programs?
How did astronauts using rovers tell direction without compasses on the Moon?
Why put copper in between battery contacts and clamps?
Why does Canada require bilingualism in a lot of federal government posts?
My employer is refusing to give me the pay that was advertised after an internal job move
How should I quote American English speakers in a British English essay?
If the Moon were impacted by a suitably sized meteor, how long would it take to impact the Earth?
Is SecureRandom.ints() secure?
Antonym of "Megalomania"
What Marvel character has this 'W' symbol?
A variant of the Multiple Traveling Salesman Problem
What are the cons of stateless password generators?
How does a poisoned arrow combine with the spell Conjure Barrage?
Who said "one can be a powerful king with a very small sceptre"?
Convert graph format for Mathematica graph functions
Is it okay for me to decline a project on ethical grounds?
Alternatives to minimizing loss in regression
Right way to remove an element in std::set
What's the best way to trim std::string?What is the easiest way to initialize a std::vector with hardcoded elements?RGBApixel memory issue with EasyBMP c++pointer being freed was not allocated for pointer assignmenthow to delete myself with referencexcode cocos2d arc multiple errors(C++ Error: pointer being freed was not allocated) for linked listsC++ memory allocation error“Pointer being freed not allocated error”Run time memory allocation error
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a graph class
struct Graph
list<Node *> vertices;
;
int main()
Graph g;
// fill out graph
return 0;
I want to perform a Dijkstra-shortest-path-like algorithm. Step 1 would be creating a set out of all the nodes, which I accomplish by
set<Node *> outstanding;
for (auto itx=g.vertices.begin(); itx!=g.vertices.end(); itx++)
outstanding.insert(*itx);
Step 2 would be to extract the vertex with a certain property
double max_height_comp = (*(g.vertices.begin()))->max_height;
set<Node *>::const_iterator it_max;
while (!outstanding.empty())
{
for (auto its=outstanding.begin(); its!=outstanding.end(); its++)
if ((*its)->max_height >= max_height_comp)
max_height_comp = (*its)->max_height;
it_max = its;
outstanding.erase(it_max);
I'm getting these runtime errors
malloc: *** error for object 0x7fc485c02de0: pointer being freed was not allocated
malloc: *** set a breakpoint in malloc_error_break to debug
I fear that erase()
is calling free()
or delete
on the elements of outstanding
which are pointers. But why would it do that? I just want to delete the value of the pointer from the set, I don't want to delete the data that the pointer is pointing to.
c++ pointers malloc stdset
add a comment |
I have a graph class
struct Graph
list<Node *> vertices;
;
int main()
Graph g;
// fill out graph
return 0;
I want to perform a Dijkstra-shortest-path-like algorithm. Step 1 would be creating a set out of all the nodes, which I accomplish by
set<Node *> outstanding;
for (auto itx=g.vertices.begin(); itx!=g.vertices.end(); itx++)
outstanding.insert(*itx);
Step 2 would be to extract the vertex with a certain property
double max_height_comp = (*(g.vertices.begin()))->max_height;
set<Node *>::const_iterator it_max;
while (!outstanding.empty())
{
for (auto its=outstanding.begin(); its!=outstanding.end(); its++)
if ((*its)->max_height >= max_height_comp)
max_height_comp = (*its)->max_height;
it_max = its;
outstanding.erase(it_max);
I'm getting these runtime errors
malloc: *** error for object 0x7fc485c02de0: pointer being freed was not allocated
malloc: *** set a breakpoint in malloc_error_break to debug
I fear that erase()
is calling free()
or delete
on the elements of outstanding
which are pointers. But why would it do that? I just want to delete the value of the pointer from the set, I don't want to delete the data that the pointer is pointing to.
c++ pointers malloc stdset
2
I suspect you're erasing an iterator to the same node twice. Can you show the code where you initialiseit_max
andmax_height_comp
between erases?
– Peter Bell
Mar 26 at 20:44
@PeterBell Thanks for the comment. I edited the post.
– ToniAz
Mar 26 at 20:48
So let's break this down. All your loop does is erase the maximum item in the set that is greater than or equal to(*(g.vertices.begin()))->max_height;
? Is that correct? If so, this does not require afor
loop. The max item can be obtained usingstd::max_element
and compare againstmax_height
. If so, erase that iterator.
– PaulMcKenzie
Mar 26 at 20:59
add a comment |
I have a graph class
struct Graph
list<Node *> vertices;
;
int main()
Graph g;
// fill out graph
return 0;
I want to perform a Dijkstra-shortest-path-like algorithm. Step 1 would be creating a set out of all the nodes, which I accomplish by
set<Node *> outstanding;
for (auto itx=g.vertices.begin(); itx!=g.vertices.end(); itx++)
outstanding.insert(*itx);
Step 2 would be to extract the vertex with a certain property
double max_height_comp = (*(g.vertices.begin()))->max_height;
set<Node *>::const_iterator it_max;
while (!outstanding.empty())
{
for (auto its=outstanding.begin(); its!=outstanding.end(); its++)
if ((*its)->max_height >= max_height_comp)
max_height_comp = (*its)->max_height;
it_max = its;
outstanding.erase(it_max);
I'm getting these runtime errors
malloc: *** error for object 0x7fc485c02de0: pointer being freed was not allocated
malloc: *** set a breakpoint in malloc_error_break to debug
I fear that erase()
is calling free()
or delete
on the elements of outstanding
which are pointers. But why would it do that? I just want to delete the value of the pointer from the set, I don't want to delete the data that the pointer is pointing to.
c++ pointers malloc stdset
I have a graph class
struct Graph
list<Node *> vertices;
;
int main()
Graph g;
// fill out graph
return 0;
I want to perform a Dijkstra-shortest-path-like algorithm. Step 1 would be creating a set out of all the nodes, which I accomplish by
set<Node *> outstanding;
for (auto itx=g.vertices.begin(); itx!=g.vertices.end(); itx++)
outstanding.insert(*itx);
Step 2 would be to extract the vertex with a certain property
double max_height_comp = (*(g.vertices.begin()))->max_height;
set<Node *>::const_iterator it_max;
while (!outstanding.empty())
{
for (auto its=outstanding.begin(); its!=outstanding.end(); its++)
if ((*its)->max_height >= max_height_comp)
max_height_comp = (*its)->max_height;
it_max = its;
outstanding.erase(it_max);
I'm getting these runtime errors
malloc: *** error for object 0x7fc485c02de0: pointer being freed was not allocated
malloc: *** set a breakpoint in malloc_error_break to debug
I fear that erase()
is calling free()
or delete
on the elements of outstanding
which are pointers. But why would it do that? I just want to delete the value of the pointer from the set, I don't want to delete the data that the pointer is pointing to.
c++ pointers malloc stdset
c++ pointers malloc stdset
edited Mar 26 at 20:46
ToniAz
asked Mar 26 at 20:41
ToniAzToniAz
1273 silver badges14 bronze badges
1273 silver badges14 bronze badges
2
I suspect you're erasing an iterator to the same node twice. Can you show the code where you initialiseit_max
andmax_height_comp
between erases?
– Peter Bell
Mar 26 at 20:44
@PeterBell Thanks for the comment. I edited the post.
– ToniAz
Mar 26 at 20:48
So let's break this down. All your loop does is erase the maximum item in the set that is greater than or equal to(*(g.vertices.begin()))->max_height;
? Is that correct? If so, this does not require afor
loop. The max item can be obtained usingstd::max_element
and compare againstmax_height
. If so, erase that iterator.
– PaulMcKenzie
Mar 26 at 20:59
add a comment |
2
I suspect you're erasing an iterator to the same node twice. Can you show the code where you initialiseit_max
andmax_height_comp
between erases?
– Peter Bell
Mar 26 at 20:44
@PeterBell Thanks for the comment. I edited the post.
– ToniAz
Mar 26 at 20:48
So let's break this down. All your loop does is erase the maximum item in the set that is greater than or equal to(*(g.vertices.begin()))->max_height;
? Is that correct? If so, this does not require afor
loop. The max item can be obtained usingstd::max_element
and compare againstmax_height
. If so, erase that iterator.
– PaulMcKenzie
Mar 26 at 20:59
2
2
I suspect you're erasing an iterator to the same node twice. Can you show the code where you initialise
it_max
and max_height_comp
between erases?– Peter Bell
Mar 26 at 20:44
I suspect you're erasing an iterator to the same node twice. Can you show the code where you initialise
it_max
and max_height_comp
between erases?– Peter Bell
Mar 26 at 20:44
@PeterBell Thanks for the comment. I edited the post.
– ToniAz
Mar 26 at 20:48
@PeterBell Thanks for the comment. I edited the post.
– ToniAz
Mar 26 at 20:48
So let's break this down. All your loop does is erase the maximum item in the set that is greater than or equal to
(*(g.vertices.begin()))->max_height;
? Is that correct? If so, this does not require a for
loop. The max item can be obtained using std::max_element
and compare against max_height
. If so, erase that iterator.– PaulMcKenzie
Mar 26 at 20:59
So let's break this down. All your loop does is erase the maximum item in the set that is greater than or equal to
(*(g.vertices.begin()))->max_height;
? Is that correct? If so, this does not require a for
loop. The max item can be obtained using std::max_element
and compare against max_height
. If so, erase that iterator.– PaulMcKenzie
Mar 26 at 20:59
add a comment |
3 Answers
3
active
oldest
votes
From the code you've shown, I think you aren't resetting it_max
or max_height_comp
between loop iterations. Thus on the second loop trip, everything is less than max_height_comp
and it_max
is never updated.
This problem can be avoided entirely by using a function from <algorithm>
, that way the variables are kept within the correct scope by construction.
while (!outstanding.empty())
auto it_max = std::max_element(outstanding.begin(), outstanding.end(),
[](Node * left, Node * right)
return left->max_height < right->max_height;
);
Node * node_max = *it_max;
outstanding.erase(it_max);
// Use the node
outstanding
is astd::set
... There are better ways to get the max element
– Shawn
Mar 26 at 21:31
How about --it_max = std::prev(outstanding.end());
?
– PaulMcKenzie
Mar 26 at 21:36
@PaulMcKenzie that won't work because the set is ordered by address and not by height. Furthermore, the set couldn't be ordered by height without losing elements that have the same height as each other.
– Peter Bell
Mar 26 at 21:50
add a comment |
You don't appear to be updating max_height_comp
for each iteration. After the first time thru the while
loop, it will keep the largest value from the previous iteration, so that it_max
will not be updated and you'll try to erase that node a second time. You need to reset max_height_comp
at the start of every loop, using the data contained within outstanding
or a number smaller than any possible value you could have.
There's also the possibility that the initial value for max_height_comp
could be larger than any in outstanding
which would result in trying to erase a default constructed iterator.
add a comment |
From the docs here:
std::set::erase
Removes from the set container either a single element or a range of elements ([first,last)).
This effectively reduces the container size by the number of elements removed, which are destroyed.
It appears that your pointer is not getting updated for some reason, and when erase()
is called, it is trying to destroy something that wasn't allocated.
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%2f55365867%2fright-way-to-remove-an-element-in-stdsetnode%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the code you've shown, I think you aren't resetting it_max
or max_height_comp
between loop iterations. Thus on the second loop trip, everything is less than max_height_comp
and it_max
is never updated.
This problem can be avoided entirely by using a function from <algorithm>
, that way the variables are kept within the correct scope by construction.
while (!outstanding.empty())
auto it_max = std::max_element(outstanding.begin(), outstanding.end(),
[](Node * left, Node * right)
return left->max_height < right->max_height;
);
Node * node_max = *it_max;
outstanding.erase(it_max);
// Use the node
outstanding
is astd::set
... There are better ways to get the max element
– Shawn
Mar 26 at 21:31
How about --it_max = std::prev(outstanding.end());
?
– PaulMcKenzie
Mar 26 at 21:36
@PaulMcKenzie that won't work because the set is ordered by address and not by height. Furthermore, the set couldn't be ordered by height without losing elements that have the same height as each other.
– Peter Bell
Mar 26 at 21:50
add a comment |
From the code you've shown, I think you aren't resetting it_max
or max_height_comp
between loop iterations. Thus on the second loop trip, everything is less than max_height_comp
and it_max
is never updated.
This problem can be avoided entirely by using a function from <algorithm>
, that way the variables are kept within the correct scope by construction.
while (!outstanding.empty())
auto it_max = std::max_element(outstanding.begin(), outstanding.end(),
[](Node * left, Node * right)
return left->max_height < right->max_height;
);
Node * node_max = *it_max;
outstanding.erase(it_max);
// Use the node
outstanding
is astd::set
... There are better ways to get the max element
– Shawn
Mar 26 at 21:31
How about --it_max = std::prev(outstanding.end());
?
– PaulMcKenzie
Mar 26 at 21:36
@PaulMcKenzie that won't work because the set is ordered by address and not by height. Furthermore, the set couldn't be ordered by height without losing elements that have the same height as each other.
– Peter Bell
Mar 26 at 21:50
add a comment |
From the code you've shown, I think you aren't resetting it_max
or max_height_comp
between loop iterations. Thus on the second loop trip, everything is less than max_height_comp
and it_max
is never updated.
This problem can be avoided entirely by using a function from <algorithm>
, that way the variables are kept within the correct scope by construction.
while (!outstanding.empty())
auto it_max = std::max_element(outstanding.begin(), outstanding.end(),
[](Node * left, Node * right)
return left->max_height < right->max_height;
);
Node * node_max = *it_max;
outstanding.erase(it_max);
// Use the node
From the code you've shown, I think you aren't resetting it_max
or max_height_comp
between loop iterations. Thus on the second loop trip, everything is less than max_height_comp
and it_max
is never updated.
This problem can be avoided entirely by using a function from <algorithm>
, that way the variables are kept within the correct scope by construction.
while (!outstanding.empty())
auto it_max = std::max_element(outstanding.begin(), outstanding.end(),
[](Node * left, Node * right)
return left->max_height < right->max_height;
);
Node * node_max = *it_max;
outstanding.erase(it_max);
// Use the node
answered Mar 26 at 21:05
Peter BellPeter Bell
3322 silver badges6 bronze badges
3322 silver badges6 bronze badges
outstanding
is astd::set
... There are better ways to get the max element
– Shawn
Mar 26 at 21:31
How about --it_max = std::prev(outstanding.end());
?
– PaulMcKenzie
Mar 26 at 21:36
@PaulMcKenzie that won't work because the set is ordered by address and not by height. Furthermore, the set couldn't be ordered by height without losing elements that have the same height as each other.
– Peter Bell
Mar 26 at 21:50
add a comment |
outstanding
is astd::set
... There are better ways to get the max element
– Shawn
Mar 26 at 21:31
How about --it_max = std::prev(outstanding.end());
?
– PaulMcKenzie
Mar 26 at 21:36
@PaulMcKenzie that won't work because the set is ordered by address and not by height. Furthermore, the set couldn't be ordered by height without losing elements that have the same height as each other.
– Peter Bell
Mar 26 at 21:50
outstanding
is a std::set
... There are better ways to get the max element– Shawn
Mar 26 at 21:31
outstanding
is a std::set
... There are better ways to get the max element– Shawn
Mar 26 at 21:31
How about --
it_max = std::prev(outstanding.end());
?– PaulMcKenzie
Mar 26 at 21:36
How about --
it_max = std::prev(outstanding.end());
?– PaulMcKenzie
Mar 26 at 21:36
@PaulMcKenzie that won't work because the set is ordered by address and not by height. Furthermore, the set couldn't be ordered by height without losing elements that have the same height as each other.
– Peter Bell
Mar 26 at 21:50
@PaulMcKenzie that won't work because the set is ordered by address and not by height. Furthermore, the set couldn't be ordered by height without losing elements that have the same height as each other.
– Peter Bell
Mar 26 at 21:50
add a comment |
You don't appear to be updating max_height_comp
for each iteration. After the first time thru the while
loop, it will keep the largest value from the previous iteration, so that it_max
will not be updated and you'll try to erase that node a second time. You need to reset max_height_comp
at the start of every loop, using the data contained within outstanding
or a number smaller than any possible value you could have.
There's also the possibility that the initial value for max_height_comp
could be larger than any in outstanding
which would result in trying to erase a default constructed iterator.
add a comment |
You don't appear to be updating max_height_comp
for each iteration. After the first time thru the while
loop, it will keep the largest value from the previous iteration, so that it_max
will not be updated and you'll try to erase that node a second time. You need to reset max_height_comp
at the start of every loop, using the data contained within outstanding
or a number smaller than any possible value you could have.
There's also the possibility that the initial value for max_height_comp
could be larger than any in outstanding
which would result in trying to erase a default constructed iterator.
add a comment |
You don't appear to be updating max_height_comp
for each iteration. After the first time thru the while
loop, it will keep the largest value from the previous iteration, so that it_max
will not be updated and you'll try to erase that node a second time. You need to reset max_height_comp
at the start of every loop, using the data contained within outstanding
or a number smaller than any possible value you could have.
There's also the possibility that the initial value for max_height_comp
could be larger than any in outstanding
which would result in trying to erase a default constructed iterator.
You don't appear to be updating max_height_comp
for each iteration. After the first time thru the while
loop, it will keep the largest value from the previous iteration, so that it_max
will not be updated and you'll try to erase that node a second time. You need to reset max_height_comp
at the start of every loop, using the data contained within outstanding
or a number smaller than any possible value you could have.
There's also the possibility that the initial value for max_height_comp
could be larger than any in outstanding
which would result in trying to erase a default constructed iterator.
answered Mar 26 at 21:02
1201ProgramAlarm1201ProgramAlarm
21.1k7 gold badges27 silver badges42 bronze badges
21.1k7 gold badges27 silver badges42 bronze badges
add a comment |
add a comment |
From the docs here:
std::set::erase
Removes from the set container either a single element or a range of elements ([first,last)).
This effectively reduces the container size by the number of elements removed, which are destroyed.
It appears that your pointer is not getting updated for some reason, and when erase()
is called, it is trying to destroy something that wasn't allocated.
add a comment |
From the docs here:
std::set::erase
Removes from the set container either a single element or a range of elements ([first,last)).
This effectively reduces the container size by the number of elements removed, which are destroyed.
It appears that your pointer is not getting updated for some reason, and when erase()
is called, it is trying to destroy something that wasn't allocated.
add a comment |
From the docs here:
std::set::erase
Removes from the set container either a single element or a range of elements ([first,last)).
This effectively reduces the container size by the number of elements removed, which are destroyed.
It appears that your pointer is not getting updated for some reason, and when erase()
is called, it is trying to destroy something that wasn't allocated.
From the docs here:
std::set::erase
Removes from the set container either a single element or a range of elements ([first,last)).
This effectively reduces the container size by the number of elements removed, which are destroyed.
It appears that your pointer is not getting updated for some reason, and when erase()
is called, it is trying to destroy something that wasn't allocated.
answered Mar 26 at 20:51
Matthew Salvatore ViglioneMatthew Salvatore Viglione
6652 silver badges21 bronze badges
6652 silver badges21 bronze badges
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%2f55365867%2fright-way-to-remove-an-element-in-stdsetnode%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
2
I suspect you're erasing an iterator to the same node twice. Can you show the code where you initialise
it_max
andmax_height_comp
between erases?– Peter Bell
Mar 26 at 20:44
@PeterBell Thanks for the comment. I edited the post.
– ToniAz
Mar 26 at 20:48
So let's break this down. All your loop does is erase the maximum item in the set that is greater than or equal to
(*(g.vertices.begin()))->max_height;
? Is that correct? If so, this does not require afor
loop. The max item can be obtained usingstd::max_element
and compare againstmax_height
. If so, erase that iterator.– PaulMcKenzie
Mar 26 at 20:59