Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 21, 2014 example of pointer usefulness in D | ||||
---|---|---|---|---|
| ||||
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++. |
October 21, 2014 Re: example of pointer usefulness in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to edn | 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.
|
October 21, 2014 Re: example of pointer usefulness in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to edn | 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.
|
October 21, 2014 Re: example of pointer usefulness in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to w0rp | 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.
|
October 21, 2014 Re: example of pointer usefulness in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeremy DeHaan | 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. |
October 21, 2014 Re: example of pointer usefulness in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to edn | 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 |
October 21, 2014 Re: example of pointer usefulness in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Freddy | 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…
|
October 21, 2014 Re: example of pointer usefulness in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Freddy Attachments: | 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 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
don't try this at home!
|
October 21, 2014 Re: example of pointer usefulness in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ola Fosheim Grøstad Attachments: | 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 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…
nope, it's vice versa: it's GC who can't work with this. ;-)
yet you can use D without GC.
|
October 21, 2014 Re: example of pointer usefulness in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ola Fosheim Grøstad | 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.
|
Copyright © 1999-2021 by the D Language Foundation