digitalmars.D.learn - pointer array?
- seany (5/5) Jul 30 2014 In Ali's excllent book, somehow one thing has escaped my
- Justin Whear (2/8) Jul 30 2014 You can use an array of void*: http://dpaste.dzfl.pl/0d3ee1723192
- Leandro Motta Barros via Digitalmars-d-learn (16/21) Jul 30 2014 Justin's answers seems correct to me, and I don't know anything about y...
- seany (16/16) Jul 30 2014 Actually, I am writing a climate simulation software, and I would
- Justin Whear (3/9) Jul 30 2014 Can you post the signatures of some of the C functions you're trying to
- seany (2/5) Jul 30 2014 let us take a simple function :
- Leandro Motta Barros via Digitalmars-d-learn (10/15) Jul 30 2014 Can't you call it directly?
- Daniel Kozak via Digitalmars-d-learn (9/15) Jul 30 2014 V Wed, 30 Jul 2014 14:33:51 +0000
- FreeSlave (5/24) Jul 31 2014 Don't use long in this case, use size_t and ptrdiff_t. They are
In Ali's excllent book, somehow one thing has escaped my attention, and that it the mentioning of pointer arrays. Can pointers of any type of pointed variable be inserted in an int array? Using to!(int) perhaps? If not directly, then what else would achieve the same effect?
Jul 30 2014
On Wed, 30 Jul 2014 14:33:51 +0000, seany wrote:In Ali's excllent book, somehow one thing has escaped my attention, and that it the mentioning of pointer arrays. Can pointers of any type of pointed variable be inserted in an int array? Using to!(int) perhaps? If not directly, then what else would achieve the same effect?You can use an array of void*: http://dpaste.dzfl.pl/0d3ee1723192
Jul 30 2014
Justin's answers seems correct to me, and I don't know anything about your specific use case, but I cannot resist to add: Think twice before doing this kind of things. I know that sometimes this is necessary or handy, but one of the great things about D is that it provides so many higher-level abstractions that we should feel ashamed to not use them. So, yes, an array of void* will work in D, as will many of the classic lower-level tricks used in, say, C. But when using them, the compiler will not be able to help you much finding errors and such. As rule I'd say that, if you can (and we usually can), try using something higher level. (In your case, perhaps an array of objects of some base class, or implementing a certain interface? Or some more radical redesign?) Cheers, LMB On Wed, Jul 30, 2014 at 11:33 AM, seany via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:In Ali's excllent book, somehow one thing has escaped my attention, and that it the mentioning of pointer arrays. Can pointers of any type of pointed variable be inserted in an int array? Using to!(int) perhaps? If not directly, then what else would achieve the same effect?
Jul 30 2014
Actually, I am writing a climate simulation software, and I would love to use D for parts of it. However some code is in C, legacy code, and for speed resons. So in some cases, I would like to send a bunch of variables , ints, dubles and floats to an external C function. The thing is, I do not always know the number of variables, so my idea was to make an array of pointers (call it stack), and then send the pointer to the stack itself to the C function. Because different stack configuration will call different C functions, and the function already knowes what to expect in this stack, I thought this was a good construction. If I am to create a set of base class, and array of it, and so much, it looks very complicated.... Of course, for more complicated objects, e.g., Groups, and other algebraic objects, I am using DLua. Any suggestions.
Jul 30 2014
On Wed, 30 Jul 2014 15:44:14 +0000, seany wrote:However some code is in C, legacy code, and for speed resons. So in some cases, I would like to send a bunch of variables , ints, dubles and floats to an external C function. The thing is, I do not always know the number of variables, so my idea was to make an array of pointers (call it stack), and then send the pointer to the stack itself to the C function.Can you post the signatures of some of the C functions you're trying to interface with?
Jul 30 2014
Can you post the signatures of some of the C functions you're trying to interface with?let us take a simple function : int add (int a, int b)
Jul 30 2014
Can't you call it directly? extern(C) { int add (int a, int b)'; } // ... auto ret = add(123, 456); LMB On Wed, Jul 30, 2014 at 2:55 PM, seany via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:Can you post the signatures of some of the C functions you're trying to interface with?let us take a simple function : int add (int a, int b)
Jul 30 2014
V Wed, 30 Jul 2014 14:33:51 +0000 seany via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> napsáno:In Ali's excllent book, somehow one thing has escaped my attention, and that it the mentioning of pointer arrays. Can pointers of any type of pointed variable be inserted in an int array? Using to!(int) perhaps? If not directly, then what else would achieve the same effect?It depends on pointer size, for eg, on 64bit system with 64bit pointers something like this should works: long[] arr = (cast(long*)pointersArray.ptr)[0 .. pointersArray.length]; or long[] arr = cast(long[])pointersArray; // but I am not sure if this is OK
Jul 30 2014
On Wednesday, 30 July 2014 at 20:51:25 UTC, Daniel Kozak via Digitalmars-d-learn wrote:V Wed, 30 Jul 2014 14:33:51 +0000 seany via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> napsáno:Don't use long in this case, use size_t and ptrdiff_t. They are required to be integer types with size of pointer (size_t is unsigned, ptrdiff_t is signed)In Ali's excllent book, somehow one thing has escaped my attention, and that it the mentioning of pointer arrays. Can pointers of any type of pointed variable be inserted in an int array? Using to!(int) perhaps? If not directly, then what else would achieve the same effect?It depends on pointer size, for eg, on 64bit system with 64bit pointers something like this should works: long[] arr = (cast(long*)pointersArray.ptr)[0 .. pointersArray.length]; or long[] arr = cast(long[])pointersArray; // but I am not sure if this is OK
Jul 31 2014