Thread overview
Associate information with a pointer address.
Sep 29
BoQsc
Sep 29
BoQsc
Sep 29
BoQsc
Sep 29
BoQsc
Sep 29
bachmeier
Oct 01
BoQsc
Oct 01
bachmeier
September 29

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

September 29

Bonus: Store hexadecimal literals in associative array of 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

September 29

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

September 29

After being very happy about associative arrays of D Language, I encountered that they are not @nogcfriendly. 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();
}

September 29

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 @nogcfriendly. 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.)

October 01

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

Output

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.

October 01
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.