[Info-vax] BASIC (and Horizon)

Arne Vajhøj arne at vajhoej.dk
Wed Jan 31 13:29:30 EST 2024


On 1/31/2024 11:17 AM, Scott Dorsey wrote:
> Michael S  <already5chosen at yahoo.com> wrote:
>>
>> The third option, the one I like least (an understatement) is use of
>> exceptions. Despite my personal preferences, it's quite popular.
> 
> Exceptions can be very elegant or very inelegant... and it is entirely
> possible to have an exception that doesn't clean anything up at all and
> just exits and lets the operating system deal with the memory.  On many
> systems this is a great idea because being able to exit quickly on a failure
> is more important than people give it credit for.  On some other systems
> that have memory management issues it can lead to leakage.

It is probably a bit easier to implement exceptions in languages
with garbage collection and a lot easier for developers to avoid
memory leak bugs.

$ type mem.cpp
#include <iostream>
#include <string>

using namespace std;

class X
{
private:
     string id;
public:
     X(string id) { this->id = id; cout << "Allocate " << id << endl; }
     virtual ~X() { cout << "Deallocate " << id << endl; }
};

void f2()
{
    throw "Houston we have a problem";
}

void f1()
{
    X o2("o2");
    X *o3 = new X("o3");
    f2();
    delete o3;
}

int main()
{
     try
     {
         X o1("o1");
         f1();
     }
     catch(char *ex)
     {
         cout << ex << endl;
     }
     return 0;
}

$ cxx mem.cpp
$ cxxlink mem
link  mem
$ run mem
Allocate o1
Allocate o2
Allocate o3
Deallocate o2
Deallocate o1
Houston we have a problem
$

We see that o2 did get deallocated, but o3 did not get deallocated.

Arne





More information about the Info-vax mailing list