Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sizeof for enums #44

Closed
Misiur opened this issue Mar 24, 2015 · 9 comments
Closed

Sizeof for enums #44

Misiur opened this issue Mar 24, 2015 · 9 comments

Comments

@Misiur
Copy link
Contributor

Misiur commented Mar 24, 2015

I'm not certain how compiler handles enumerators, but sizeof doesn't bode well with hidden array sizes:

enum E_TEST {
    RandomValue,
    StringValue[64]
};

new Anything[MAX_PLAYERS][E_TEST];

//...
sizeof Anything[0][StringValue]; //1 - it makes sense for compiler, but not for average user
@Arkshine
Copy link

You should use sizeof Anything[][StringValue]

@Misiur
Copy link
Contributor Author

Misiur commented Mar 24, 2015

Ok, if Zeex says it's an unreasonable request I'll respect that. But very often I see people caught up with default values, for example strcat:

native strcat(dest[], const source[], maxlength=sizeof dest);

So

strcat(Anything[0][StringValue], "Dickbutt");

Simply fails.

@Arkshine
Copy link

There is a known issue with enum field and sizeof in this situation. It has been fixed in the Pawn 3.3.3930.

This should be similar to this : Arkshine/amxmodx@a873066

@Zeex
Copy link
Member

Zeex commented Mar 29, 2015

I was thinking... could this possibly break something? From the language point of view, it seems totally fine to return 1 for sizeof Anything[0][StringValue] since enums are just array indices IIRC.

@Y-Less what do you think?

@Zeex Zeex reopened this Mar 29, 2015
@Y-Less
Copy link
Member

Y-Less commented Mar 29, 2015

I've always seen "1" here as the correct return. This enum:

enum X
{
    A,
    B[10],
    C
}

Is entirely equivalent to:

enum X
{
    A,
    B,
    C = B + 10
}

Or:

#define A X_tag:0
#define B X_tag:1
#define C X_tag:11
#define X X_tag:12

People would entirely expect "sizeof (B)" to return "1" there. "B" is just a subset of "X", asking for its size is asking for the size of part of an array, not the whole array, and with this patch not even an element of the array.

Also, I have always said "enums are not arrays" - they are flattened by the compiler. Doing:

new arr[X];
arr[B][3];

Is just fancy syntax for:

new arr[X];
arr[B + X_tag:3];

I didn't comment on this earlier, as if a majority of people think it should be different (as even the later official versions of PAWN seem to think it should) then who am I to argue? But as I say, given everything here, I don't think it makes sense from a language point of view. And since you asked directly...

@Arkshine
Copy link

I'm not sure we are talking about the same thing (patch is not about return of sizeof Anything[0][StringValue], also it should not possible to do that, I mean with 0), but how is handled an enum internally is irrelevant to the fact that if you use an enum as a "structure", you would want to expect sizeof B returns 10 and not 1. You're asking sizeof of an array not the index in the internal flat array. Enum in Pawn is an ugly hack for sure.

@Zeex
Copy link
Member

Zeex commented Mar 29, 2015

Indeed sizeof Anything[0][StringValue] doesn't compile outside of function calls. I was worried that this might have been used in some parts of YSI or perhaps other libraries.

I also looked this up in newer Pawn sources, couldn't find Pawn 3.3.3930 referenced in Arkshine/amxmodx@a873066 but 3.3.4058 has it. I guess they knew what they were doing and had thought about possible backwards compatibility issues in advance.

@Y-Less
Copy link
Member

Y-Less commented Mar 29, 2015

I can't actually think of a place where it would cause issues, at least not for YSI. To be fair, thinking about it more it might actually be better.

@Zeex
Copy link
Member

Zeex commented Mar 29, 2015

OK, then there are apparently no drawbacks, plus it's in the official Pawn. Let's leave it here.

@Zeex Zeex closed this as completed Mar 29, 2015
@Zeex Zeex added bug labels Mar 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants