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

Usage of new keyword changes const-ness properties of variable #325

Closed
1 of 3 tasks
Southclaws opened this issue Jun 21, 2018 · 6 comments
Closed
1 of 3 tasks

Usage of new keyword changes const-ness properties of variable #325

Southclaws opened this issue Jun 21, 2018 · 6 comments

Comments

@Southclaws
Copy link
Collaborator

Southclaws commented Jun 21, 2018

Is this a BUG REPORT, FEATURE REQUEST or QUESTION?:

  • Bug Report
  • Feature Request
  • Question

What happened:

Using a new const cell as an array size:

new const MAX_OBJECT_ID_LEN = 25;
static PlayerObjectID[MAX_PLAYERS][MAX_OBJECT_ID_LEN];

Results in:

F:\Projects\samp-sandbox\gamemodes\sandbox\player-auth\player-auth.inc:26 (error) must be a constant expression; assumed zero
F:\Projects\samp-sandbox\gamemodes\sandbox\player-auth\player-auth.inc:26 (error) invalid array size (negative, zero or out of bounds)

What you expected to happen:

No error, since the value is const, I wouldn't expect the presence of new to change that fact.

How to reproduce it (as minimally and precisely as possible):

See above^

Anything else we need to know?:

Omitting new from the declaration compiles fine:

const MAX_OBJECT_ID_LEN = 25;
static PlayerObjectID[MAX_PLAYERS][MAX_OBJECT_ID_LEN];
@YashasSamaga
Copy link
Member

YashasSamaga commented Jun 22, 2018

const makes the symbol constant
new allocates memory for the symbol

Just using const makes the symbol a compile time constant whereas using new const allocates space for the symbol but makes it immutable.

Since you have used new, the symbol actually takes up space in the stack/data section. Hence, you can't get the value at compile time.

I haven't verified what's written above against what's given in the language guide but that's how the compiler works.

@Daniel-Cortez
Copy link
Contributor

This one must be related: #70.

@Y-Less
Copy link
Member

Y-Less commented Jun 22, 2018

Just did a quick test, this compiles fine:

main()
{
	new a = 5;
	new const b = a;
	printf("%d", b);
}

So they aren't truly compile-time constants, just values that can't be changed after initial assignment.

@Y-Less
Copy link
Member

Y-Less commented Jun 22, 2018

const b = a; fails, as does b = 7;.

@Southclaws
Copy link
Collaborator Author

Seems I misunderstood the semantics of the keywords. Now it's clear, it makes total sense why there is a difference. I also didn't know new had more semantic meaning than just "create a variable" - allocation at run time explains this perfectly.

@Y-Less
Copy link
Member

Y-Less commented Jun 23, 2018

To be fair, this is another of those extreme corner cases most people don't know - I had to do an experiment then to work it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants