www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associate information with a pointer address.

reply BoQsc <vaidas.boqsc gmail.com> writes:
For some reason I thought this was something I wanted to achieve 
and share.

```
import std;
void main()
{
     string variable;
     void * pointeraddress = &variable;
     string[void *] associative;

     associative[pointeraddress] = "someinformation";

     writeln("Hello D ", pointeraddress);
	writeln("Hello D ", associative[pointeraddress]);
}
```

https://run.dlang.io/is/WkQ50H

![img](https://i.imgur.com/pEJT9GR.png)
Sep 29 2023
parent reply BoQsc <vaidas.boqsc gmail.com> writes:

pointer addresses.

```
import std;
void main()
{
     string[void *] associative;

     // Store Hexadecimal as pointer address in associative array
     associative[cast(void *)0x7FFCD332CD60] = "someinformation";

     // Store Hexadecimal literal as pointer address in a variable.
     // void * customPointer = cast(void *)0x7FFCD332CD60;

	writeln("Hello D ", associative[cast(void *)0x7FFCD332CD60]);
     writeln("Hello D ", associative);
}

```
https://run.dlang.io/is/LamPne
![](https://i.imgur.com/Sb9EJ4m.png)
Sep 29 2023
parent reply BoQsc <vaidas.boqsc gmail.com> writes:
Out of scope access to a variable using stored pointer address, 
demonstration.

```
import std;

void outofcontext()
{
     writeln("Hello D ", associative);
     foreach (pointeraddress, information; associative)
     {
         writeln(*cast(string*)pointeraddress);
     }

}

static string[void* ] associative;

void main()
{
     writeln("Hello D ", associative);

     string variable = "hi";
     void* pointeraddress = &variable;
     associative[pointeraddress] = "someinformation";

     outofcontext();
}
```
https://run.dlang.io/is/QVgL1J
![](https://i.imgur.com/DZqXtj4.png)
Sep 29 2023
parent reply BoQsc <vaidas.boqsc gmail.com> writes:
After being very happy about associative arrays of D Language, I 
encountered that they are not ` nogc`friendly. Unsure if I should 
wait for D language to support it, or do I need to rethink 
everything.

Error
```
onlineapp.d(20): Error: assigning an associative array element in 
` nogc` function `D main` may cause a GC allocation
```

Source Code
```
import std;

void outofcontext()  nogc  system
{
     printf("Hello D ", associative);
     foreach (pointeraddress, information; associative){
		
        printf("%i", *cast(int *)pointeraddress);
     }

}

static string[void*] associative;

void main()  nogc  system
{
     printf("Hello D ", associative);

     int variable = 6;
     associative[&variable] = "someinformation";

     outofcontext();
}

```
Sep 29 2023
parent reply bachmeier <no spam.net> writes:
On Friday, 29 September 2023 at 14:31:54 UTC, BoQsc wrote:
 After being very happy about associative arrays of D Language, 
 I encountered that they are not ` nogc`friendly. Unsure if I 
 should wait for D language to support it, or do I need to 
 rethink everything.
You can work with AA's inside nogc functions just fine as long as you don't do something that would cause a GC allocation. Consider this program: ``` import std; nogc double getValue(double[string] aa, string key) { return aa[key]; } void main() { double[string] aa; aa["one"] = 1.0; aa["two"] = 2.0; writeln(aa.getValue("two")); } ``` I'm not sure why you want to add elements inside a nogc function - hard to say without knowing your use case. When I create an AA with many elements, I disable the GC, add a bunch of elements, and then turn it back on. Once the AA has been created, I can send it to nogc functions for processing. I have a hard time believing there would be meaningful performance benefits doing anything further. (Though I can only speak to my experience, which of course does not include all applications.)
Sep 29 2023
parent reply BoQsc <vaidas.boqsc gmail.com> writes:
The package dependency `emsi_containers` that can be found in
  https://code.dlang.org/packages/emsi_containers might be a 
viable way to resolve the problem.



```
/+dub.sdl:
dependency "emsi_containers" version="~>0.7"
+/
import std;
void main(string[] args)  nogc
{
     import containers;
     DynamicArray!int arr;
     arr ~= 1;
     arr ~= 3;
     foreach (e; arr)
         printf("%i",e);
}
```

https://run.dlang.io/is/zD2zKg


```
13
```

I keep on wondering if something like this would be applicable to 
be in the standard library of D Language in the future.

`DynamicArray!int arr;`
And as always, I find it frustrating to read a source code that 
includes `!` as template instatiation without seemingly obvious 
alternative way of handling it.
Oct 01 2023
parent bachmeier <no spam.net> writes:
On Sunday, 1 October 2023 at 09:41:39 UTC, BoQsc wrote:
 The package dependency `emsi_containers` that can be found in
  https://code.dlang.org/packages/emsi_containers might be a 
 viable way to resolve the problem.



 ```
 /+dub.sdl:
 dependency "emsi_containers" version="~>0.7"
 +/
 import std;
 void main(string[] args)  nogc
 {
     import containers;
     DynamicArray!int arr;
     arr ~= 1;
     arr ~= 3;
     foreach (e; arr)
         printf("%i",e);
 }
 ```
The HashMap struct matches the associative array you were talking about earlier. It's been a long time since I looked at it, so I don't know if it's a complete replacement for the built-in associative array.
Oct 01 2023