Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (7th_zorro, degenerate_762, AndrewAMD, ozgur), 774 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
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 Offline OP
Serious User
Dark_samurai  Offline OP
Serious User

Joined: Jul 2005
Posts: 1,930
Austria
Hi JCL,

I just noticed the following behavior:

Code:
#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: Dark_samurai] #436180
01/21/14 08:11
01/21/14 08:11
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
In lite-C, sizeof() is a macro, not a function. It returns the size of the variable type, not of the whole array.

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 Offline
Expert
WretchedSid  Offline
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:

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 Offline OP
Serious User
Dark_samurai  Offline 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
Re: sizeof() treats static arrays as pointers [Re: Dark_samurai] #436218
01/21/14 18:32
01/21/14 18:32
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
You certainly have a point. I'll put a real sizeof() function on my list.

Re: sizeof() treats static arrays as pointers [Re: jcl] #436264
01/22/14 15:15
01/22/14 15:15
Joined: Jul 2005
Posts: 1,930
Austria
Dark_samurai Offline OP
Serious User
Dark_samurai  Offline OP
Serious User

Joined: Jul 2005
Posts: 1,930
Austria
Thanks laugh


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: Dark_samurai] #441519
05/26/14 06:57
05/26/14 06:57
Joined: Mar 2013
Posts: 18
USA - East Coast
D
DarkProgrammer Offline
Newbie
DarkProgrammer  Offline
Newbie
D

Joined: Mar 2013
Posts: 18
USA - East Coast
I had the same issue with the sizeof() command, so, I did a forum search & found this thread. Is there a "work around" for this problem?

Re: sizeof() treats static arrays as pointers [Re: DarkProgrammer] #441524
05/26/14 08:21
05/26/14 08:21
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Sure, always #define static array sizes. This should be a programmer habit anyway. You'll also need it for loops on that array.

#define MYLENGTH 20

short array[MYLENGTH];

...

int arraysize = MYLENGTH*sizeof(short);


Moderated by  jcl, Nems, Spirit, Tobias 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1