DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   get length of a dynamic array (http://www.daniweb.com/forums/thread17212.html)

Phaelax Jan 19th, 2005 2:01 pm
get length of a dynamic array
 
Can't seem to figure this out.
string *array;
 
void addNodes(string names[])
{
        array = names;
        //how many elements in the array???
}

Narue Jan 19th, 2005 3:02 pm
Re: get length of a dynamic array
 
>Can't seem to figure this out.
You're not the only one. There isn't a portable way to get the size of a dynamically allocated array. You need to pass the size to your function:
void addNodes(string names[], size_t size)

Chainsaw Jan 19th, 2005 4:13 pm
Re: get length of a dynamic array
 
[humor]
keep accessing array elements from 0 until the program segfaults, then back up one and that's your limit.
[/humor]

swets_here May 31st, 2005 6:04 am
Re: get length of a dynamic array
 
I guess this would be helpful,
/******for a static array********/
char inp[4];
for (n=0;inp[n];n++)
; //do nothing inside the loop
cout<<n; //n will display the number of elements in the array inp[]

/******for dynamic array********/
char *arr = new char [sizeof(char)];
for (n=0;arr[n];n++)
; //do nothing inside the loop
cout<<n; //n will display the number of elements in the array arr[]

Dogtree May 31st, 2005 10:03 am
Re: get length of a dynamic array
 
>> I guess this would be helpful
No, not a bit. Your code is wrong.

>> for (n=0;inp[n];n++)
Assuming that n was defined somewhere, the test for imp[n] against 0 is dngerous because imp is uninitialized. There could be a null character straight away, or 5000 characters later. You're really risking an access violation with this loop.

>> char *arr = new char [sizeof(char)];
This alloctes memory for one char. sizeof(char) is guaranteed to return 1, everywhere, without fail. That's one of the few absolutes when it comes to type sizes in C++.

>> for (n=0;arr[n];n++)
You have the same problem here as the previous loop.

Yes, your idea is valid assuming there's some sentinel value at the end of the array to stop the loop on. With the original question that's difficult because any string object is valid in the general case. That's probably why you took it upon yourself to change the example to char so that you could use a null character as the sentinel.

The best solution is to avoid using arrays in the first place because they're unsafe and most people don't understand them well enough to avoid the pitfalls, as displayed by your flawed example. The std::vector class provides a good container that grows dynamically.

Becuzz Jun 1st, 2005 11:09 am
Re: get length of a dynamic array
 
The easiest way to get the length of a dynamic array is this

sizeof(array)/sizeof(arraytype)

where array is your dynamic array and arraytype is the data type (for instance int)

Hope this helps :)

Dogtree Jun 1st, 2005 11:49 am
Re: get length of a dynamic array
 
>> The easiest way to get the length of a dynamic array is this
Is it? Forget about the 'array' part and look closely at the 'dynamic' part. A dynamic array is not an array, it's a pointer to a block of memory that can be subscripted like an array:
#include <iostream>

#define length(x) (sizeof(x) / sizeof(*(x)))

void foo(int a[])
{
  std::cout << "From foo(): " << length(a) << '\n';
}

int main()
{
  int *a = new int[10];
  int b[10];

  std::cout << "From main(): " << length(a) << '\n';
  foo(b);
}
So the sizeof trick just breaks silently when you use it on a dynamic array, or an array passed as a function parameter. Templates are a better solution because they complain when you pass a pointer and not an array:
#include <iostream>

template <typename T, int sz>
char (&array(T(&)[sz]))[sz];

void foo(int a[])
{
  std::cout << "From foo(): " << sizeof array(a) << '\n';
}

int main()
{
  int *a = new int[10];
  int b[10];

  std::cout << "From main(): " << sizeof array(a) << '\n';
  foo(b);
}
The rule of thumb is that if you want the size of a dynamic array, you save it! If you want the size of an array parameter, you pass it! Anyone who doesn't know these rules or isn't comfortable with them would be better off using a smart container like std::vector or boost::array.

Becuzz Jun 1st, 2005 12:36 pm
Re: get length of a dynamic array
 
Ok so i mistyped. Just make it this and it will work (i think)
sizeof(*names)/sizeof(string)

Happy?

P.S. btw i know to pass the length of a dynamic array or to use vector but im just trying to solve this guy's problem. (so stop criticizing people)

Dave Sinkula Jun 1st, 2005 12:57 pm
Re: get length of a dynamic array
 
Try it with a dynamic array and find out you are wrong.

You are being helpfully corrected, you are the one starting to get out of line.

vegaseat Jun 1st, 2005 1:08 pm
Re: get length of a dynamic array
 
I get 4 for the sizeof(string), hmmm?
I 'd say vectorize!


All times are GMT -4. The time now is 11:25 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC