The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Transition from C# to C++

RevolutionaryRevolutionary Registered User regular
edited June 2008 in Help / Advice Forum
Hi guys. Over the last year I've been learning VB.NET, C#.NET and now I want to get some grounding in C++. My C# experience lends greatly to this, my only concern is memory management.

I know that I have to manually remove object's I'm no longer using and that if there are objects I'm no longer using then they are a memory leak.

But what, in terms of actually punching in code, does that entail? Do I simply do a 'delete <object here>' whenever it's not needed? It sounds too easy.

In short: In practice, what does managing memory entail?

I'm aware several books can be written on the topic, I'm asking about this from a hobbyist programmer's perspective.

Any help appreciated.

Revolutionary on

Posts

  • bowenbowen Sup? Registered User regular
    edited June 2008
    Do I simply do a 'delete <object here>'

    That's about the gist of it. For every new, you must have a corresponding delete.

    bowen on
    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • AndorienAndorien Registered User regular
    edited June 2008
    It sounds too easy.

    It really does, but that's where the trap can lie. In effect, you must always be sure than an object called with new gets killed with delete when you discard the pointer to it (either from simply not needing it, or the pointer going out of scope). This means that you must hunt down every possible means that your pointer to it goes out of scope, and ensure it either gets deleted beforehand, or you have another pointer to access it. In non-trivial projects, this can become a pain.

    Andorien on
  • theSquidtheSquid Sydney, AustraliaRegistered User regular
    edited June 2008
    Hence this is why you learn the STL. Quite frankly, that kind of memory management is prettier than the alternative in C, but it's still not terribly necessary in this day and age.

    95% of all the memory management you'll do is likely to end up implementing data structures, or common searching and sorting algorithms, or things like iterators. Guess what the C++ STL contains! Algorithms, data structures and iterators!

    Use them along with the OO aspects of C++ and you'll barely ever need to allocate memory anyway.

    theSquid on
  • bowenbowen Sup? Registered User regular
    edited June 2008
    theSquid wrote: »
    Hence this is why you learn the STL. Quite frankly, that kind of memory management is prettier than the alternative in C, but it's still not terribly necessary in this day and age.

    95% of all the memory management you'll do is likely to end up implementing data structures, or common searching and sorting algorithms, or things like iterators. Guess what the C++ STL contains! Algorithms, data structures and iterators!

    Use them along with the OO aspects of C++ and you'll barely ever need to allocate memory anyway.

    I hate it when people can't write their own linked list class, though. :|

    bowen on
    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • MonoxideMonoxide Registered User, ClubPA regular
    edited June 2008
    bowen wrote: »
    theSquid wrote: »
    Hence this is why you learn the STL. Quite frankly, that kind of memory management is prettier than the alternative in C, but it's still not terribly necessary in this day and age.

    95% of all the memory management you'll do is likely to end up implementing data structures, or common searching and sorting algorithms, or things like iterators. Guess what the C++ STL contains! Algorithms, data structures and iterators!

    Use them along with the OO aspects of C++ and you'll barely ever need to allocate memory anyway.

    I hate it when people can't write their own linked list class, though. :|

    Can't is one thing. Not having to is another.

    though if all you need is a linked list it's probably more efficient to write your own than to use the STL

    Monoxide on
  • bowenbowen Sup? Registered User regular
    edited June 2008
    Monoxide wrote: »
    bowen wrote: »
    theSquid wrote: »
    Hence this is why you learn the STL. Quite frankly, that kind of memory management is prettier than the alternative in C, but it's still not terribly necessary in this day and age.

    95% of all the memory management you'll do is likely to end up implementing data structures, or common searching and sorting algorithms, or things like iterators. Guess what the C++ STL contains! Algorithms, data structures and iterators!

    Use them along with the OO aspects of C++ and you'll barely ever need to allocate memory anyway.

    I hate it when people can't write their own linked list class, though. :|

    Can't is one thing. Not having to is another.

    though if all you need is a linked list it's probably more efficient to write your own than to use the STL

    Yeah along with no knowledge what-so-ever of sorting algorithms, other data structures (trees at least, not-so-much graphs), and Big-O notation. Want to take it beyond hobby, definitely learn those things as it's pretty invaluable in the job.

    bowen on
    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • LewishamLewisham Registered User regular
    edited June 2008
    One thing I would say is that C# is nothing like C++ aside from the fact they have C-style syntax.

    C++ is a completely different beast.

    Lewisham on
  • theSquidtheSquid Sydney, AustraliaRegistered User regular
    edited June 2008
    Monoxide wrote: »
    bowen wrote: »
    theSquid wrote: »
    Hence this is why you learn the STL. Quite frankly, that kind of memory management is prettier than the alternative in C, but it's still not terribly necessary in this day and age.

    95% of all the memory management you'll do is likely to end up implementing data structures, or common searching and sorting algorithms, or things like iterators. Guess what the C++ STL contains! Algorithms, data structures and iterators!

    Use them along with the OO aspects of C++ and you'll barely ever need to allocate memory anyway.

    I hate it when people can't write their own linked list class, though. :|

    Can't is one thing. Not having to is another.

    though if all you need is a linked list it's probably more efficient to write your own than to use the STL

    Agree on the first point, disagree on the second.

    It is not more efficient to write your own linked list class. The STL does all its magicks through templates and code generation, so while it may take slightly longer to compile it will be just as fast. Additionally, they're exception-safe.

    Although bowen is right - all of those data structures should be memorised - stacks, queues, vectors, lists etc. Along with all their public member functions.

    theSquid on
Sign In or Register to comment.