Author Topic: C++ Exception handlers  (Read 12147 times)

0 Members and 1 Guest are viewing this topic.

Offline ee851Topic starter

  • Regular Contributor
  • *
  • Posts: 122
  • Country: us
  • carbon-based caveman
Re: C++ Exception handlers
« Reply #25 on: May 13, 2012, 05:20:00 pm »
What is a memory leak?

I'd been thinking it means holes of unused RAM inside a thread's address space that develop as memory is allocated and deallocated during the thread's lifetime, similar to the holes  that develop in the filesystems of certain operating systems (and must be defragmented).  But I might be wrong.

I realize this discussion is going somewhat outside the context of my original question and am okay with that.

Yes, ejeffrey, you're right--I'm fuzzy on the whole idea of garbage collection. 

"delete is the opposite of garbage collection.  Garbage collection is a system that keeps track of all the pointers to objects, and when the last pointer goes out of scope, the object is automatically deleted, and its memory and hopefully other resources are freed.  "

So all I need to do is use new and delete statements in my code, and the linker automagically links code into the executable to invoke this "system" --based on object size --to allocate and deallocate memory as needed, eh?


nctnico,

Thanks for the example.   I think I understand now.   That's done by hidden code inserted by the C++ linker at link time, I'd imagine.

So the way to have "global variables" available at all times to all objects during  the lifetime of main() is to define them in the public section of objects declared prior to int main()  in my source code?

I share your enthusiasm for avoiding or minimizing dynamic memory allocation/deallocation, and with code in general.

I am not familiar with the STL libraries, but will search for them on the web.   Same with linked-list templates.

Thanks, AntiProtonBoy, for that illustrative example.  I understand assignment of objects to
existing objects (deep copying) requires writing a method for the equals operator and overloading it within the context of my object's definition.   I hope I remember!   I appreciate your taking time to write that example.   I think I understand now.

Thanks, IanB, for cluing me in on exception handlers.   I take it they operate in a similar, architecture-dependent fashion to interrupt handlers, namely,

(1) You set up the ESR to be linked to a specific physical memory address inside the thread.
(2) You enable/disable the ESR by writing a specific bit pattern to a specific register in the CPU.
(3) You're responsible for any "special" response to the hardware exception by putting a jump to the corresponding code into the ESR.
(4) The program counter continues with the thread's next instruction when control is returned from the ESR.


« Last Edit: May 13, 2012, 05:49:32 pm by ee851 »
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 28111
  • Country: nl
    • NCT Developments
Re: C++ Exception handlers
« Reply #26 on: May 13, 2012, 05:59:01 pm »
What is a memory leak?

I'd been thinking it means holes of unused RAM inside a thread's address space that develop as memory is allocated and deallocated during the thread's lifetime, similar to the holes  that develop in the filesystems of certain operating systems (and must be defragmented).  But I might be wrong.
You are  ;) A memory leak is allocated memory which is not freed. Memory fragmentation is another problem but there are interesting solutions for that as well. In of the microcontroller based devices I made I needed several buffers which could have changing sizes. What I wanted to avoid was memory fragmentation and using dynamic memory allocation. I ended up implementing a system which allowed to create buffers which could be expanded or shrunk based on a linked list. Each segment contained a small amount of memory and a index to the next segment (or end of buffer flag). This way I never had problems with memory fragmentation. Ofcourse this doesn't prevent an out-of-memory condition but that was not a real problem for the device.

Quote
nctnico,

Thanks for the example.   I think I understand now.   That's done by hidden code inserted by the C++ linker at link time, I'd imagine.

So the way to have "global variables" available at all times to all objects during  the lifetime of main() is to define them in the public section of objects declared prior to int main()  in my source code?

I share your enthusiasm for avoiding or minimizing dynamic memory allocation/deallocation, and with code in general.

I am not familiar with the STL libraries, but will search for them on the web.   Same with linked-list templates.
There is an interesting book called 'C++ data structures' which also explains the underlying mechanisms of the data storage templates from the STL library: http://www.amazon.com/Plus-Data-Structures-Nell-Dale/dp/1449646751/
Its a bit expensive but I got an old print from a book market for a few bucks.
« Last Edit: May 13, 2012, 06:23:21 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 12414
  • Country: us
Re: C++ Exception handlers
« Reply #27 on: May 13, 2012, 06:10:03 pm »
What is a memory leak?

A memory leak is simply a piece of memory that has been reserved for use by a program that never gets released when the program has finished with it and remains unused while the program runs on. Memory leaks can happen both in unmanaged memory environments like C++ and managed environments like .NET or Java. To make a memory leak all you have to do is hold on to the memory and not let it go.

Quote
So all I need to do is use new and delete statements in my code, and the linker automagically links code into the executable to invoke this "system" --based on object size --to allocate and deallocate memory as needed, eh?

Actually, no. It is not good to use new and delete in C++ -- in fact one could almost say "new and delete are considered harmful". I flag up the use of new and delete as suspect in code reviews.

At the microcontroller level you really shouldn't be designing code that needs dynamic memory management. There's no virtual memory system and there is no huge pool of memory to start with. In a microcontroller program you want to be explicitly accounting for and controlling every resource you use by design. Your program should be completely deterministic, stable and predictable in its behaviour, able to run forever without needing a reset. It's much harder to do this when you include complex features like dynamic memory management in your program.

At the desktop and server level, new and delete are very raw, low level features in C++ that are hard to use without risk of bugs or leaks. It is best to confine the use of new and delete to library code like the STL, and to have your own code use the higher level abstractions like STL containers for dynamic memory. Your code will be more readable and at the same time less prone to leaks or use of bad memory pointers. If you use C++ without the STL you are only using half of C++.
 

Offline ee851Topic starter

  • Regular Contributor
  • *
  • Posts: 122
  • Country: us
  • carbon-based caveman
Re: C++ Exception handlers
« Reply #28 on: May 14, 2012, 12:49:31 am »
At the microcontroller level you really shouldn't be designing code that needs dynamic memory management.

Amen, brother.

Thanks IanB, for your comments on memory leak.   

I have begun to educate myself on the STL, and to use its classes.    No sense in reinventing the wheel.
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: C++ Exception handlers
« Reply #29 on: May 14, 2012, 03:15:01 am »
Here's a little advise regarding C++.  When you think you have learned the language, read Scott Meyers' book Effective C++.

This book is a classic and every serious C++ programmer has a copy.  This isn't a book for learning C++ so find another book for that.  It is a book that tells you all the things you need to know to be an expert C++ developer.
« Last Edit: May 14, 2012, 05:09:10 am by TerminalJack505 »
 

Offline ee851Topic starter

  • Regular Contributor
  • *
  • Posts: 122
  • Country: us
  • carbon-based caveman
Re: C++ Exception handlers
« Reply #30 on: May 18, 2012, 09:34:28 pm »
Thank you for the book recommendation.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf