Casting pointers

2015-08-26 Thread John Burton via Digitalmars-d-learn
This would be undefined behavior in c++ due to aliasing rules on pointers. It appears to work reliably in D when I try it, but that's obviously no guarantee that it's correct or will continue to do so. Is this correct code in D? And if not, what should I do instead to cleanly and efficiently

Arrays of structs

2015-08-27 Thread John Burton via Digitalmars-d-learn
I'm a c++ programmer trying to understand how memory allocation works in D. I created a struct and added a destructor to it. My understanding is that structs have deterministic destructors - they are called when the struct goes out of scope (unless it is allocated with new). Now if I put

Re: Arrays of structs

2015-08-27 Thread John Burton via Digitalmars-d-learn
Ok that's great thank you. It's quite hard trying to get a proper understanding of memory allocation in D after years of C++ / RAII / unique_ptr / shared_ptr . I understand the details of course but it's going to take a while to really know it. Is there any way to explicitly free a dynamic

Re: Arrays of structs

2015-08-27 Thread John Burton via Digitalmars-d-learn
Thanks again for the updates. I've experimented some more and believe I understand. To be honest I'm finding it very hard to find the right idioms in D for safe and efficient programming when I'm so used to C++ / RAII everywhere. I'll adapt though :P

Re: Casting pointers

2015-08-27 Thread John Burton via Digitalmars-d-learn
Ok thank you. Seems to me that there is enough doubt that this is supported that I'd be best to avoid relying on it, or at least ensure that it's all encapsulated in something I can easily change.

C style 'static' functions

2017-07-19 Thread John Burton via Digitalmars-d-learn
In C I can declare a function 'static' and it's only visible from within that implementation file. So I can have a static function 'test' in code1.c and another non static function 'test' in utils.c and assuming a suitable prototype I can use 'test' in my program and the one in code1.c will

Re: Append to dynamic array that was allocated via calloc

2017-07-25 Thread John Burton via Digitalmars-d-learn
On Tuesday, 25 July 2017 at 13:24:36 UTC, Mike Parker wrote: On Tuesday, 25 July 2017 at 12:40:13 UTC, John Burton wrote: [...] This should give you the answer: writefln("Before: ptr = %s capacity = %s", slice.ptr, slice.capacity); slice ~= 1; writefln("After: ptr = %s capacity = %s",

Append to dynamic array that was allocated via calloc

2017-07-25 Thread John Burton via Digitalmars-d-learn
I can create a "slice" using non-gc allocated memory. int* ptr = cast(int*)calloc(int.sizeof, 10); int[] data = ptr[0..10]; If I don't want a memory leak I have to call free(ptr) somewhere as it won't be GC collected when data or ptr go out of scope. I presume there is nothing

Re: C style 'static' functions

2017-07-19 Thread John Burton via Digitalmars-d-learn
On Wednesday, 19 July 2017 at 12:05:09 UTC, Kagamin wrote: Try a newer compiler, this was fixed recently. Hmm it turns out this machine has 2.0.65 on which is fairly ancient. I'd not realized this machine had not been updated. Sorry for wasting everyones' time if that's so, and thanks for

Re: C style 'static' functions

2017-07-19 Thread John Burton via Digitalmars-d-learn
On Wednesday, 19 July 2017 at 11:31:32 UTC, Jacob Carlborg wrote: On 2017-07-19 09:22, John Burton wrote: In C I can declare a function 'static' and it's only visible from within that implementation file. So I can have a static function 'test' in code1.c and another non static function 'test'

Re: C style 'static' functions

2017-07-19 Thread John Burton via Digitalmars-d-learn
On Wednesday, 19 July 2017 at 12:15:05 UTC, Mike Parker wrote: On Wednesday, 19 July 2017 at 12:11:38 UTC, John Burton wrote: On Wednesday, 19 July 2017 at 12:05:09 UTC, Kagamin wrote: Try a newer compiler, this was fixed recently. Hmm it turns out this machine has 2.0.65 on which is fairly

Re: C style 'static' functions

2017-07-19 Thread John Burton via Digitalmars-d-learn
On Wednesday, 19 July 2017 at 07:51:11 UTC, Gary Willoughby wrote: On Wednesday, 19 July 2017 at 07:22:48 UTC, John Burton wrote: In C++ I could use static or an anonymous namespace for implementation functions, but there doesn't seem to be anything similar in D. Is there any way to achieve

Re: Advice wanted on garbage collection of sockets for c++ programmer using D

2017-06-28 Thread John Burton via Digitalmars-d-learn
On Tuesday, 27 June 2017 at 09:54:19 UTC, John Burton wrote: I'm coming from a C++ background so I'm not too used to garbage collection and it's implications. I have a function that creates a std.socket.Socket using new and connects to a tcp server, and writes some stuff to it. I then

Advice wanted on garbage collection of sockets for c++ programmer using D

2017-06-27 Thread John Burton via Digitalmars-d-learn
I'm coming from a C++ background so I'm not too used to garbage collection and it's implications. I have a function that creates a std.socket.Socket using new and connects to a tcp server, and writes some stuff to it. I then explicitly close the socket, and the socket object goes out of scope.

Re: Advice wanted on garbage collection of sockets for c++ programmer using D

2017-06-27 Thread John Burton via Digitalmars-d-learn
On Tuesday, 27 June 2017 at 10:14:16 UTC, Jonathan M Davis wrote: On Tuesday, June 27, 2017 09:54:19 John Burton via Digitalmars-d-learn wrote: I'm coming from a C++ background so I'm not too used to garbage collection and it's implications. I have a function that creates a std.socket.Socket

Assert and undefined behavior

2017-10-11 Thread John Burton via Digitalmars-d-learn
The spec says this :- "As a contract, an assert represents a guarantee that the code must uphold. Any failure of this expression represents a logic error in the code that must be fixed in the source code. A program for which the assert contract is false is, by definition, invalid, and

Understanding gc memory profile report

2017-09-08 Thread John Burton via Digitalmars-d-learn
I wrote this simple program to test out my understanding of memory allocation :- import std.stdio; void main() { int [] array = new int[250]; writeln(array.length, " elements ", array); // Append one value to the array array ~= 123;

Re: Address of data that is static, be it shared or tls or __gshared or immutable on o/s

2017-09-11 Thread John Burton via Digitalmars-d-learn
On Sunday, 10 September 2017 at 21:38:03 UTC, Cecil Ward wrote: On Wednesday, 6 September 2017 at 15:55:35 UTC, Ali Çehreli wrote: [...] Ali, I have worked on operating systems' development in r+d. My definitions of terms are hopefully the same as yours. If we refer to two threads, if they

fromStringz for wide characters

2017-09-05 Thread John Burton via Digitalmars-d-learn
std.string.fromStringz will create me a string from a null terminated array of characters. But I have a zero terminated array of "short"s (from a win32 api call) which I'd like to turn into a wstring. But there doesn't seem to be a function to do this. Do I need to write my own, or am I

Is there any threadsafe queue?

2017-09-13 Thread John Burton via Digitalmars-d-learn
Is there any threadsafe queue in the standard library? I've not been able to find anything but thought I'd check before making my own. I want to be able to assemble messages (Which are just streams of bytes) in one thread into a struct and push them to the queue, and then have a another

Re: Is there any threadsafe queue?

2017-09-13 Thread John Burton via Digitalmars-d-learn
On Wednesday, 13 September 2017 at 09:49:46 UTC, Jonathan M Davis wrote: On Wednesday, September 13, 2017 07:51:19 John Burton via Digitalmars-d- learn wrote: [...] You could probably do what you want with std.concurrency, since most of what it does is deal with sending and receiving data

Is this defined behaviour in D?

2017-09-25 Thread John Burton via Digitalmars-d-learn
If I have a int* pointer for example, that points to the start of an int array and step through the array until I get the value 123, is it defined in D what happens if you step off the end of the array? What I might expect to happen is for the code to just keep stepping through sequential

Re: fromStringz for wide characters

2017-09-05 Thread John Burton via Digitalmars-d-learn
On Tuesday, 5 September 2017 at 08:39:37 UTC, Jonathan M Davis wrote: On Tuesday, September 05, 2017 08:15:04 John Burton via Digitalmars-d-learn wrote: std.string.fromStringz will create me a string from a null terminated array of characters. But I have a zero terminated array of "sh

Re: writeln() sometimes double prints from main() if I run a thread checking for input?

2017-09-05 Thread John Burton via Digitalmars-d-learn
On Thursday, 31 August 2017 at 21:06:17 UTC, Ivan Kazmenko wrote: On Thursday, 31 August 2017 at 14:43:39 UTC, Steven Schveighoffer wrote: Just a thought, but the "double printing" could be a misunderstanding. It could be printing Output\nOutput2, but not getting the 2 out there. Just to

Re: Assert and undefined behavior

2017-10-12 Thread John Burton via Digitalmars-d-learn
On Thursday, 12 October 2017 at 14:22:43 UTC, Timon Gehr wrote: On 11.10.2017 11:27, John Burton wrote: Yes, that's what it is saying. (The other answers, that say or try to imply that this is not true or true but not a bad thing, are wrong.) ... However, in practice, I think none of the

Re: Temporary objects as function parameters or when-is-this-shit-going-to-end?

2017-10-17 Thread John Burton via Digitalmars-d-learn
On Friday, 13 October 2017 at 10:35:56 UTC, Jack Applegame wrote: If you don't want to get the great PITA, never create temporary objects in function parameters. I recently spent a whole day digging through my reference counted containers library. But nasty bug was not there, but in the

Cross compile windows programs on linux

2018-08-24 Thread John Burton via Digitalmars-d-learn
Is in the subject. Are there any cross compilers that will run on a linux system but compile D code using Win32 into a windows .exe file, preferably 64 bit? I can find hints of cross compilers but not really seen anything packaged up?

Re: Cross compile windows programs on linux

2018-08-24 Thread John Burton via Digitalmars-d-learn
On Friday, 24 August 2018 at 15:26:30 UTC, kinke wrote: On Friday, 24 August 2018 at 13:10:40 UTC, John Burton wrote: Is in the subject. Are there any cross compilers that will run on a linux system but compile D code using Win32 into a windows .exe file, preferably 64 bit? I can find hints of

Garbage collected pointers?

2018-03-01 Thread John Burton via Digitalmars-d-learn
In the language spec here :- https://dlang.org/spec/garbage.html#pointers_and_gc It refers to a distinction between pointers to garbage collected memory and pointers that are not. In particular it says that with a non garbage collected pointer you can do anything that is legal in C but with

Re: Garbage collected pointers?

2018-03-01 Thread John Burton via Digitalmars-d-learn
On Thursday, 1 March 2018 at 12:20:08 UTC, Steven Schveighoffer wrote: On 3/1/18 7:05 AM, Gary Willoughby wrote: On Thursday, 1 March 2018 at 10:10:27 UTC, John Burton wrote: My question is how do I tell if a pointer is "garbage collected" or not? You could try `GC.addrOf()` or `GC.query()`

Is there an efficient byte buffer queue?

2018-10-08 Thread John Burton via Digitalmars-d-learn
My use case is sending data to a socket. One part of my program generates blocks of bytes, and the socket part tries to send them to the socket and then removes from the queue the number that got sent. I am currently using a byte[] and using concatenation and slicing to maintain the queue

When does GC run?

2018-10-16 Thread John Burton via Digitalmars-d-learn
Is there any documentation or information about the specifics of the garbage collector? The information I have found indicates that it runs to free memory when the system runs out of memory to allocate. But will this try to use all the system memory or some other amount before trying to

Re: Is there an efficient byte buffer queue?

2018-10-16 Thread John Burton via Digitalmars-d-learn
On Sunday, 14 October 2018 at 13:07:30 UTC, Heromyth wrote: On Monday, 8 October 2018 at 09:39:55 UTC, John Burton wrote: My use case is sending data to a socket. We have ported some containers from JAVA. ByteBuffer is a basic container interface and widely used in JAVA. See also:

DirectX bindings

2018-10-30 Thread John Burton via Digitalmars-d-learn
I want to do some graphics using direct3d11 on windows. There are some bindings that I used once before https://github.com/evilrat666/directx-d However they are marked as [discontinued] While I'm sure they will continue to work so wouldn't worry about using this I wonder if there are any other

Re: DirectX bindings

2018-10-30 Thread John Burton via Digitalmars-d-learn
On Tuesday, 30 October 2018 at 10:46:35 UTC, Mike Parker wrote: On Tuesday, 30 October 2018 at 10:30:48 UTC, John Burton wrote: I want to do some graphics using direct3d11 on windows. There are some bindings that I used once before https://github.com/evilrat666/directx-d However they are

Can I create static c callable library?

2018-09-25 Thread John Burton via Digitalmars-d-learn
I need to write a library to statically link into a c program. Can I write this library in D? Will I be able to use proper D abilities like gc? Obviously the public interface will need to be basic c callable functions... I 'main' is a c program will this work?

Re: Can I create static c callable library?

2018-09-26 Thread John Burton via Digitalmars-d-learn
On Tuesday, 25 September 2018 at 12:05:21 UTC, Jonathan M Davis wrote: [...] Thanks everyone. Is there any documentation anywhere that deals with calling D from C? I could find plenty the other way round. I think I'll give up on the idea though, and rewrite the whole thing in D :)

Re: Is there a nice syntax to achieve optional named parameters?

2019-01-16 Thread John Burton via Digitalmars-d-learn
On Wednesday, 16 January 2019 at 11:21:53 UTC, Dukc wrote: On Tuesday, 15 January 2019 at 11:14:54 UTC, John Burton wrote: This is ok, but I'm not so keen on separating the creation and construction like this. Is there a better way that's not ugly? You can make the constructor a template

Re: Is there a nice syntax to achieve optional named parameters?

2019-01-15 Thread John Burton via Digitalmars-d-learn
On Tuesday, 15 January 2019 at 11:26:50 UTC, rikki cattermole wrote: Longer term, you're better off with the builder. Thanks for your reply. But what is the builder? Creating windows is a very complex task that can balloon in scope. Well that was mostly just an example that I thought

Is there a nice syntax to achieve optional named parameters?

2019-01-15 Thread John Burton via Digitalmars-d-learn
As an example let's say I have a type 'Window' that represents a win32 window. I'd like to be able to construct an instance of the type with some optional parameters that default to some reasonable settings and create the underlying win32 window. I'd ideally like some syntax like this :-

Re: Is there a nice syntax to achieve optional named parameters?

2019-01-15 Thread John Burton via Digitalmars-d-learn
On Tuesday, 15 January 2019 at 12:15:41 UTC, rikki cattermole wrote: On 16/01/2019 1:05 AM, John Burton wrote: On Tuesday, 15 January 2019 at 11:26:50 UTC, rikki cattermole wrote: Longer term, you're better off with the builder. Thanks for your reply. But what is the builder?

Re: Is there a nice syntax to achieve optional named parameters?

2019-01-17 Thread John Burton via Digitalmars-d-learn
On Wednesday, 16 January 2019 at 14:59:01 UTC, Kagamin wrote:> On Tuesday, 15 January 2019 at 11:14:54 UTC, John Burton wrote: auto window = Window(title = "My Window", width = 1000, fullscreen = true); In this particular case I would make the constructor take 3 parameters - title, width and

Re: Is there a nice syntax to achieve optional named parameters?

2019-01-17 Thread John Burton via Digitalmars-d-learn
On Thursday, 17 January 2019 at 01:43:42 UTC, SrMordred wrote: On Tuesday, 15 January 2019 at 11:14:54 UTC, John Burton wrote: [...] Let me throw this idea here: struct Config { string title; int width; } struct Window { this(Config config) {

Re: Is there a nice syntax to achieve optional named parameters?

2019-01-21 Thread John Burton via Digitalmars-d-learn
On Monday, 21 January 2019 at 07:57:58 UTC, Simen Kjærås wrote: On Saturday, 19 January 2019 at 14:26:31 UTC, Zenw wrote: On Tuesday, 15 January 2019 at 11:14:54 UTC, John Burton wrote: [...] how about this auto With(string code,T)(T value) { with(value) { mixin(code ~";");

Re: Is there a nice syntax to achieve optional named parameters?

2019-01-18 Thread John Burton via Digitalmars-d-learn
On Thursday, 17 January 2019 at 01:43:42 UTC, SrMordred wrote: struct Config { string title; int width; } struct Window { this(Config config) It likely is a bad idea for a small struct like this but if it was much bigger would it makes sense to write this as :-

Re: DirectXMath alternative

2018-12-04 Thread John Burton via Digitalmars-d-learn
On Wednesday, 5 December 2018 at 01:57:53 UTC, evilrat wrote: On Tuesday, 4 December 2018 at 20:41:54 UTC, Guillaume Piolat wrote: On Tuesday, 4 December 2018 at 20:33:07 UTC, John Burton wrote: What is the best alternative for D, assuming there is anything? (I want vector, matrix math for use

Re: DirectXMath alternative

2018-12-05 Thread John Burton via Digitalmars-d-learn
On Wednesday, 5 December 2018 at 10:52:44 UTC, Guillaume Piolat wrote: On Wednesday, 5 December 2018 at 01:57:53 UTC, evilrat wrote: On Tuesday, 4 December 2018 at 20:41:54 UTC, Guillaume Piolat wrote: [...] I was using gl3n then switched to gfm math. Try gfm, IIRC it should work without

DirectXMath alternative

2018-12-04 Thread John Burton via Digitalmars-d-learn
There is a directx-d library which seems to work nicely for d3d11 but it doesn't include anything like DirectXMath.h presumably because it's all implemented as inline intrinsics and very visual c++ specific. What is the best alternative for D, assuming there is anything? (I want vector,

Is it safe in D to cast pointers to structures like this?

2020-01-14 Thread John Burton via Digitalmars-d-learn
After years of C++ I have become paranoid about any casting of pointers being undefined behavior due to aliasing so want to see if :- 1) This is safe to do in D. 2) If not is there anything I can do to make it safe. 3) If not, what is the best approach? I have a void* pointing to a block of

Re: How to get the pointer of "this" ?

2020-05-25 Thread John Burton via Digitalmars-d-learn
On Sunday, 24 May 2020 at 17:40:10 UTC, bauss wrote: On Sunday, 24 May 2020 at 17:05:16 UTC, Vinod K Chandran wrote: [...] I think your issue might be elsewhere because casting this should be fine and it should not complain about that in your given code. At least you should be able to

Re: How to get the pointer of "this" ?

2020-05-25 Thread John Burton via Digitalmars-d-learn
On Monday, 25 May 2020 at 16:39:30 UTC, Mike Parker wrote: On Monday, 25 May 2020 at 08:39:23 UTC, John Burton wrote: I believe that in D *this* is a reference to the object and not a pointer like in C++. So I think that writing might be what you need? No. A class reference is a pointer

Re: Contributing to D wiki

2020-07-30 Thread John Burton via Digitalmars-d-learn
On Monday, 27 July 2020 at 16:58:13 UTC, H. S. Teoh wrote: On Mon, Jul 27, 2020 at 11:39:32AM +, John Burton via Digitalmars-d-learn wrote: [...] I tried looking there for information and examples of getting glfw3 statically linked into my program using LDC and didn't really find anything

Static link of glfw3 library fails for me

2020-07-26 Thread John Burton via Digitalmars-d-learn
I'm trying to replicate a program I make in C++ using D. I am using the ldc2 compiler and want to *static* link in the glfw library. Following the docs I have an dub.sdl file that looks like the one below. The library I'm linking with is the vs2019 one from the GLFW zip file from their

Re: Static link of glfw3 library fails for me

2020-07-26 Thread John Burton via Digitalmars-d-learn
On Sunday, 26 July 2020 at 10:41:27 UTC, Mike Parker wrote: On Sunday, 26 July 2020 at 08:28:29 UTC, John Burton wrote: And I get the following errors from the link :- lld-link: error: undefined symbol: __GSHandlerCheck lld-link: error: undefined symbol: __security_check_cookie lld-link:

Re: Contributing to D wiki

2020-07-27 Thread John Burton via Digitalmars-d-learn
On Wednesday, 15 July 2020 at 22:18:47 UTC, H. S. Teoh wrote: On Wed, Jul 15, 2020 at 09:27:22PM +, tastyminerals via Digitalmars-d-learn wrote: [...] D wiki is badly outdated. This is not a fact but a gut feeling after reading through some of its pages. I was wondering who's owning it

Re: Static link of glfw3 library fails for me

2020-07-27 Thread John Burton via Digitalmars-d-learn
On Monday, 27 July 2020 at 08:53:25 UTC, Mike Parker wrote: On Monday, 27 July 2020 at 07:30:42 UTC, John Burton wrote: For reference I got this to work by doing the following :- 1) Installed visual studio build tools. I could not get this to work at all with the linker etc that comes with

Re: Static link of glfw3 library fails for me

2020-07-27 Thread John Burton via Digitalmars-d-learn
On Sunday, 26 July 2020 at 12:24:06 UTC, John Burton wrote: On Sunday, 26 July 2020 at 10:41:27 UTC, Mike Parker wrote: On Sunday, 26 July 2020 at 08:28:29 UTC, John Burton wrote: And I get the following errors from the link :- lld-link: error: undefined symbol: __GSHandlerCheck lld-link:

Are there any containers that go with allocators?

2021-02-09 Thread John Burton via Digitalmars-d-learn
Normally I'm happy with the GC containers in D, they work well and suit my use. I have a few uses that would benefit from allocation in memory arenas or local stack based allocation. Looks like std.experimental has allocators for those use cases. But I can't find any containers to make use

Re: Are there any containers that go with allocators?

2021-02-09 Thread John Burton via Digitalmars-d-learn
On Tuesday, 9 February 2021 at 12:23:52 UTC, rikki cattermole wrote: https://github.com/dlang-community/containers It uses the older design for allocators (dependency). Looks good, thank you