943,884 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 87226
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 19th, 2005
0

get length of a dynamic array

Expand Post »
Can't seem to figure this out.
C++ Syntax (Toggle Plain Text)
  1. string *array;
  2.  
  3. void addNodes(string names[])
  4. {
  5. array = names;
  6. //how many elements in the array???
  7. }
Similar Threads
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jan 19th, 2005
2

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:
C++ Syntax (Toggle Plain Text)
  1. void addNodes(string names[], size_t size)
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 19th, 2005
1

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]
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
May 31st, 2005
-3

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[]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
swets_here is offline Offline
4 posts
since May 2005
May 31st, 2005
1

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.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
Jun 1st, 2005
0

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
Reputation Points: 9
Solved Threads: 0
Unverified User
Becuzz is offline Offline
2 posts
since Jun 2005
Jun 1st, 2005
0

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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. #define length(x) (sizeof(x) / sizeof(*(x)))
  4.  
  5. void foo(int a[])
  6. {
  7. std::cout << "From foo(): " << length(a) << '\n';
  8. }
  9.  
  10. int main()
  11. {
  12. int *a = new int[10];
  13. int b[10];
  14.  
  15. std::cout << "From main(): " << length(a) << '\n';
  16. foo(b);
  17. }
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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. template <typename T, int sz>
  4. char (&array(T(&)[sz]))[sz];
  5.  
  6. void foo(int a[])
  7. {
  8. std::cout << "From foo(): " << sizeof array(a) << '\n';
  9. }
  10.  
  11. int main()
  12. {
  13. int *a = new int[10];
  14. int b[10];
  15.  
  16. std::cout << "From main(): " << sizeof array(a) << '\n';
  17. foo(b);
  18. }
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.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
Jun 1st, 2005
0

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)
Reputation Points: 9
Solved Threads: 0
Unverified User
Becuzz is offline Offline
2 posts
since Jun 2005
Jun 1st, 2005
0

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 1st, 2005
0

Re: get length of a dynamic array

I get 4 for the sizeof(string), hmmm?
I 'd say vectorize!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: inheritance public , private difference
Next Thread in C++ Forum Timeline: How do you have a variable name itself?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC