Re: Managing malloced memory

2021-10-12 Thread anon via Digitalmars-d-learn
On Wednesday, 6 October 2021 at 18:29:34 UTC, Steven Schveighoffer wrote: ```d struct GCWrapped(T) { private T *_val; this(T* val) { _val = val; } ref T get() { return *_val; } alias get this; // automatically unwrap ~this() { free(_val); _val = null; } @disable this(this); //

Re: Managing malloced memory

2021-10-11 Thread anon via Digitalmars-d-learn
On Thursday, 7 October 2021 at 11:55:35 UTC, Steven Schveighoffer wrote: The GC is technically not required to free any blocks ever. But in general, it does. When it does free a struct, as long as you allocated with `new`, it should call the dtor. In practice when I played around with it,

Re: Managing malloced memory

2021-10-06 Thread anon via Digitalmars-d-learn
I found https://dlang.org/library/std/typecons/unique.html , which I think solves my problem by disabling copying. Thanks for the help.

Re: Managing malloced memory

2021-10-06 Thread anon via Digitalmars-d-learn
Sorry for messed up post, fixed it. On Wednesday, 6 October 2021 at 18:29:34 UTC, Steven Schveighoffer wrote: You can return this thing and pass it around, and the GC will keep it alive until it's not needed. Then on collection, the value is freed. Is the gc required to call ~this() on the

Re: Managing malloced memory

2021-10-06 Thread anon via Digitalmars-d-learn
Thanks for the help. On Wednesday, 6 October 2021 at 18:29:34 UTC, Steven Schveighoffer wrote: You can return this thing and pass it around, and the GC will keep it alive until it's not needed. Then on collection, the value is freed. Is the gc required to call ~this() on the struct? I

Managing malloced memory

2021-10-06 Thread anon via Digitalmars-d-learn
I interface to a C library that gives me a malloced object. How can I manage that pointer so that it gets freed automatically. What I've thought of so far: * scope(exit): not an option because I want to return that memory * struct wrapper: Doesn't work because if I pass it to another function,

Re: Idiomatic way to write a range that tracks how much it consumes

2020-04-26 Thread anon via Digitalmars-d-learn
To implement your option A you could simply use std.range.enumerate. Would something like this work? import std.algorithm.iteration : map; import std.algorithm.searching : until; import std.range : tee; size_t bytesConsumed; auto result = input.map!(a => a.yourTransformation )

Re: char array weirdness

2016-03-28 Thread Anon via Digitalmars-d-learn
On Monday, 28 March 2016 at 23:06:49 UTC, Anon wrote: Any because you're using ranges, *And because you're using ranges,

Re: char array weirdness

2016-03-28 Thread Anon via Digitalmars-d-learn
On Monday, 28 March 2016 at 22:49:28 UTC, Jack Stouffer wrote: On Monday, 28 March 2016 at 22:43:26 UTC, Anon wrote: On Monday, 28 March 2016 at 22:34:31 UTC, Jack Stouffer wrote: void main () { import std.range.primitives; char[] val = ['1', '0', 'h', '3', '6', 'm', '2', '8', 's'];

Re: char array weirdness

2016-03-28 Thread Anon via Digitalmars-d-learn
On Monday, 28 March 2016 at 22:34:31 UTC, Jack Stouffer wrote: void main () { import std.range.primitives; char[] val = ['1', '0', 'h', '3', '6', 'm', '2', '8', 's']; pragma(msg, ElementEncodingType!(typeof(val))); pragma(msg, typeof(val.front)); } prints char dchar

Re: Is it safe to use 'is' to compare types?

2016-03-08 Thread Anon via Digitalmars-d-learn
On Tuesday, 8 March 2016 at 20:26:04 UTC, Yuxuan Shui wrote: On Monday, 7 March 2016 at 16:13:45 UTC, Steven Schveighoffer wrote: On 3/4/16 4:30 PM, Yuxuan Shui wrote: On Friday, 4 March 2016 at 15:18:55 UTC, Steven Schveighoffer wrote: [...] Thanks for answering. But I still don't

Re: If stdout is __gshared, why does this throw / crash?

2016-03-05 Thread Anon via Digitalmars-d-learn
On Saturday, 5 March 2016 at 14:18:31 UTC, Atila Neves wrote: With a small number of threads, things work as intended in the code below. But with 1000, on my machine it either crashes or throws an exception: import std.stdio; import std.parallelism; import std.range; void main() {

Re: Voldemort Type Construction Error

2016-01-15 Thread Anon via Digitalmars-d-learn
On Friday, 15 January 2016 at 14:04:50 UTC, Nordlöw wrote: What have I missed? In line 126, `static struct Result()` is a template. Either drop the parens there, or change the call on line 187 to `Result!()(haystack, needles)`.

Re: switch with enum

2015-11-25 Thread Anon via Digitalmars-d-learn
On Wednesday, 25 November 2015 at 21:26:09 UTC, Meta wrote: On Wednesday, 25 November 2015 at 20:47:35 UTC, anonymous wrote: Use `final switch`. Ordinary `switch`es need an explicit default case. `final switch`es have to cover all possibilities individually. Implicit default cases are not

Re: bigint compile time errors

2015-07-02 Thread Anon via Digitalmars-d-learn
On Friday, 3 July 2015 at 02:37:00 UTC, Paul D Anderson wrote: The following code fails to compile and responds with the given error message. Varying the plusTwo function doesn't work; as long as there is an arithmetic operation the error occurs. This works for me on OSX 10.10 (Yosemite)

Re: ldc std.getopt

2015-04-29 Thread Anon via Digitalmars-d-learn
On Wednesday, 29 April 2015 at 19:43:44 UTC, Laeeth Isharc wrote: I get the following errors under LDC (this is LDC beta, but same problem under master) although the code compiles fine under DMD. Am I doing something wrong? The help generating feature of std.getopt is new in 2.067. Use

Re: Initializing defaults based on type.

2015-03-07 Thread anon via Digitalmars-d-learn
On Friday, 6 March 2015 at 16:04:33 UTC, Benjamin Thaut wrote: On Friday, 6 March 2015 at 15:36:47 UTC, anon wrote: Hi, I can't figure this out. struct Pair(T) { T x; T y; alias x c; alias y r; } What would like is that the x and y to be initialized to different values depending on

Initializing defaults based on type.

2015-03-06 Thread anon via Digitalmars-d-learn
Hi, I can't figure this out. struct Pair(T) { T x; T y; alias x c; alias y r; } What would like is that the x and y to be initialized to different values depending on type eg: struct Container { Pair!double sample1; // This will initialize sample1 with 0 for both x and y

How can I convert the following C to D.

2015-01-21 Thread anon via Digitalmars-d-learn
I have the following C code, how can I do the same in D. Info **info; info = new Info*[hl + 2]; int r; for(r = 0; r hl; r++) { info[r] = new Info[vl + 2]; } info[r] = NULL; anon

Re: How can I convert the following C to D.

2015-01-21 Thread anon via Digitalmars-d-learn
On Wednesday, 21 January 2015 at 23:59:34 UTC, ketmar via Digitalmars-d-learn wrote: On Wed, 21 Jan 2015 23:50:59 + anon via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Wednesday, 21 January 2015 at 23:47:46 UTC, ketmar via Digitalmars-d-learn wrote: On Wed, 21 Jan

Re: How can I convert the following C to D.

2015-01-21 Thread anon via Digitalmars-d-learn
On Thursday, 22 January 2015 at 00:16:23 UTC, bearophile wrote: anon: I have the following C code, how can I do the same in D. Info **info; info = new Info*[hl + 2]; int r; for(r = 0; r hl; r++) { info[r] = new Info[vl + 2]; } info[r] = NULL; I suggest you to ignore ketmar, he's

Re: How can I convert the following C to D.

2015-01-21 Thread anon via Digitalmars-d-learn
On Wednesday, 21 January 2015 at 23:47:46 UTC, ketmar via Digitalmars-d-learn wrote: On Wed, 21 Jan 2015 23:44:49 + anon via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: I have the following C code, how can I do the same in D. Info **info; info = new Info*[hl + 2]; int

Re: D language manipulation of dataframe type structures

2014-12-26 Thread anon via Digitalmars-d-learn
On Wednesday, 25 September 2013 at 04:35:57 UTC, lomereiter wrote: I thought about it once but quickly abandoned the idea. The primary reason was that D doesn't have REPL and is thus not suitable for interactive data exploration. https://github.com/MartinNowak/drepl https://drepl.dawg.eu/

Re: Loops versus ranges

2014-12-19 Thread anon via Digitalmars-d-learn
On Friday, 19 December 2014 at 10:41:04 UTC, bearophile wrote: A case where the usage of ranges (UFCS chains) leads to very bad performance: import std.stdio: writeln; import std.algorithm: map, join; uint count1, count2; const(int)[] foo1(in int[] data, in int i, in int max) {