Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,519 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,926 people online right now. Registration is fast and FREE... Join Now!




size of array?

 
Reply to this topicStart new topic

size of array?

marla
post 22 Nov, 2005 - 08:01 PM
Post #1


New D.I.C Head

*
Joined: 17 Nov, 2005
Posts: 14



Thanked 1 times
My Contributions


i was trying to use

CODE
int myLength = sizeof(myArray);

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

Go to the top of the page


Mrafcho001
post 22 Nov, 2005 - 08:04 PM
Post #2


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


sizeof(array) will return the size of the array not how many items are in the array.
User is offlineProfile CardPM

Go to the top of the page

marla
post 22 Nov, 2005 - 08:49 PM
Post #3


New D.I.C Head

*
Joined: 17 Nov, 2005
Posts: 14



Thanked 1 times
My Contributions


umm ok ... so can you possibly point me in the direction of ascertaining the ammount of items in my array without using a loop ...
User is offlineProfile CardPM

Go to the top of the page

Nova Dragoon
post 22 Nov, 2005 - 08:58 PM
Post #4


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,124



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

marla
post 22 Nov, 2005 - 09:22 PM
Post #5


New D.I.C Head

*
Joined: 17 Nov, 2005
Posts: 14



Thanked 1 times
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Dark_Nexus
post 22 Nov, 2005 - 11:12 PM
Post #6


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,309



Thanked 1 times

Dream Kudos: 625
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 23 Nov, 2005 - 06:44 AM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

marla
post 23 Nov, 2005 - 12:58 PM
Post #8


New D.I.C Head

*
Joined: 17 Nov, 2005
Posts: 14



Thanked 1 times
My Contributions


using c. sizeof(myArray) / sizeof(int) worked out great, thanks!
User is offlineProfile CardPM

Go to the top of the page

microchip
post 23 Nov, 2005 - 01:13 PM
Post #9


New D.I.C Head

*
Joined: 14 Aug, 2005
Posts: 37



Thanked 3 times
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Dark_Nexus
post 23 Nov, 2005 - 03:42 PM
Post #10


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,309



Thanked 1 times

Dream Kudos: 625
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

microchip
post 24 Nov, 2005 - 07:12 AM
Post #11


New D.I.C Head

*
Joined: 14 Aug, 2005
Posts: 37



Thanked 3 times
My Contributions


Cool, didn't know that!
User is offlineProfile CardPM

Go to the top of the page

jaredolympia
post 24 Nov, 2005 - 09:42 AM
Post #12


New D.I.C Head

Group Icon
Joined: 15 Oct, 2005
Posts: 29



Dream Kudos: 75
My Contributions


[/CODE]arraySize = sizeof(array)/sizeof(array[0]);[CODE]
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 01:03AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month