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?










0
















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.










share|improve this question













marked as duplicate by πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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





    @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 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





    @πάνταῥεῖ 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















0
















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.










share|improve this question













marked as duplicate by πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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





    @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 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





    @πάνταῥεῖ 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













0












0








0









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.










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 17:16









JThistleJThistle

11711




11711




marked as duplicate by πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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





    @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 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





    @πάνταῥεῖ 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












  • 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







  • 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 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





    @πάνταῥεῖ 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







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












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived