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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page