Should I explicitly delete structs in C++? [duplicate]2019 Community Moderator ElectionCalling delete on variable allocated on the stackWhen to use “delete”?C++ and when to use deleteDo I have to delete struct pointer manually in C++?When should you use a class vs a struct in C++?What are the differences between a pointer variable and a reference variable in C++?What are the differences between struct and class in C++?When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?The Definitive C++ Book Guide and ListWhen to use struct?Difference between 'struct' and 'typedef struct' in C++?What is the “-->” operator in C++?typedef struct vs struct definitionsWhen exactly is it leak safe to use (anonymous) inner classes?
How are passwords stolen from companies if they only store hashes?
 
 Do I need to be arrogant to get ahead?
 
 World War I as a war of liberals against authoritarians?
 
 What can I do if I am asked to learn different programming languages very frequently?
 
 Why is there so much iron?
 
 What does Deadpool mean by "left the house in that shirt"?
 
 Is honey really a supersaturated solution? Does heating to un-crystalize redissolve it or melt it?
 
 Is it insecure to send a password in a `curl` command?
 
 Can a wizard cast a spell during their first turn of combat if they initiated combat by releasing a readied spell?
 
 I got the following comment from a reputed math journal. What does it mean?
 
 Using Past-Perfect interchangeably with the Past Continuous
 
 My friend is being a hypocrite
 
 Do native speakers use "ultima" and "proxima" frequently in spoken English?
 
 Violin - Can double stops be played when the strings are not next to each other?
 
 Does multi-classing into Fighter give you heavy armor proficiency?
 
 Is it possible to stack the damage done by the Absorb Elements spell?
 
 What exactly term 'companion plants' means?
 
 Relation between independence and correlation of uniform random variables
 
 gerund and noun applications
 
 Worshiping one God at a time?
 
 A Ri-diddley-iley Riddle
 
 What does "Four-F." mean?
 
 What are idioms that are antonymous to "don't skimp on"?
 
 When to use snap-off blade knife and when to use trapezoid blade knife?
Should I explicitly delete structs in C++? [duplicate]
2019 Community Moderator ElectionCalling delete on variable allocated on the stackWhen to use “delete”?C++ and when to use deleteDo I have to delete struct pointer manually in C++?When should you use a class vs a struct in C++?What are the differences between a pointer variable and a reference variable in C++?What are the differences between struct and class in C++?When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?The Definitive C++ Book Guide and ListWhen to use struct?Difference between 'struct' and 'typedef struct' in C++?What is the “-->” operator in C++?typedef struct vs struct definitionsWhen exactly is it leak safe to use (anonymous) inner classes?
This question already has an answer here:
 When to use “delete”?
 
 5 answers
 
 
 C++ and when to use delete
 
 4 answers
 
 
 Calling delete on variable allocated on the stack
 
 11 answers
 
 
 Do I have to delete struct pointer manually in C++?
 
 4 answers
 
 
Let's say I have a struct defined as so:
struct Barre 
 int startString;
 int endString;
 Barre() startString = endString = -1; 
 Barre(int s, int e) : startString(s), endString(e) 
 bool exists() return startString > -1; 
 ;
I will create an instance of this struct like this, for example:
Barre b = Barre(2, 4);
Let's say I insert this into a std::map<int, Barre> which is a member of a class, with a key of, for example, 3.
If I then create another Barre as above and overwrite the value of the map at key 3 with this new instance of the Barre struct, do I need to explicitly delete the old Barre object that I'm overwriting to prevent memory leaks? Or does it not persist once it is no longer stored in a map in this way?
Thanks for any help.
c++ oop struct memory-leaks
 marked as duplicate by πάντα ῥεῖ
 StackExchange.ready(function() 
 if (StackExchange.options.isMobile) return;
 $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() 
 var $hover = $(this).addClass('hover-bound'),
 $msg = $hover.siblings('.dupe-hammer-message');
 $hover.hover(
 function() 
 $hover.showInfoMessage('', 
 messageElement: $msg.clone().show(),
 transient: false,
 position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
 dismissable: false,
 relativeToBody: true
 );
 ,
 function() 
 StackExchange.helpers.removeMessages();
 
 );
 );
 );
 Mar 7 at 17:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 4 more comments
This question already has an answer here:
 When to use “delete”?
 
 5 answers
 
 
 C++ and when to use delete
 
 4 answers
 
 
 Calling delete on variable allocated on the stack
 
 11 answers
 
 
 Do I have to delete struct pointer manually in C++?
 
 4 answers
 
 
Let's say I have a struct defined as so:
struct Barre 
 int startString;
 int endString;
 Barre() startString = endString = -1; 
 Barre(int s, int e) : startString(s), endString(e) 
 bool exists() return startString > -1; 
 ;
I will create an instance of this struct like this, for example:
Barre b = Barre(2, 4);
Let's say I insert this into a std::map<int, Barre> which is a member of a class, with a key of, for example, 3.
If I then create another Barre as above and overwrite the value of the map at key 3 with this new instance of the Barre struct, do I need to explicitly delete the old Barre object that I'm overwriting to prevent memory leaks? Or does it not persist once it is no longer stored in a map in this way?
Thanks for any help.
c++ oop struct memory-leaks
 marked as duplicate by πάντα ῥεῖ
 StackExchange.ready(function() 
 if (StackExchange.options.isMobile) return;
 $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() 
 var $hover = $(this).addClass('hover-bound'),
 $msg = $hover.siblings('.dupe-hammer-message');
 $hover.hover(
 function() 
 $hover.showInfoMessage('', 
 messageElement: $msg.clone().show(),
 transient: false,
 position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
 dismissable: false,
 relativeToBody: true
 );
 ,
 function() 
 StackExchange.helpers.removeMessages();
 
 );
 );
 );
 Mar 7 at 17:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
 
 
 1
 
 
 
 
 
 Also, as a rule of thumb for modern C++ (C++11 and later), you really shouldn't ever need to use- newand especially not- deleteyourself, unless you are doing some pretty deep library development, or use some existing framework which requires you to use them for legacy reasons (for example with Qt you'll use- new, but don't need- delete, and C++11 doesn't yet have- make_uniquefunction). Instead, use smart pointers.
 
 – hyde
 Mar 7 at 17:25
 
 
 
 
 
 
 3
 
 
 
 
 
 @JThistle "it's worth noting that I have searched for an answer to this and can't find anything." Well, this was the google query I used to find the duplicate questions: c++ when do i need delete site:stackoverflow.com
 
 – πάντα ῥεῖ
 Mar 7 at 17:27
 
 
 
 
 
 1
 
 
 
 
 
 I used 'do I need to delete a struct' and variations on that, DuckDuckGo returned nothing helpful.
 
 – JThistle
 Mar 7 at 17:30
 
 
 
 
 
 2
 
 
 
 
 
 @JThistle The problem with your query might possibly have been that you searched for- structspecifically, which is completely irrelevant for the question. A- structis just the same as a- classwith all members- publicas default. Nothing to do with dynamic memory management at all.
 
 – πάντα ῥεῖ
 Mar 7 at 17:36
 
 
 
 
 
 1
 
 
 
 
 
 @πάνταῥεῖ When reading questions, it's good to keep in mind that things that are self-evident for someone who knows a language, necessarily aren't that for everybody, even experienced programmers. For example a C# developer (I don't know if OP is) entering C++ world might have a bit of WTF moment when they learn that- structand- classare basically the same thing, and until that realization might have trouble formulating a suitable search engine query.
 
 – hyde
 Mar 7 at 18:23
 
 
 
|
show 4 more comments
This question already has an answer here:
 When to use “delete”?
 
 5 answers
 
 
 C++ and when to use delete
 
 4 answers
 
 
 Calling delete on variable allocated on the stack
 
 11 answers
 
 
 Do I have to delete struct pointer manually in C++?
 
 4 answers
 
 
Let's say I have a struct defined as so:
struct Barre 
 int startString;
 int endString;
 Barre() startString = endString = -1; 
 Barre(int s, int e) : startString(s), endString(e) 
 bool exists() return startString > -1; 
 ;
I will create an instance of this struct like this, for example:
Barre b = Barre(2, 4);
Let's say I insert this into a std::map<int, Barre> which is a member of a class, with a key of, for example, 3.
If I then create another Barre as above and overwrite the value of the map at key 3 with this new instance of the Barre struct, do I need to explicitly delete the old Barre object that I'm overwriting to prevent memory leaks? Or does it not persist once it is no longer stored in a map in this way?
Thanks for any help.
c++ oop struct memory-leaks
This question already has an answer here:
 When to use “delete”?
 
 5 answers
 
 
 C++ and when to use delete
 
 4 answers
 
 
 Calling delete on variable allocated on the stack
 
 11 answers
 
 
 Do I have to delete struct pointer manually in C++?
 
 4 answers
 
 
Let's say I have a struct defined as so:
struct Barre 
 int startString;
 int endString;
 Barre() startString = endString = -1; 
 Barre(int s, int e) : startString(s), endString(e) 
 bool exists() return startString > -1; 
 ;
I will create an instance of this struct like this, for example:
Barre b = Barre(2, 4);
Let's say I insert this into a std::map<int, Barre> which is a member of a class, with a key of, for example, 3.
If I then create another Barre as above and overwrite the value of the map at key 3 with this new instance of the Barre struct, do I need to explicitly delete the old Barre object that I'm overwriting to prevent memory leaks? Or does it not persist once it is no longer stored in a map in this way?
Thanks for any help.
This question already has an answer here:
 When to use “delete”?
 
 5 answers
 
 
 C++ and when to use delete
 
 4 answers
 
 
 Calling delete on variable allocated on the stack
 
 11 answers
 
 
 Do I have to delete struct pointer manually in C++?
 
 4 answers
 
 
c++ oop struct memory-leaks
c++ oop struct memory-leaks
asked Mar 7 at 17:16
JThistleJThistle
11711
11711
 marked as duplicate by πάντα ῥεῖ
 StackExchange.ready(function() 
 if (StackExchange.options.isMobile) return;
 $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() 
 var $hover = $(this).addClass('hover-bound'),
 $msg = $hover.siblings('.dupe-hammer-message');
 $hover.hover(
 function() 
 $hover.showInfoMessage('', 
 messageElement: $msg.clone().show(),
 transient: false,
 position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
 dismissable: false,
 relativeToBody: true
 );
 ,
 function() 
 StackExchange.helpers.removeMessages();
 
 );
 );
 );
 Mar 7 at 17:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
 marked as duplicate by πάντα ῥεῖ
 StackExchange.ready(function() 
 if (StackExchange.options.isMobile) return;
 $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() 
 var $hover = $(this).addClass('hover-bound'),
 $msg = $hover.siblings('.dupe-hammer-message');
 $hover.hover(
 function() 
 $hover.showInfoMessage('', 
 messageElement: $msg.clone().show(),
 transient: false,
 position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
 dismissable: false,
 relativeToBody: true
 );
 ,
 function() 
 StackExchange.helpers.removeMessages();
 
 );
 );
 );
 Mar 7 at 17:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
 
 
 1
 
 
 
 
 
 Also, as a rule of thumb for modern C++ (C++11 and later), you really shouldn't ever need to use- newand especially not- deleteyourself, unless you are doing some pretty deep library development, or use some existing framework which requires you to use them for legacy reasons (for example with Qt you'll use- new, but don't need- delete, and C++11 doesn't yet have- make_uniquefunction). Instead, use smart pointers.
 
 – hyde
 Mar 7 at 17:25
 
 
 
 
 
 
 3
 
 
 
 
 
 @JThistle "it's worth noting that I have searched for an answer to this and can't find anything." Well, this was the google query I used to find the duplicate questions: c++ when do i need delete site:stackoverflow.com
 
 – πάντα ῥεῖ
 Mar 7 at 17:27
 
 
 
 
 
 1
 
 
 
 
 
 I used 'do I need to delete a struct' and variations on that, DuckDuckGo returned nothing helpful.
 
 – JThistle
 Mar 7 at 17:30
 
 
 
 
 
 2
 
 
 
 
 
 @JThistle The problem with your query might possibly have been that you searched for- structspecifically, which is completely irrelevant for the question. A- structis just the same as a- classwith all members- publicas default. Nothing to do with dynamic memory management at all.
 
 – πάντα ῥεῖ
 Mar 7 at 17:36
 
 
 
 
 
 1
 
 
 
 
 
 @πάνταῥεῖ When reading questions, it's good to keep in mind that things that are self-evident for someone who knows a language, necessarily aren't that for everybody, even experienced programmers. For example a C# developer (I don't know if OP is) entering C++ world might have a bit of WTF moment when they learn that- structand- classare basically the same thing, and until that realization might have trouble formulating a suitable search engine query.
 
 – hyde
 Mar 7 at 18:23
 
 
 
|
show 4 more comments
 
 
 1
 
 
 
 
 
 Also, as a rule of thumb for modern C++ (C++11 and later), you really shouldn't ever need to use- newand especially not- deleteyourself, unless you are doing some pretty deep library development, or use some existing framework which requires you to use them for legacy reasons (for example with Qt you'll use- new, but don't need- delete, and C++11 doesn't yet have- make_uniquefunction). Instead, use smart pointers.
 
 – hyde
 Mar 7 at 17:25
 
 
 
 
 
 
 3
 
 
 
 
 
 @JThistle "it's worth noting that I have searched for an answer to this and can't find anything." Well, this was the google query I used to find the duplicate questions: c++ when do i need delete site:stackoverflow.com
 
 – πάντα ῥεῖ
 Mar 7 at 17:27
 
 
 
 
 
 1
 
 
 
 
 
 I used 'do I need to delete a struct' and variations on that, DuckDuckGo returned nothing helpful.
 
 – JThistle
 Mar 7 at 17:30
 
 
 
 
 
 2
 
 
 
 
 
 @JThistle The problem with your query might possibly have been that you searched for- structspecifically, which is completely irrelevant for the question. A- structis just the same as a- classwith all members- publicas default. Nothing to do with dynamic memory management at all.
 
 – πάντα ῥεῖ
 Mar 7 at 17:36
 
 
 
 
 
 1
 
 
 
 
 
 @πάνταῥεῖ When reading questions, it's good to keep in mind that things that are self-evident for someone who knows a language, necessarily aren't that for everybody, even experienced programmers. For example a C# developer (I don't know if OP is) entering C++ world might have a bit of WTF moment when they learn that- structand- classare basically the same thing, and until that realization might have trouble formulating a suitable search engine query.
 
 – hyde
 Mar 7 at 18:23
 
 
 
1
1
Also, as a rule of thumb for modern C++ (C++11 and later), you really shouldn't ever need to use
new and especially not delete yourself, unless you are doing some pretty deep library development, or use some existing framework which requires you to use them for legacy reasons (for example with Qt you'll use new, but don't need delete, and C++11 doesn't yet have make_unique function). Instead, use smart pointers.– hyde
Mar 7 at 17:25
Also, as a rule of thumb for modern C++ (C++11 and later), you really shouldn't ever need to use
new and especially not delete yourself, unless you are doing some pretty deep library development, or use some existing framework which requires you to use them for legacy reasons (for example with Qt you'll use new, but don't need delete, and C++11 doesn't yet have make_unique function). Instead, use smart pointers.– hyde
Mar 7 at 17:25
3
3
@JThistle "it's worth noting that I have searched for an answer to this and can't find anything." Well, this was the google query I used to find the duplicate questions: c++ when do i need delete site:stackoverflow.com
– πάντα ῥεῖ
Mar 7 at 17:27
@JThistle "it's worth noting that I have searched for an answer to this and can't find anything." Well, this was the google query I used to find the duplicate questions: c++ when do i need delete site:stackoverflow.com
– πάντα ῥεῖ
Mar 7 at 17:27
1
1
I used 'do I need to delete a struct' and variations on that, DuckDuckGo returned nothing helpful.
– JThistle
Mar 7 at 17:30
I used 'do I need to delete a struct' and variations on that, DuckDuckGo returned nothing helpful.
– JThistle
Mar 7 at 17:30
2
2
@JThistle The problem with your query might possibly have been that you searched for
struct specifically, which is completely irrelevant for the question. A struct is just the same as a class with all members public as default. Nothing to do with dynamic memory management at all.– πάντα ῥεῖ
Mar 7 at 17:36
@JThistle The problem with your query might possibly have been that you searched for
struct specifically, which is completely irrelevant for the question. A struct is just the same as a class with all members public as default. Nothing to do with dynamic memory management at all.– πάντα ῥεῖ
Mar 7 at 17:36
1
1
@πάνταῥεῖ When reading questions, it's good to keep in mind that things that are self-evident for someone who knows a language, necessarily aren't that for everybody, even experienced programmers. For example a C# developer (I don't know if OP is) entering C++ world might have a bit of WTF moment when they learn that
struct and class are basically the same thing, and until that realization might have trouble formulating a suitable search engine query.– hyde
Mar 7 at 18:23
@πάνταῥεῖ When reading questions, it's good to keep in mind that things that are self-evident for someone who knows a language, necessarily aren't that for everybody, even experienced programmers. For example a C# developer (I don't know if OP is) entering C++ world might have a bit of WTF moment when they learn that
struct and class are basically the same thing, and until that realization might have trouble formulating a suitable search engine query.– hyde
Mar 7 at 18:23
|
show 4 more comments
 0
 
active
oldest
votes
 0
 
active
oldest
votes
 0
 
active
oldest
votes
active
oldest
votes
active
oldest
votes
 
1
Also, as a rule of thumb for modern C++ (C++11 and later), you really shouldn't ever need to use
newand especially notdeleteyourself, unless you are doing some pretty deep library development, or use some existing framework which requires you to use them for legacy reasons (for example with Qt you'll usenew, but don't needdelete, and C++11 doesn't yet havemake_uniquefunction). Instead, use smart pointers.– hyde
Mar 7 at 17:25
3
@JThistle "it's worth noting that I have searched for an answer to this and can't find anything." Well, this was the google query I used to find the duplicate questions: c++ when do i need delete site:stackoverflow.com
– πάντα ῥεῖ
Mar 7 at 17:27
1
I used 'do I need to delete a struct' and variations on that, DuckDuckGo returned nothing helpful.
– JThistle
Mar 7 at 17:30
2
@JThistle The problem with your query might possibly have been that you searched for
structspecifically, which is completely irrelevant for the question. Astructis just the same as aclasswith all memberspublicas default. Nothing to do with dynamic memory management at all.– πάντα ῥεῖ
Mar 7 at 17:36
1
@πάνταῥεῖ When reading questions, it's good to keep in mind that things that are self-evident for someone who knows a language, necessarily aren't that for everybody, even experienced programmers. For example a C# developer (I don't know if OP is) entering C++ world might have a bit of WTF moment when they learn that
structandclassare basically the same thing, and until that realization might have trouble formulating a suitable search engine query.– hyde
Mar 7 at 18:23