www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - example of pointer usefulness in D

reply "edn" <edn yahoo.com> writes:
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
next sibling parent reply "w0rp" <devw0rp gmail.com> writes:
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
parent reply "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
On Tuesday, 21 October 2014 at 12:40:57 UTC, w0rp wrote:
 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.
Can you do the same with classes if you wanted to avoid a GC allocation? Just wondering.
Oct 21 2014
parent "Brad Anderson" <eco gnuk.net> writes:
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:
 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.
Can you do the same with classes if you wanted to avoid a GC allocation? Just wondering.
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.
Oct 21 2014
prev sibling next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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
parent Paulo Pinto <pjmlp progtools.org> writes:
Am 21.10.2014 um 14:47 schrieb monarch_dodra:
 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.
Actually it goes back to Algol and all languages in the Pascal family support it. -- Paulo
Oct 21 2014
prev sibling next sibling parent reply "Freddy" <Hexagonalstar64 gmail.com> writes:
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
next sibling parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Tuesday, 21 October 2014 at 19:33:14 UTC, Freddy wrote:
 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
Uhm. That won't work with the GC…
Oct 21 2014
next sibling parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
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:
 On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:
 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++.
https://en.wikipedia.org/wiki/XOR_linked_list
Uhm. That won't work with the GC=E2=80=A6
nope, it's vice versa: it's GC who can't work with this. ;-) yet you can use D without GC.
Oct 21 2014
prev sibling parent "Chris Williams" <yoreanon-chrisw yahoo.co.jp> writes:
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:
 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
Uhm. That won't work with the GC…
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.
Oct 21 2014
prev sibling parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
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:
 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
don't try this at home!
Oct 21 2014
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/21/2014 05:22 AM, 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++.
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
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/21/2014 01:15 PM, bearophile wrote:
 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
Yes but that doesn't scale when we need to use it again: (condition ? a : b) = 42; foo(condition ? a : b); // ... Ali
Oct 21 2014