Re: is the array literal in a loop stack or heap allocated?

2023-10-10 Thread Imperatorn via Digitalmars-d-learn

On Wednesday, 11 October 2023 at 02:54:53 UTC, mw wrote:

Hi,

I want to confirm: in the following loop, is the array literal 
`a` vs. `b` stack or heap allocated? and how many times?


void main() {

int[2] a;
int[] b;

int i;
While(++i <=100) {

  a = [i, i+1];  // array literal
  b = [i, i+1];

}

}


Thanks.


profile=gc


Re: is the array literal in a loop stack or heap allocated?

2023-10-10 Thread mw via Digitalmars-d-learn

On Wednesday, 11 October 2023 at 03:15:30 UTC, H. S. Teoh wrote:
On Wed, Oct 11, 2023 at 02:54:53AM +, mw via 
Digitalmars-d-learn wrote:

Hi,

I want to confirm: in the following loop, is the array literal 
`a` vs. `b` stack or heap allocated? and how many times?


void main() {

int[2] a;


This is stack-allocated. Once per call to the function.



int[] b;


This is an empty slice. It can refer to either stack or heap 
memory, depending on what's assigned to it.




int i;
While(++i <=100) {

  a = [i, i+1];  // array literal


`a` is overwritten in-place once per loop.


How about the temporary array literal on the right hand side? 
It's stack / heap allocated? Or it's not in the language 
specification, but up to the (optimizing) compiler to decide?





  b = [i, i+1];

[...]

A new array consisting of 2 elements is allocated, once per 
loop, and assigned to b each time. Any arrays from previous 
iterations will be collected by the GC eventually.



T





Re: is the array literal in a loop stack or heap allocated?

2023-10-10 Thread ryuukk_ via Digitalmars-d-learn

On Wednesday, 11 October 2023 at 02:54:53 UTC, mw wrote:

Hi,

I want to confirm: in the following loop, is the array literal 
`a` vs. `b` stack or heap allocated? and how many times?


void main() {

int[2] a;
int[] b;

int i;
While(++i <=100) {

  a = [i, i+1];  // array literal
  b = [i, i+1];

}

}


Thanks.



a is a static array, therefore it won't allocate any, it's a 
memcpy


b will be heap allocated, and it'll do an allocate at each 
iteration



```D
void test_b()
{
int[] a;
int i;
while (++i <= 100)
{
a = [i, i + 1];
printf("%p\n", a.ptr);
}
}
```

You can run this, and it'll print a different address each time


If you add `[]` it'll do range based copy (memcpy), but since the 
array is not initialized, it has a length of 0, so you only need 
to allocate once (either with GC or with malloc)


```D
void test_b()
{
int[] a;
int i;

a.length = 2; // initialize the heap allocated array here

// or with malloc:
// auto ptr = malloc(int.sizeof * 2);
// a = cast(int[]) ptr[0 .. int.sizeof * 2];

while (++i <= 100)
{
a[] = [i, i + 1];
printf("%p\n", a.ptr);
}
}
```

Otherwise you'd get: ``core.exception.RangeError@onlineapp.d(18): 
Range violation``


I don't use D with the GC, so my memory about it is probably 
foggy, but i'm pretty sure what i said is right, please anyone 
correct me if i'm wrong




Re: is the array literal in a loop stack or heap allocated?

2023-10-10 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Oct 11, 2023 at 02:54:53AM +, mw via Digitalmars-d-learn wrote:
> Hi,
> 
> I want to confirm: in the following loop, is the array literal `a` vs.
> `b` stack or heap allocated? and how many times?
> 
> void main() {
> 
> int[2] a;

This is stack-allocated. Once per call to the function.


> int[] b;

This is an empty slice. It can refer to either stack or heap memory,
depending on what's assigned to it.


> int i;
> While(++i <=100) {
> 
>   a = [i, i+1];  // array literal

`a` is overwritten in-place once per loop.


>   b = [i, i+1];
[...]

A new array consisting of 2 elements is allocated, once per loop, and
assigned to b each time. Any arrays from previous iterations will be
collected by the GC eventually.


T

-- 
They pretend to pay us, and we pretend to work. -- Russian saying


is the array literal in a loop stack or heap allocated?

2023-10-10 Thread mw via Digitalmars-d-learn

Hi,

I want to confirm: in the following loop, is the array literal 
`a` vs. `b` stack or heap allocated? and how many times?


void main() {

int[2] a;
int[] b;

int i;
While(++i <=100) {

  a = [i, i+1];  // array literal
  b = [i, i+1];

}

}


Thanks.


Re: Define a new custom operator in D Language.

2023-10-10 Thread Jesse Phillips via Digitalmars-d-learn

On Monday, 2 October 2023 at 18:34:13 UTC, BoQsc wrote:

---
**This might lead to less gaps between math formulas and the 
implementation.**


Or at the very least would allow to define a formula in the 
source code for further implementation and introduce some 
consistency.


You could write a parser with pegged 
https://code.dlang.org/packages/pegged


Could probably support unicode math symbols.


Re: Need help with 128bit integer ucent boolfilter

2023-10-10 Thread Per Nordlöw via Digitalmars-d-learn

On Friday, 6 October 2023 at 13:44:14 UTC, d007 wrote:
I am search for a fast 128bit integer ucent boolfilter, used 
for server side duplicate request filter.



Is 128bit boolfilter a doable thing? or it will not work or 
will be much more slow compare to 64 bit solution?


Can you describe or give a reference to what you mean by bool 
filter?


Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-10 Thread bachmeier via Digitalmars-d-learn

On Tuesday, 10 October 2023 at 13:55:44 UTC, rempas wrote:

On Tuesday, 10 October 2023 at 11:46:38 UTC, Hipreme wrote:
My engine has its own implementation of toString(long), which 
does not have dependency with the C runtime:


https://github.com/MrcSnm/HipremeEngine/blob/master/modules/util/source/hip/util/conv.d#L180C1-L208C2

I have reimplemented the entire conv module since it is one of 
mostly used module and it pulled down a lot of things, so, 
with my util module I was able to make my program much smaller 
too.


Thank you for the idea! However, your code used Phobos which I 
don't want to use so it will not do.


Which part uses Phobos? The linked function compiles without 
importing anything.


Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-10 Thread rempas via Digitalmars-d-learn

On Tuesday, 10 October 2023 at 11:46:38 UTC, Hipreme wrote:
My engine has its own implementation of toString(long), which 
does not have dependency with the C runtime:


https://github.com/MrcSnm/HipremeEngine/blob/master/modules/util/source/hip/util/conv.d#L180C1-L208C2

I have reimplemented the entire conv module since it is one of 
mostly used module and it pulled down a lot of things, so, with 
my util module I was able to make my program much smaller too.


Thank you for the idea! However, your code used Phobos which I 
don't want to use so it will not do.


Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-10 Thread rempas via Digitalmars-d-learn

On Tuesday, 10 October 2023 at 11:45:25 UTC, Dennis wrote:
The result of `.stringof` is implementation defined, it can be 
used for debugging but don't make your program's semantics 
depend on the output of it.


...

...

...That being said, this trick can be used to convert an 
integer to string at compile time:



```D
enum itoa(int i) = i.stringof;

static foreach(i; 0 .. 10) {
  mixin(create_fn!(itoa!i));
}
```

Technically not reliable, but I don't expect integers to ever 
get printed differently than a string of base 10 digits.


Thank you! It is great and works great. I will however use the 
example from Imperatorn as it does not use ".stringof". Have an 
amazing day!


Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-10 Thread rempas via Digitalmars-d-learn

On Tuesday, 10 October 2023 at 05:32:52 UTC, Imperatorn wrote:

If count < 10 then why not just

```d
import std;

static foreach(c; "0123456789")
{
  mixin(create_fn!(c));
}

enum create_fn(char num) = `
  auto function_`~ num ~`()
=> "Hello from function `~ num ~`!";
`;

void main()
{
  assert(function_9() == "Hello from function 9!");
}
```


Thank you! Yeah, the most clean code wins for me, so I'll use 
yours (if it's open source, lol)! Thank you and have an amazing 
day!


Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-10 Thread rempas via Digitalmars-d-learn

On Monday, 9 October 2023 at 22:49:11 UTC, Salih Dincer wrote:
Great masters generally warn to stay away from stringof. Please 
do not use it as much as possible. The following code snippet 
will be useful to you:


```d
alias CN = __traits(allMembers, CardinalNumbers);

static foreach(i; CN)
{
  mixin(create_fn!(i[1]));
}

enum create_fn(char num) = `
  auto function_`~ num ~`()
    => "Hello from function `~ num ~`!";
`;

enum CardinalNumbers
{
  n0, n1, n2, n3, n4, n5, n6, n7, n8, n9
}

void main()
{
  assert(function_9() == "Hello from function 9!");
}
```

SDB@79


Thank you so much! This will do the trick. Have a beautiful day!


Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-10 Thread Hipreme via Digitalmars-d-learn

On Monday, 9 October 2023 at 18:25:15 UTC, rempas wrote:

On Monday, 9 October 2023 at 17:42:48 UTC, Imperatorn wrote:


You could just add your own int to string I guess?


That will be a good idea! I'll do it in the future if that is 
the case, as it's not important, and I want to finish my job. 
Thank you and have a great day!


My engine has its own implementation of toString(long), which 
does not have dependency with the C runtime:


https://github.com/MrcSnm/HipremeEngine/blob/master/modules/util/source/hip/util/conv.d#L180C1-L208C2

I have reimplemented the entire conv module since it is one of 
mostly used module and it pulled down a lot of things, so, with 
my util module I was able to make my program much smaller too.





Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-10 Thread Dennis via Digitalmars-d-learn

On Monday, 9 October 2023 at 16:33:32 UTC, rempas wrote:
However, in my example, "stringof" returns the character "i" 
itself and turns that into a string instead of getting its 
actual value (number).


The result of `.stringof` is implementation defined, it can be 
used for debugging but don't make your program's semantics depend 
on the output of it.


...

...

...That being said, this trick can be used to convert an integer 
to string at compile time:



```D
enum itoa(int i) = i.stringof;

static foreach(i; 0 .. 10) {
  mixin(create_fn!(itoa!i));
}
```

Technically not reliable, but I don't expect integers to ever get 
printed differently than a string of base 10 digits.