Array Lengths?
-
Hey Everyone, I've got a short array that starts with 1 element (obviously) and it grows if it needs more elements. Untimately the array needs to grow as much as possible to suit the program.... I don't know however, how to return the size of the array. e.g. ShortArray[0] = 1; ShortArray[1] = 5; Length of the array is 2... Mean there are two elements. How do you return the length? Thanks for your help in advance.... I appreciate all your input :-) Cheers, Michael
-
Hey Everyone, I've got a short array that starts with 1 element (obviously) and it grows if it needs more elements. Untimately the array needs to grow as much as possible to suit the program.... I don't know however, how to return the size of the array. e.g. ShortArray[0] = 1; ShortArray[1] = 5; Length of the array is 2... Mean there are two elements. How do you return the length? Thanks for your help in advance.... I appreciate all your input :-) Cheers, Michael
Michael101 wrote:
I've got a short array that starts with 1 element
How did you declare the array?
Michael101 wrote:
Untimately the array needs to grow as much as possible to suit the program
This means you dynamically allocate the memory?
nave [OpenedFileFinder]
-
Michael101 wrote:
I've got a short array that starts with 1 element
How did you declare the array?
Michael101 wrote:
Untimately the array needs to grow as much as possible to suit the program
This means you dynamically allocate the memory?
nave [OpenedFileFinder]
register short* PrimeNumber = 0; That's how I declared the array.... I would change it if I need to though. Yeah I want allocate it more memory during runtime i.e it grows. Cheers, Michael
-
Hey Everyone, I've got a short array that starts with 1 element (obviously) and it grows if it needs more elements. Untimately the array needs to grow as much as possible to suit the program.... I don't know however, how to return the size of the array. e.g. ShortArray[0] = 1; ShortArray[1] = 5; Length of the array is 2... Mean there are two elements. How do you return the length? Thanks for your help in advance.... I appreciate all your input :-) Cheers, Michael
Use a
std::vector
and be done with it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
register short* PrimeNumber = 0; That's how I declared the array.... I would change it if I need to though. Yeah I want allocate it more memory during runtime i.e it grows. Cheers, Michael
Michael101 wrote:
I would change it if I need to though.
The memory can be allocated like PrimeNumber = new short[2];// an array having length of 2 is created So 2 is length of array. Similarly you can put the number of element you want in the array in the place of 2.
nave [OpenedFileFinder]
-
Michael101 wrote:
I would change it if I need to though.
The memory can be allocated like PrimeNumber = new short[2];// an array having length of 2 is created So 2 is length of array. Similarly you can put the number of element you want in the array in the place of 2.
nave [OpenedFileFinder]
Do you know any methods or functions that return how many elements that are in the array? Like before Array[0] = 1; Array[1] = 2; There are "Two" elements in this array. Do you know of anything like that? Cheers again, Michael
-
Do you know any methods or functions that return how many elements that are in the array? Like before Array[0] = 1; Array[1] = 2; There are "Two" elements in this array. Do you know of anything like that? Cheers again, Michael
Sorry I dont get your question correctly. Suppose you are allocating a memeory with size 5. Then the length of the array is 5. And whats the confusion in that? you can get the size of memory that you dynamically allocated using new or malloc using the function
_msize()
. Is that what you want?nave [OpenedFileFinder]
-
Use a
std::vector
and be done with it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
yep I agree.... Thank you for your help! Cheers
-
Hey Everyone, I've got a short array that starts with 1 element (obviously) and it grows if it needs more elements. Untimately the array needs to grow as much as possible to suit the program.... I don't know however, how to return the size of the array. e.g. ShortArray[0] = 1; ShortArray[1] = 5; Length of the array is 2... Mean there are two elements. How do you return the length? Thanks for your help in advance.... I appreciate all your input :-) Cheers, Michael
Michael101 wrote:
ShortArray[0] = 1; ShortArray[1] = 5; Length of the array is 2... Mean there are two elements. How do you return the length?
const unsigned int length = sizeof(ShortArray)/sizeof(ShortArray[0]);
That is, you divide the amount (in bytes) of your array by the size (in bytes) of one element.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
George Orwell, "Keep the Aspidistra Flying", Opening words