September 24
On Tuesday, 24 September 2024 at 10:49:45 UTC, Dom DiSc wrote:
> On Tuesday, 24 September 2024 at 03:23:31 UTC, Jonathan M Davis wrote:
>> So, the issue really isn't the value itself. That will convert to other sizes just fine, particularly since it's known at compile time. Rather, what matters is what you get when you do math on it
>
> Doing math most of the time involves at least two values, in this case sizeof (of type size_t) and 4 (a nibble).

You might want to make sure you understand the problem before commenting, that would save everybody some time.

sizeof is a property here, not a value, and that property has for value 4, and type size_t.
September 24

On Tuesday, 24 September 2024 at 13:36:06 UTC, deadalnix wrote:

>

sizeof is a property here, not a value, and that property has for value 4, and type size_t.

When we want to look at the size of an enum, why is it 128 bytes if its original type is string? It's off topic but I want to give an example:

enum iColor : short { R, G, B } //*
enum sColor : string
{
    R = "red", G = "green", B = "blue"
}//*/

template ByteSize(E) if (is(E == enum))
{
    alias T = imported!"std.traits".OriginalType!E;
    enum ByteSize = T.sizeof * 8;
}

import std.stdio;

void main()
{
    ByteSize!iColor.writeln(" == ",
             iColor.sizeof * 8);

    ByteSize!sColor.writeln(" == ",
             sColor.sizeof * 8);
    
    String.sizeof.writeln; // 48
}

struct String
{
    immutable(char)[] str;
    size_t a, b, c, length;
} 

SDB@79

September 24

On Tuesday, 24 September 2024 at 14:06:50 UTC, Salih Dincer wrote:

>

On Tuesday, 24 September 2024 at 13:36:06 UTC, deadalnix wrote:

>

sizeof is a property here, not a value, and that property has for value 4, and type size_t.

When we want to look at the size of an enum, why is it 128 bytes if its original type is string?

I assume you mean "bits," not "bytes." A string in D consists of a 64-bit pointer and a 64-bit length, for a total of 128 bits (or 16 bytes).

September 24
On Tuesday, September 24, 2024 4:49:45 AM MDT Dom DiSc via Digitalmars-d wrote:
> On Tuesday, 24 September 2024 at 03:23:31 UTC, Jonathan M Davis
>
> wrote:
> > So, the issue really isn't the value itself. That will convert to other sizes just fine, particularly since it's known at compile time. Rather, what matters is what you get when you do math on it
>
> Doing math most of the time involves at least two values, in this
> case sizeof (of type size_t) and 4 (a nibble).
> It should be clearly defined in what type such an operation
> should result.

As long as sizeof is size_t, then math will give you ulong on 64-bit systems and uint on 32-bit systems. The main problem with it not being a size_t is if you're on a 64-bit system and you do math with a 32-bit value. For instance, it's quite common for folks to just use int by default with many numbers, but the result of multiplying such a value against a sizeof really needs to be size_t if you don't want potential overflow issues. If sizeof defaulted to ubyte just because it's a value that can fit in a ubyte, then multiplying it against an int would give you an int when what you really need if you want to avoid bugs is a ulong / size_t, because that's what's needed to represent larger memory sizes. There would quite easily be problems with large arrays if the type being used weren't size_t.

And yes, having sizeof be treated as size_t does sometimes get annoying when you don't actually need the larger values, and you want to do something other than get a value to use for something other than memory manipulation, but it means that you get the correct result by default, because you're guaranteed to be dealing with the correct type for memory sizes. If you then actually need a smaller type, you can cast to a smaller type. And since sizeof typically gives a value that fits in a ubyte (and definitely does with float), and the value is known at compile-time, you can always do something like

ubyte v = float.sizeof;

if you want to be dealing with a ubyte. D defaults to the size that is the least likely to result in silent bugs and then allows you to convert to smaller types if you want to for your particular application.

The entire reason that Steven is running into issues here is because he's deciding for whatever reason when dealing with raylib to use int for memory sizes when it's usually the case that the correct thing to do is to use size_t in order to ensure that you can correctly handle large arrays (and that can easily come up with stuff like manipulating files given how large they can get these days).

- Jonathan M Davis



September 25
On 9/24/2024 3:49 AM, Dom DiSc wrote:
> It should be clearly defined in what type such an operation should result.

D uses the C integer promotion rules for this. It's well defined.

September 25

On Sunday, 22 September 2024 at 12:59:04 UTC, Steven Schveighoffer wrote:

>

The chances that T.sizeof > int.max is near zero.

Code with T.sizeof > uint.max would not even compile:

https://issues.dlang.org/show_bug.cgi?id=21995
Issue 21995 - Struct with size uint.max or greater causes ICE

1 2
Next ›   Last »