Re: Throw stack trace from program kill

2022-01-16 Thread Era Scarecrow via Digitalmars-d-learn
On Sunday, 16 January 2022 at 18:03:53 UTC, Paul Backus wrote: On POSIX, you can use the `sigaction` function to install a signal handler for `SIGINT`, the signal generated by CTRL+C. To terminate the program with a stack trace, simply have the signal handler `throw` an `Error`. I never

Re: Dynamic array or not

2022-01-16 Thread Era Scarecrow via Digitalmars-d-learn
On Sunday, 16 January 2022 at 15:32:41 UTC, Salih Dincer wrote: If count is not equal to 8 I get weird results! The reason of course, is the free(): // [93947717336544, 1, 2, 3, 4, 5, 6] I wonder if you're seeing something you're not suppose to as part of the internals; Typically malloc for

Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Monday, 17 January 2022 at 03:11:50 UTC, Steven Schveighoffer wrote: The profile=gc appears to only show GC allocations that the *compiler* initiates (i.e. via `new`, array operations (like appending) or closure allocations). It does not detect that the functions that actually allocate

Re: Dynamic array ot not

2022-01-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/16/22 9:42 PM, forkit wrote: On Monday, 17 January 2022 at 00:54:19 UTC, forkit wrote: // mArr2 is a dynamic array allocated on the gc heap // although compiling with '-profile=gc' incorrectly suggests otherwise. if add @nogc to above code: (8): Error: array literal in

Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Monday, 17 January 2022 at 00:54:19 UTC, forkit wrote: .. module test; import std; @safe void main() { // mArr1 is a dynamic array allocated on the gc heap. int[][] mArr1 = [[1, 2], [3, 4], [5, 6], [7, 8]]; static assert(is(typeof(mArr1.array(; alias R1 =

Re: Throw stack trace from program kill

2022-01-16 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Jan 16, 2022 at 01:42:21PM -0500, Steven Schveighoffer via Digitalmars-d-learn wrote: > On 1/16/22 1:33 PM, Paul Backus wrote: [...] > > ```d > > extern(C) void handleCtrlC(int) > > { > > import core.stdc.stdlib: exit; > > import std.stdio: writeln; > > > > try throw new

Re: Dynamic array ot not

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 17:51, H. S. Teoh wrote: >> In practice, malloc'ed memory is cleared e.g. by memset(). Or, there is >> calloc() which returns memory filled with zeros. > > Correction: malloc() is not guaranteed to clear the allocated memory. Agreed. What I meant is, the programmer ordinarily clears

Re: Dynamic array ot not

2022-01-16 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 17 January 2022 at 01:06:05 UTC, Salih Dincer wrote: ```d // Taaata, magic... // Your eyes don't surprise you! typeid(range).writeln(": ", range); typeid(slices).writeln(": ", slices); ``` In fact, although range and slice seem to be equal to each other, they are not!

Re: Dynamic array ot not

2022-01-16 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Jan 16, 2022 at 08:21:29AM -0800, Ali Çehreli via Digitalmars-d-learn wrote: > On 1/16/22 07:32, Salih Dincer wrote: > > On Sunday, 16 January 2022 at 11:43:40 UTC, Ali Çehreli wrote: > >> > >> void main() { > >> enum count = 7; > >> > >> // Allocate some memory > >> void* rawData =

Re: Dynamic array ot not

2022-01-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/16/22 7:54 PM, forkit wrote: On Sunday, 16 January 2022 at 23:34:41 UTC, Ali Çehreli wrote: Definitely a -profile=gc bug. Here are the existing ones:   https://issues.dlang.org/buglist.cgi?quicksearch=profile%20gc Ali yeah, a bug makes more sense ... otherwise I really would have had

Re: Dynamic array ot not

2022-01-16 Thread Salih Dincer via Digitalmars-d-learn
```d import std; // If we summarize in code... void main() { // It's a dynamic array and its copy below: size_t[] arr = [1, 2, 3, 4, 5, 6]; auto arrCopy = arr.dup; // This is its lazy range: auto range = arr.chunks(2); typeid(range).writeln(": ", range); // But this is its copy

Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Sunday, 16 January 2022 at 23:34:41 UTC, Ali Çehreli wrote: Definitely a -profile=gc bug. Here are the existing ones: https://issues.dlang.org/buglist.cgi?quicksearch=profile%20gc Ali yeah, a bug makes more sense ... otherwise I really would have had a slice to data that doesn't exist

Re: Dynamic array ot not

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 15:14, forkit wrote: > But -profile=gc .. says no. It's not. > > int[][] mArr = [[1, 2], [3, 4], [5, 6], [7, 8]]; // GC allocation occurs. > int[][] mArr = iota(1, 9).chunks(2).map!array.array; // No GC allocation > occurs. Definitely a -profile=gc bug. Here are the existing ones:

Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Sunday, 16 January 2022 at 23:03:49 UTC, Ali Çehreli wrote: That's not correct. There are many range algorithms that are lazy to defer memory allocation but array() is not one of those. array() does eagerly allocate memory, which is it's whole purpose:

Re: Dynamic array ot not

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 14:43, forkit wrote: > Well, it's fair to say, that 'range-based programming' is kinda new to me. I hope it will be useful to you. :) > int[][] mArr2 = [[1, 2], [3, 4], [5, 6], [7, 8]]; // GC allocation > > But it turns out: > > int[][] mArr = iota(1, 9).chunks(2).map!array.array;

Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Sunday, 16 January 2022 at 11:43:40 UTC, Ali Çehreli wrote: So, in all three examples it is the same D feature, a slice, that references data but the data is managed in different ways. Ali Well, it's fair to say, that 'range-based programming' is kinda new to me. With this statement:

Re: Unintuitive behavior with shared classes & template member functions; is this intended?

2022-01-16 Thread Jaime via Digitalmars-d-learn
On Saturday, 15 January 2022 at 02:52:20 UTC, Steven Schveighoffer wrote: you probably can fix the issue via: ```d class C { shared: ... // everything } ``` This seems like a good solution. To achieve implicit sharing on the aggregate itself, I can use this approach to define a

Re: Improve a simple event handler

2022-01-16 Thread JN via Digitalmars-d-learn
On Saturday, 15 January 2022 at 23:15:16 UTC, JN wrote: Is there some way I could improve this with some D features? My main gripes with it are: Managed to dramatically simplify it to 10 lines of code with variadic templates. ```d import std.stdio; struct Event(T...) { void

Re: Throw stack trace from program kill

2022-01-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/16/22 1:33 PM, Paul Backus wrote: It worked when I tested it, Yeah, but your example is designed specifically to only encounter the signal in one specific spot (inside a D function). but I'm not sure how reliable it is. A more conservative implementation would be something like

Re: Throw stack trace from program kill

2022-01-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/16/22 1:42 PM, Steven Schveighoffer wrote: This too is not going to be a good idea. writeln(e.info) is going to possibly start allocating. A signal can come at any time, even when locks are held or things are in an intermediate state. That being said, if this is being used for

Re: Throw stack trace from program kill

2022-01-16 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 16 January 2022 at 18:22:04 UTC, Steven Schveighoffer wrote: Does this work normally? The memory error handler for Linux jumps through a lot of hoops to be able to throw an error from a signal handler. See https://github.com/dlang/druntime/blob/master/src/etc/linux/memoryerror.d

Re: Throw stack trace from program kill

2022-01-16 Thread Adam D Ruppe via Digitalmars-d-learn
On Sunday, 16 January 2022 at 18:03:53 UTC, Paul Backus wrote: extern(C) void handleCtrlC(int) { throw new Error("Killed by CTRL+C"); } This is really iffy since signals can come to random threads at random times. The best thing to do is typically to just set a "user requested

Re: Throw stack trace from program kill

2022-01-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/16/22 1:03 PM, Paul Backus wrote: On Sunday, 16 January 2022 at 15:15:07 UTC, Hipreme wrote: Is there some way to throw a stack trace when killing the program from the CTRL+C from the terminal? It would help a lot into debugging occasional infinity loops On POSIX, you can use the

Re: Throw stack trace from program kill

2022-01-16 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 16 January 2022 at 15:15:07 UTC, Hipreme wrote: Is there some way to throw a stack trace when killing the program from the CTRL+C from the terminal? It would help a lot into debugging occasional infinity loops On POSIX, you can use the `sigaction` function to install a signal

Re: Throw stack trace from program kill

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 07:15, Hipreme wrote: > Is there some way to throw a stack trace when killing the program from > the CTRL+C from the terminal? I am interested in how to add Ctrl+C support for a D program as well. > It would help a lot into debugging occasional infinity loops One way of achieving

Re: Dynamic array ot not

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 07:32, Salih Dincer wrote: > On Sunday, 16 January 2022 at 11:43:40 UTC, Ali Çehreli wrote: >> >> void main() { >> enum count = 7; >> >> // Allocate some memory >> void* rawData = malloc(int.sizeof * count); In practice, malloc'ed memory is cleared e.g. by memset(). Or, there is

Re: Dynamic array ot not

2022-01-16 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 16 January 2022 at 11:43:40 UTC, Ali Çehreli wrote: void main() { enum count = 7; // Allocate some memory void* rawData = malloc(int.sizeof * count); If count is not equal to 8 I get weird results! The reason of course, is the free(): // [93947717336544, 1, 2, 3, 4, 5, 6]

Throw stack trace from program kill

2022-01-16 Thread Hipreme via Digitalmars-d-learn
Is there some way to throw a stack trace when killing the program from the CTRL+C from the terminal? It would help a lot into debugging occasional infinity loops

Re: Dynamic array ot not

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 01:43, forkit wrote: > On Sunday, 16 January 2022 at 04:58:21 UTC, Ali Çehreli wrote: >> >> I have a problem with calling type[] a dynamic array because it is a >> slice, which may be providing access to the elements of a dynamic array. >> > > Yes. A more useful way of describing []

Re: Starting and managing threads

2022-01-16 Thread forkit via Digitalmars-d-learn
On Monday, 27 December 2021 at 10:59:07 UTC, Ali Çehreli wrote: ...my DConf Online 2020 presentation here: https://dconf.org/2020/online/#ali1 Ali Hey, that is a really great presentation! Many more people should watch it, and learn ;-)

Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Sunday, 16 January 2022 at 04:58:21 UTC, Ali Çehreli wrote: I have a problem with calling type[] a dynamic array because it is a slice, which may be providing access to the elements of a dynamic array. Yes. A more useful way of describing [] would be to say: "[] represents a dynamic

Re: Starting and managing threads

2022-01-16 Thread Bagomot via Digitalmars-d-learn
The program does nothing probably because of that continue. (?) No, it does work inside the loop. So, the event loop is in a separate thread. What should happen when the events trigger? Do you want this thread to handle the events or should this thread send a > message to the main thread