digitalmars.D - example of pointer usefulness in D
- edn (3/3) Oct 21 2014 Could someone provide me with examples showing the usefulness of
- w0rp (4/7) Oct 21 2014 You can use C libraries in D, and pointers will surely come into
- Jeremy DeHaan (3/10) Oct 21 2014 Can you do the same with classes if you wanted to avoid a GC
- Brad Anderson (8/19) Oct 21 2014 You can create a class in any chunk of memory (say memory
- monarch_dodra (11/14) Oct 21 2014 The only difference between C/C++ and D is that C uses pointers
- Paulo Pinto (5/13) Oct 21 2014 Actually it goes back to Algol and all languages in the Pascal family
- Freddy (2/5) Oct 21 2014 https://en.wikipedia.org/wiki/XOR_linked_list
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (2/7) Oct 21 2014 Uhm. That won't work with the GC…
- ketmar via Digitalmars-d (4/12) Oct 21 2014 nope, it's vice versa: it's GC who can't work with this. ;-)
- Chris Williams (10/18) Oct 21 2014 It won't work with the GC, but one could still safely implement
- ketmar via Digitalmars-d (3/9) Oct 21 2014 don't try this at home!
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (17/19) Oct 21 2014 A pointer is the only sensible option for the following:
- bearophile (9/15) Oct 21 2014 Regarding this example, this works:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/22) Oct 21 2014 Yes but that doesn't scale when we need to use it again:
Could someone provide me with examples showing the usefulness of pointers in the D language? They don't seem to be used as much as in C and C++.
Oct 21 2014
On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:Could someone provide me with examples showing the usefulness of pointers in the D language? They don't seem to be used as much as in C and C++.You can use C libraries in D, and pointers will surely come into regular use there. You could also heap allocate structs and end up with pointers to structs.
Oct 21 2014
On Tuesday, 21 October 2014 at 12:40:57 UTC, w0rp wrote:On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:Can you do the same with classes if you wanted to avoid a GC allocation? Just wondering.Could someone provide me with examples showing the usefulness of pointers in the D language? They don't seem to be used as much as in C and C++.You can use C libraries in D, and pointers will surely come into regular use there. You could also heap allocate structs and end up with pointers to structs.
Oct 21 2014
On Tuesday, 21 October 2014 at 16:55:09 UTC, Jeremy DeHaan wrote:On Tuesday, 21 October 2014 at 12:40:57 UTC, w0rp wrote:You can create a class in any chunk of memory (say memory returned through malloc for instance) using std.conv.emplace: http://dlang.org/library/std/conv/emplace.html std.typecons.Unique and std.typecons.Scoped are better options if you are going for a scope destroyed class or a class on the stack as they take care of the details for you. std.typecons.RefCounted would be a great option but it does not yet work on classes.On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:Can you do the same with classes if you wanted to avoid a GC allocation? Just wondering.Could someone provide me with examples showing the usefulness of pointers in the D language? They don't seem to be used as much as in C and C++.You can use C libraries in D, and pointers will surely come into regular use there. You could also heap allocate structs and end up with pointers to structs.
Oct 21 2014
On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:Could someone provide me with examples showing the usefulness of pointers in the D language? They don't seem to be used as much as in C and C++.The only difference between C/C++ and D is that C uses pointers for both "pointer to object" and "pointer to array", whereas D has a "slice" object. C++ introduced "pass-by-ref" (also exists in D), which has tended to reduce the (visible) use. Furthermore, the more modern the language, the more the "raw" pointers tend to be encapsulated in structures that manage them for you. So while you don't "see" them quite as much, they are still there, and fill exactly the same role.
Oct 21 2014
Am 21.10.2014 um 14:47 schrieb monarch_dodra:On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:Actually it goes back to Algol and all languages in the Pascal family support it. -- PauloCould someone provide me with examples showing the usefulness of pointers in the D language? They don't seem to be used as much as in C and C++.The only difference between C/C++ and D is that C uses pointers for both "pointer to object" and "pointer to array", whereas D has a "slice" object. C++ introduced "pass-by-ref" (also exists in D), which has tended to reduce the (visible) use.
Oct 21 2014
On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:Could someone provide me with examples showing the usefulness of pointers in the D language? They don't seem to be used as much as in C and C++.https://en.wikipedia.org/wiki/XOR_linked_list
Oct 21 2014
On Tuesday, 21 October 2014 at 19:33:14 UTC, Freddy wrote:On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:Uhm. That won't work with the GC…Could someone provide me with examples showing the usefulness of pointers in the D language? They don't seem to be used as much as in C and C++.https://en.wikipedia.org/wiki/XOR_linked_list
Oct 21 2014
On Tue, 21 Oct 2014 19:36:49 +0000 via Digitalmars-d <digitalmars-d puremagic.com> wrote:On Tuesday, 21 October 2014 at 19:33:14 UTC, Freddy wrote:nope, it's vice versa: it's GC who can't work with this. ;-) yet you can use D without GC.On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:Uhm. That won't work with the GC=E2=80=A6Could someone provide me with examples showing the usefulness=20 of pointers in the D language? They don't seem to be used as=20 much as in C and C++.https://en.wikipedia.org/wiki/XOR_linked_list
Oct 21 2014
On Tuesday, 21 October 2014 at 19:36:50 UTC, Ola Fosheim Grøstad wrote:On Tuesday, 21 October 2014 at 19:33:14 UTC, Freddy wrote:It won't work with the GC, but one could still safely implement an XOR linked list in D, if they used malloc/free manually, so that the collector was never aware of the existence of those allocations. As to the OP, at the moment, D doesn't support structs as a reference type so if you want a container that stores references to structs rather than copies, the container will probably use pointers.On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:Uhm. That won't work with the GC…Could someone provide me with examples showing the usefulness of pointers in the D language? They don't seem to be used as much as in C and C++.https://en.wikipedia.org/wiki/XOR_linked_list
Oct 21 2014
On Tue, 21 Oct 2014 19:33:13 +0000 Freddy via Digitalmars-d <digitalmars-d puremagic.com> wrote:On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:don't try this at home!Could someone provide me with examples showing the usefulness=20 of pointers in the D language? They don't seem to be used as=20 much as in C and C++.=20 https://en.wikipedia.org/wiki/XOR_linked_list
Oct 21 2014
On 10/21/2014 05:22 AM, edn wrote:> Could someone provide me with examples showing the usefulness ofpointers in the D language? They don't seem to be used as much as in C and C++.A pointer is the only sensible option for the following: - References to any local data because 'ref' is only for parameters and return types. int a; int b; int* r = (condition ? &a : &b); // r must be a pointer *r = 42; - Any link in a linked data structure like linked lists and trees: struct L { L* next; // must be a pointer } One can use classes and slices for references as well but they are more expensive. Ali
Oct 21 2014
Ali Çehreli:- References to any local data because 'ref' is only for parameters and return types. int a; int b; int* r = (condition ? &a : &b); // r must be a pointer *r = 42;Regarding this example, this works: void main() { int a, b; bool condition; (condition ? a : b) = 42; } Bye, bearophile
Oct 21 2014
On 10/21/2014 01:15 PM, bearophile wrote:Ali Çehreli:Yes but that doesn't scale when we need to use it again: (condition ? a : b) = 42; foo(condition ? a : b); // ... Ali- References to any local data because 'ref' is only for parameters and return types. int a; int b; int* r = (condition ? &a : &b); // r must be a pointer *r = 42;Regarding this example, this works: void main() { int a, b; bool condition; (condition ? a : b) = 42; } Bye, bearophile
Oct 21 2014