how to get the size of an array
-
hi, is there anyway in C++ that I could get the size of a simple type array? I'm trying to pass an array to a function without passing the size of the array.. (a.k.a. Wal2k)
www.wal2k.com -
hi, is there anyway in C++ that I could get the size of a simple type array? I'm trying to pass an array to a function without passing the size of the array.. (a.k.a. Wal2k)
www.wal2k.comnot in general. if you know the arrays are always allocated with malloc, calloc or realloc, you can use _msize to get the allocated size, then divide by the type size. but that's a giant hack and is very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new) it's best to just pass the array size, or use a dynamic container like std::vector. Cleek | Image Toolkits | Thumbnail maker
-
not in general. if you know the arrays are always allocated with malloc, calloc or realloc, you can use _msize to get the allocated size, then divide by the type size. but that's a giant hack and is very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new) it's best to just pass the array size, or use a dynamic container like std::vector. Cleek | Image Toolkits | Thumbnail maker
Chris Losinger wrote: but that's a giant hack and is an very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new) For that reason, I never could get the infamous
countof
to work!// the countof macro for calculating array size
#define countof(X) (sizeof(X) / sizeof((X)[0]))Good to know why it doesn't work.. Behind every great black man... ... is the police. - Conspiracy brother Blog[^]
-
not in general. if you know the arrays are always allocated with malloc, calloc or realloc, you can use _msize to get the allocated size, then divide by the type size. but that's a giant hack and is very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new) it's best to just pass the array size, or use a dynamic container like std::vector. Cleek | Image Toolkits | Thumbnail maker
Chris Losinger wrote: it's best to just pass the array size, or use a dynamic container like std::vector. True. Another alternative, which is used sometimes for arrays of pointers, are NULL terminated arrays. -- jlr http://jlamas.blogspot.com/[^]
-
Chris Losinger wrote: but that's a giant hack and is an very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new) For that reason, I never could get the infamous
countof
to work!// the countof macro for calculating array size
#define countof(X) (sizeof(X) / sizeof((X)[0]))Good to know why it doesn't work.. Behind every great black man... ... is the police. - Conspiracy brother Blog[^]
That macro only works if the compiler can see X as an array:
#define COUNT_OF(X) (sizeof(X) / sizeof((X)[0]))
void DisplayNumbers(int[6] numbers);
void DisplayNumbers2(int* pNumbers);
void DisplayNumbers3(int* pNumbers, int nSize);
void main()
{
int numbers[] = {1, 2, 3, 5, 8, 13};
// Here it works fine
int nSize = COUNT_OF(numbers);
DisplayNumbers(numbers);
DisplayNumbers2(numbers);
DisplayNumbers3(numbers, nSize);
}
void DisplayNumbers(int[6] numbers)
{
printf("DisplayNumbers shows:\n ");
// Here it works fine too, although it's
// not very useful because you are already
// stating the size is 6
int nSize = COUNT_OF(numbers);
for (int i = 0; i < nSize; ++i)
printf("%d ", numbers[i]);
printf("\n");
}
void DisplayNumbers2(int* pNumbers)
{
printf("DisplayNumbers2 shows:\n ");
// Here it doesn't work at all, because for
// all the compiler knows, pNumbers is just
// a pointer to an int. Thus, sizeof(X) in
// the macro will return 4 (i.e., the size
// of any pointer), sizeof((X)[0]) will
// return 4 too, and the result for COUNT_OF(X)
// will then be 1.
int nSize = COUNT_OF(pNumbers); // will return 1
for (int i = 0; i < nSize; ++i)
printf("%d ", numbers[i]);
printf("\n");
}
void DisplayNumbers3(int* pNumbers, int nSize)
{
printf("DisplayNumbers3 shows:\n ");
for (int i = 0; i < nSize; ++i)
printf("%d ", numbers[i]);
printf("\n");
}The output for this program would be:
DisplayNumbers shows:
1 2 3 5 8 13
DisplayNumbers2 shows:
1
DisplayNumbers3 shows:
1 2 3 5 8 13-- jlr http://jlamas.blogspot.com/[^]
-
hi, is there anyway in C++ that I could get the size of a simple type array? I'm trying to pass an array to a function without passing the size of the array.. (a.k.a. Wal2k)
www.wal2k.com -
hi, is there anyway in C++ that I could get the size of a simple type array? I'm trying to pass an array to a function without passing the size of the array.. (a.k.a. Wal2k)
www.wal2k.comthanks to all of you, I was thinking about using a special value to end my array as it's posted here...