Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

iOS and MacOS / "sizeof" does not work

Author
Message
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 26th Apr 2013 18:52
Hi all,

I try to use sizeof but I always get result 4
( sizeof "int" I guess ).

NSLog(@"sizeof Array = %ld ", sizeof Array);

I tryed ld, ud , d and get 4 but I know the size of
my Array.

When I use 'go to definition' I get 'Symbol Not Found',
so where should sizeof be, and how do I get it ?

Can you help?


cheers
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 26th Apr 2013 19:30
How is Array defined?

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 26th Apr 2013 22:51
int Size = n;

int *Array = new int[Size];

for ( int i = 0; i < Size; i++)
{
Array[i] = i;
}
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 26th Apr 2013 22:53
sorry,


int Size = n;

int *Array = new int[Size];

for ( int i = 0; i < Size; i++)
{
Array[i] = i;
}
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 26th Apr 2013 22:55
sorry,
the Array in the for loop has square brackets with an i in it [i],
but when I post it it disapiers.


int Size = n;

int *Array = new int[Size];

for ( int i = 0; i < Size; i++)
{
Array[i] = i;
}
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 27th Apr 2013 18:30 Edited at: 28th Apr 2013 18:34
Where is the first 'n' defined?

Is the code you posted being done in a function?

And, I would not use variable names like 'Size' and 'Array'. They are too close to potential actual types or functions.

If you put your code snippets in the code blocks (using the code button in the message post box before and after the code, or typing \[code\] and \[/code\] around them, without the '\'s, they are just so that the tags don't get converted in this post) then the brackets would work.

The process that handles the post interpreted '<bracket>i' to indicate that you want the following text to be in italics.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 27th Apr 2013 18:56
'n' only stands for natural number

'Array' stands for the name I have chosen for the Array

and yes it is in a function.

[/code]

int Size = 16;
int *GameSpriteArray = new int[Size];

for ( int i = 0; i < Size; i++)
{
GameSpriteArray[i] = 10001 + i;
}

NSLog(@"sizeof GameSpriteArray === %ld ", sizeof GameSpriteArray);


[code]
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 27th Apr 2013 21:37 Edited at: 27th Apr 2013 21:38
Try this page and see if it helps.

What sort of output, if any, is the NSLog command producing in your log?

It notes that you will get the size of the array in bytes, not in the number of elements.

Any text that is tagged in these posts uses the following format (inside a code block so the tags are ignored):


EDIT: I tried to put tagged code in the above, but the tag closed it and left one hanging. Tags start with '[tag]' and end with '[/tag]'

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 28th Apr 2013 00:47
I think the key is

"Symbol Not Found"

when I use "go to definition".


Do You get the same message ?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 28th Apr 2013 02:38
Well, I haven't tried any of your code, since there is only the smallest snippet.

But the message seems to say it all. I wouldn't be surprised if it really doesn't like an array named Array.

If you show the full function code, maybe it would make more sense.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 28th Apr 2013 04:26
I changed GameSpriteArray to GameSpriteA,

same thing.

What do you get when you do "go to definition"?

Where should sizeof be located ?
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 28th Apr 2013 04:42
Thats an example,

I can use ld or lu result is always 4.

void TestFunc( int StartNum )
{
int Size = 16;
int *GameSpriteA = new int[Size];

for ( int i = 0; i < Size; i++)
{
GameSpriteA[i] = StartNum + i;
}

unsigned long int temp = sizeof GameSpriteA;
NSLog(@"sizeof GameSpriteA === %ld ", temp);

}
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 28th Apr 2013 05:36
From your original snippets, I thought the array was actually named only 'Array'.

Did you look at the link I gave you about the sizeof function?

Have you figured out why you get the message "Symbol Not Defined"?

That sounds like your problem.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 28th Apr 2013 06:00
What do you get when you do "go to definition"?

Where should sizeof be located ?
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 28th Apr 2013 06:07
I saw the page before and many others,

it should work the way I do it.

sizeof should return the size in bytes.
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 28th Apr 2013 06:18
Just ran into this

" Doesn't sizeof only work at compile time?

So if you use dynamic memory allocation, you can't use this. You'll have to record the length yourself. "
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 28th Apr 2013 18:28
Quote: "Where should sizeof be located ?"

When I select the text sizeof and right click, I get the option to go to the header file in the list, but not go to definition.

sizeof is a function call.

I added the following to one my init functions in my WIP (please learn how to use the buttons in the 'Post Forum Message' form to bracket your code):



The TADisplay is a class I created to handle a variety of display things.

The showAGKmessage takes a format string and variables, similar to the NSLog function you are using, and pops up a message.

The message said that the array size was 4. Which is correct because Array (I went with that just to see how the compiler liked it) is an integer pointer, not an actual array. You can treat it as an array once you've assigned values to it, but the actual variable itself is NOT an array, it is a pointer.

And the correct format specifier is '%u' for unsigned int.

I think I misunderstood your first post. But the sizeof returning 4 is correct. If your array was defined as something like 'int Array[16]', then you would get the number of bytes actually used for the array (which happens to be 64, 4*16);

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master

Login to post a reply

Server time is: 2024-11-23 09:44:08
Your offset time is: 2024-11-23 09:44:08