Re: basic question about adresses and values in structs

2014-09-04 Thread nikki via Digitalmars-d-learn
thanks! just what I needed, with some stumbling I managed to get 
everything working as intended: using a pointer variable to save 
an adres of a function, then dereferencing to use it.


Now I am wondering when to use the ** ?

for example I found this function over at 
https://github.com/d-gamedev-team/gfm/blob/master/core/gfm/core/memory.d


void* storeRawPointerAndReturnAligned(void* raw, size_t 
alignment) nothrow

{
enum size_t pointerSize = size_t.sizeof;
char* start = cast(char*)raw + pointerSize;
void* aligned = nextAlignedPointer(start, alignment);
void** rawLocation = cast(void**)(cast(char*)aligned - 
pointerSize);

*rawLocation = raw;
return aligned;
}

it's a little over my head yet..


Re: basic question about adresses and values in structs

2014-09-04 Thread evilrat via Digitalmars-d-learn

On Thursday, 4 September 2014 at 09:54:57 UTC, nikki wrote:
thanks! just what I needed, with some stumbling I managed to 
get everything working as intended: using a pointer variable to 
save an adres of a function, then dereferencing to use it.


Now I am wondering when to use the ** ?

for example I found this function over at 
https://github.com/d-gamedev-team/gfm/blob/master/core/gfm/core/memory.d


void* storeRawPointerAndReturnAligned(void* raw, size_t 
alignment) nothrow

{
enum size_t pointerSize = size_t.sizeof;
char* start = cast(char*)raw + pointerSize;
void* aligned = nextAlignedPointer(start, alignment);
void** rawLocation = cast(void**)(cast(char*)aligned - 
pointerSize);

*rawLocation = raw;
return aligned;
}

it's a little over my head yet..


void** (double ptr) is a pointer to array of pointers(just 
imagine a crossword where each horizontal letter is part of 
vertical word) there is little reason to use them in D, mostly to 
C/C++ interfacing


Re: basic question about adresses and values in structs

2014-09-04 Thread Ali Çehreli via Digitalmars-d-learn

On 09/04/2014 02:54 AM, nikki wrote:

 a pointer variable to save an adres of a function, then dereferencing 
to use

 it.

If possible, even in C, I would recommend using a 'function pointer' for 
that. However, there are cases where the signature of the function 
should be unknown to the code that is storing it so a void* is used. 
(Note that, as discussed on these forums in the past, void* has always 
been intended to be a data pointer. The fact that it works for function 
pointers is something we get as lucky accidents, which will most 
probably always supported by compilers and CPUs.)


Here is how D does function pointers:

  http://dlang.org/expression.html#FunctionLiteral

And a chapter that expands on those:

  http://ddili.org/ders/d.en/lambda.html

 Now I am wondering when to use the ** ?

The simple answer is when dealing with the address of a type that is 
'void*' itself. In other words, there is nothing special about **: It 
appears as the type that is a pointer to a pointer. Inserting spaces:


int * p;  // A pointer to an int
void* * q;// A pointer to a void*

// (untested)
static assert (is (typeof(*p) == int));
static assert (is (typeof(*q) == void*));

int i;
*p = i;// Can store an int

void* v;
*q = v;// Can store a void*

Ali



Re: basic question about adresses and values in structs

2014-09-04 Thread nikki via Digitalmars-d-learn

On Thursday, 4 September 2014 at 14:00:14 UTC, Ali Çehreli wrote:

On 09/04/2014 02:54 AM, nikki wrote:

 a pointer variable to save an adres of a function, then
dereferencing to use
 it.

If possible, even in C, I would recommend using a 'function 
pointer' for that. However, there are cases where the signature 
of the function should be unknown to the code that is storing 
it so a void* is used. (Note that, as discussed on these forums 
in the past, void* has always been intended to be a data 
pointer. The fact that it works for function pointers is 
something we get as lucky accidents, which will most probably 
always supported by compilers and CPUs.)

...
Ali


Ah right I was so busy with these * and  ;)

Thanks!


basic question about adresses and values in structs

2014-09-01 Thread nikki via Digitalmars-d-learn
so I am still very new to structs and  and * adress and pointer 
stuff, I have this basic code :


struct S {
int value = 0;
}

void func(S thing){
writeln(thing); //BFC52B44
thing.value = 100;
}

S guy = {value:200};
writeln(guy); //BFC52CCC
func(guy);
writeln(guy.value);// this prints 200, because the adress was 
not the same


I think I see whats going on but I don't know how to fix it?


Re: basic question about adresses and values in structs

2014-09-01 Thread nikki via Digitalmars-d-learn

sorry could have quicker just googled it thanks!


Re: basic question about adresses and values in structs

2014-09-01 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 1 September 2014 at 18:08:48 UTC, nikki wrote:
so I am still very new to structs and  and * adress and 
pointer stuff, I have this basic code :


struct S {
int value = 0;
}

void func(S thing){
writeln(thing); //BFC52B44
thing.value = 100;
}

S guy = {value:200};
writeln(guy); //BFC52CCC
func(guy);
writeln(guy.value);// this prints 200, because the adress 
was not the same


I think I see whats going on but I don't know how to fix it?


void func(ref S thing){
writeln(thing);
thing.value = 100;
}

The ref keyword passes the variable into the function by 
reference, so that it is not copied.


Re: basic question about adresses and values in structs

2014-09-01 Thread nikki via Digitalmars-d-learn

ah so much cleaner then the mess I was almost into ;)

thanks


Re: basic question about adresses and values in structs

2014-09-01 Thread Ali Çehreli via Digitalmars-d-learn

On 09/01/2014 11:23 AM, nikki wrote:

ah so much cleaner then the mess I was almost into ;)

thanks


In case they are useful to you or somebody else, the following chapters 
are relevant.


Value Types and Reference Types:

  http://ddili.org/ders/d.en/value_vs_reference.html

Pointers:

  http://ddili.org/ders/d.en/pointers.html

Ali