Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
October 28, 2012 What is the proper way to handle pointers in variable arguments list? | ||||
---|---|---|---|---|
| ||||
The following fails because the compiler assumes I am trying to dereference non-pointer variables. Can this be done? void main() { int i; int* pi; double d; double* pd; char c; char* pc; scan(i, pi, d, pd, c, pc); } void scan(A...)(ref A data) { import std.traits; foreach (element; data) { if(isPointer!(typeof(element)) && isIntegral!(typeof(*element))) { *element = 10; } } } Thanks |
October 28, 2012 Re: What is the proper way to handle pointers in variable arguments list? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyro[17] | On 29-Oct-12 00:36, Tyro[17] wrote: > The following fails because the compiler assumes I am trying to > dereference non-pointer variables. Can this be done? > > void main() > { > int i; > int* pi; > double d; > double* pd; > char c; > char* pc; > > scan(i, pi, d, pd, c, pc); > } > > void scan(A...)(ref A data) > { > import std.traits; > foreach (element; data) { > if(isPointer!(typeof(element)) && isIntegral!(typeof(*element))) { > *element = 10; > } > } > } > > Thanks Well, first things first: if ---> static if -- Dmitry Olshansky |
October 28, 2012 Re: What is the proper way to handle pointers in variable arguments list? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | On 10/28/12 4:44 PM, Dmitry Olshansky wrote:
> On 29-Oct-12 00:36, Tyro[17] wrote:
>> The following fails because the compiler assumes I am trying to
>> dereference non-pointer variables. Can this be done?
>>
>> void main()
>> {
>> int i;
>> int* pi;
>> double d;
>> double* pd;
>> char c;
>> char* pc;
>>
>> scan(i, pi, d, pd, c, pc);
>> }
>>
>> void scan(A...)(ref A data)
>> {
>> import std.traits;
>> foreach (element; data) {
>> if(isPointer!(typeof(element)) &&
>> isIntegral!(typeof(*element))) {
>> *element = 10;
>> }
>> }
>> }
>>
>> Thanks
>
> Well, first things first:
> if ---> static if
>
Changing it to static allows compilation, however I get the following runtime error:
"Segmentation fault: 11"
This happens whether I try to read from *element or modify it.
|
October 28, 2012 Re: What is the proper way to handle pointers in variable arguments list? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyro[17] | On 2012-08-28 22:10, Tyro[17] <nospam@home.com> wrote: > On 10/28/12 4:44 PM, Dmitry Olshansky wrote: >> On 29-Oct-12 00:36, Tyro[17] wrote: >>> The following fails because the compiler assumes I am trying to >>> dereference non-pointer variables. Can this be done? >>> >>> void main() >>> { >>> int i; >>> int* pi; >>> double d; >>> double* pd; >>> char c; >>> char* pc; >>> >>> scan(i, pi, d, pd, c, pc); >>> } >>> >>> void scan(A...)(ref A data) >>> { >>> import std.traits; >>> foreach (element; data) { >>> if(isPointer!(typeof(element)) && >>> isIntegral!(typeof(*element))) { >>> *element = 10; >>> } >>> } >>> } >>> >>> Thanks >> >> Well, first things first: >> if ---> static if >> > > Changing it to static allows compilation, however I get the following runtime error: > > "Segmentation fault: 11" > > This happens whether I try to read from *element or modify it. I assume you've actually initialized the pointers to something? Given the above code, all the pointers point to null, and a segfault would be a reasonable reaction. -- Simen |
October 28, 2012 Re: What is the proper way to handle pointers in variable arguments list? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | On 10/28/12 5:16 PM, Simen Kjaeraas wrote:
> On 2012-08-28 22:10, Tyro[17] <nospam@home.com> wrote:
>
>> On 10/28/12 4:44 PM, Dmitry Olshansky wrote:
>>> On 29-Oct-12 00:36, Tyro[17] wrote:
>>>> The following fails because the compiler assumes I am trying to
>>>> dereference non-pointer variables. Can this be done?
>>>>
>>>> void main()
>>>> {
>>>> int i;
>>>> int* pi;
>>>> double d;
>>>> double* pd;
>>>> char c;
>>>> char* pc;
>>>>
>>>> scan(i, pi, d, pd, c, pc);
>>>> }
>>>>
>>>> void scan(A...)(ref A data)
>>>> {
>>>> import std.traits;
>>>> foreach (element; data) {
>>>> if(isPointer!(typeof(element)) &&
>>>> isIntegral!(typeof(*element))) {
>>>> *element = 10;
>>>> }
>>>> }
>>>> }
>>>>
>>>> Thanks
>>>
>>> Well, first things first:
>>> if ---> static if
>>>
>>
>> Changing it to static allows compilation, however I get the following
>> runtime error:
>>
>> "Segmentation fault: 11"
>>
>> This happens whether I try to read from *element or modify it.
>
> I assume you've actually initialized the pointers to something?
>
> Given the above code, all the pointers point to null, and a segfault
> would be a reasonable reaction.
>
Wow, so obvious yet so obviously overlooked... *banging my head on the monitor*
Thanks
|
Copyright © 1999-2021 by the D Language Foundation