2 registered members (AndrewAMD, ChrstphFr),
941
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
sizeof() treats static arrays as pointers
#436167
01/20/14 21:49
01/20/14 21:49
|
Joined: Jul 2005
Posts: 1,930 Austria
Dark_samurai
OP
Serious User
|
OP
Serious User
Joined: Jul 2005
Posts: 1,930
Austria
|
Hi JCL, I just noticed the following behavior:
#include <acknex.h>
short test;
short array[20];
typedef struct TESTSTRUCT
{
short array[20];
}TESTSTRUCT;
TESTSTRUCT teststruct;
void main()
{
error(str_for_num( NULL, sizeof(test) ) ); // 2 -> OK
error(str_for_num( NULL, sizeof(array) ) ); // 4 -> NOK
error(str_for_num( NULL, sizeof(teststruct) ) ); // 40 -> OK
}
The seconds sizeof() statemant should return 40. Or am I wrong?
ANet - A stable and secure network plugin with multi-zone, unlimited players, voip, server-list features,... (for A7/A8)! get free version
|
|
|
Re: sizeof() treats static arrays as pointers
[Re: jcl]
#436193
01/21/14 13:22
01/21/14 13:22
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
That's nice, Dark_samurai is right though. The sizeof operator in the second example should result in 40. The wording in the standard is crap, but it clearly says that array sizes are returned. The relevant quote: When applied to an operand that has array type, the result is the total number of bytes in the array.
(Section 6.5.3.4) Pointers on the other hand don't count as arrays as far as the standard is concerned, so if it were a pointer, it should return 4.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: sizeof() treats static arrays as pointers
[Re: WretchedSid]
#436210
01/21/14 16:18
01/21/14 16:18
|
Joined: Jul 2005
Posts: 1,930 Austria
Dark_samurai
OP
Serious User
|
OP
Serious User
Joined: Jul 2005
Posts: 1,930
Austria
|
I don't think it's good to have this difference to normal C in such a basic functionality. Just imagine someone who used C already for some time and then switches to Lite-C because he wants to make games now. He writes some code and he will soon run into serious problems because sizeof works different. And the problem will be most likely some memory related crashes which sometimes may occur or may not (we all know this kind of problems I think). And to say the truth, if your code has a problem, I think the last thing you would check is if sizeof is working correctly ^^
Thats why in my opinion, this is a serious problem...
Edit: Thanks Sid for searching the according paragraph in the standard!
Last edited by Dark_samurai; 01/21/14 16:21.
ANet - A stable and secure network plugin with multi-zone, unlimited players, voip, server-list features,... (for A7/A8)! get free version
|
|
|
|