14 Replies - 221408 Views - Last Post: 07 February 2010 - 11:51 AM Rate Topic: -----

#1 marla  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 17-November 05

size of array?

Posted 22 November 2005 - 09:01 PM

i was trying to use

int myLength = sizeof(myArray);

it is an array of unsigned integers. how can i find out how many items are in the array?
Is This A Good Question/Topic? 1

Replies To: size of array?

#2 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: size of array?

Posted 22 November 2005 - 09:04 PM

sizeof(array) will return the size of the array not how many items are in the array.
Was This Post Helpful? 0
  • +
  • -

#3 marla  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 17-November 05

Re: size of array?

Posted 22 November 2005 - 09:49 PM

umm ok ... so can you possibly point me in the direction of ascertaining the ammount of items in my array without using a loop ...
Was This Post Helpful? 0
  • +
  • -

#4 Nova Dragoon  Icon User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 36
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Re: size of array?

Posted 22 November 2005 - 09:58 PM

well if you know how big your array is in bytes, and you know how big one integer is sizeof(int) i think will work, you can divide.
Was This Post Helpful? 0
  • +
  • -

#5 marla  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 17-November 05

Re: size of array?

Posted 22 November 2005 - 10:22 PM

the array is constantly being changed. i do not know the size of the array. i just want to find out the number of elements in the array.
Was This Post Helpful? 0
  • +
  • -

#9 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: size of array?

Posted 23 November 2005 - 12:12 AM

sizeof(myArray) should return the total size of the array in bytes. knowing this, and that the array is totally composed of integers and that each integer is 4 bytes, you can figure out how many items are in the array.

sizeof(myArray) / sizeof(int) = # of items in the array.
Was This Post Helpful? 1

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: size of array?

Posted 23 November 2005 - 07:44 AM

Are you using c or c++? If C++, then you should probably use vectors for storing a variable number of elements. Even if you are unable to use vectors, that array must be getting resized in your code somewhere...you should be able to determine the new number of elements using the information used to resize the array.
Was This Post Helpful? 0
  • +
  • -

#11 marla  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 17-November 05

Re: size of array?

Posted 23 November 2005 - 01:58 PM

using c. sizeof(myArray) / sizeof(int) worked out great, thanks!
Was This Post Helpful? 1

#12 microchip  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 37
  • Joined: 14-August 05

Re: size of array?

Posted 23 November 2005 - 02:13 PM

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.
Was This Post Helpful? 0
  • +
  • -

#13 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: size of array?

Posted 23 November 2005 - 04:42 PM

the sizeof() keyword is called everytime the compiler encounters it, or it is replaced with a known constant. either way, it will work during runtime.
Was This Post Helpful? 0
  • +
  • -

#14 microchip  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 37
  • Joined: 14-August 05

Re: size of array?

Posted 24 November 2005 - 08:12 AM

Cool, didn't know that!
Was This Post Helpful? 1

#23 jaredolympia  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 15-October 05

Re: size of array?

Posted 24 November 2005 - 10:42 AM

arraySize = sizeof(array)/sizeof(array[0]);

Was This Post Helpful? 1

#59 aekarahan  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 1
  • Joined: 07-January 09

Re: size of array?

Posted 17 May 2009 - 03:44 AM

microchip, you're right! sizeof works in compile time. If you send the array pointer to a function and ask for sizeof this pointer, you'll get the sizeof the pointer (32/64 bit 4 or 8 bytes depending the machine) thus it will return 4 or 8 in this case. But if your sizeof call see the actual array declaration (thus they are in the same scope) then the sizeof will not convert the array identifier to a pointer, and returns the array size IN BYTES.
Was This Post Helpful? 1

#60 Guest_Aaqib*


Reputation:

Re: size of array?

Posted 07 February 2010 - 09:47 AM

View Postmarla, on 22 November 2005 - 08:01 PM, said:

i was trying to use

int myLength = sizeof(myArray);

it is an array of unsigned integers. how can i find out how many items are in the array?


just write
int myLength = sizeof(myArray)/sizeof(int);
Was This Post Helpful? 0

#61 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: size of array?

Posted 07 February 2010 - 11:51 AM

While I think it's great that you posted with factual information, the quoted post is from 2005. :/ Relevant, but certainly not timely.


Locked.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1