Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* Adator. prog.
  + feladatsor
  + quick link

Teaching

* Prog. for Data Sci.
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

scope

From the book Programming in D.

  • scope(exit): the expression is always executed when exiting the scope, regardless of whether successfully or due to an exception
  • scope(success): the expression is executed only if the scope is being exited successfully
  • scope(failure): the expression is executed only if the scope is being exited due to an exception
(1) example
import std.stdio;
import core.stdc.stdlib;

void main()
{
    int* p = cast(int*) malloc(10 * int.sizeof);
    scope(exit) free(p); // Ensure cleanup

    if (p is null)
    {
        // Handle allocation failure
        writeln("Error: malloc() failed");
        return;
    }

    // Use the memory
    p[0] = 42;
    writeln(p[0]);
    // ... rest of your code
}

Notes:

  • Here, scope(exit) guarantees that when we leave the scope, the allocated memory will be freed.
  • D is a garbage-collected language, so there are better alternatives than calling malloc() directly. This is just for the sake of the example.
  • scope statements can be specified as blocks as well:
scope(exit) {
    free(p); // Ensure cleanup
}
  • If you have several scope statements, their blocks are executed in reverse order. Imagine that these scope statements are put in a stack data structure.
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 September 03, 11:41