get length of a dynamic array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

get length of a dynamic array

 
0
  #1
Jan 19th, 2005
Can't seem to figure this out.
  1. string *array;
  2.  
  3. void addNodes(string names[])
  4. {
  5. array = names;
  6. //how many elements in the array???
  7. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: get length of a dynamic array

 
2
  #2
Jan 19th, 2005
>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:
  1. void addNodes(string names[], size_t size)
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: get length of a dynamic array

 
0
  #3
Jan 19th, 2005
[humor]
keep accessing array elements from 0 until the program segfaults, then back up one and that's your limit.
[/humor]
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 4
Reputation: swets_here is an unknown quantity at this point 
Solved Threads: 0
swets_here swets_here is offline Offline
Newbie Poster

Re: get length of a dynamic array

 
-3
  #4
May 31st, 2005
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[]
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: get length of a dynamic array

 
1
  #5
May 31st, 2005
>> 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2
Reputation: Becuzz is an unknown quantity at this point 
Solved Threads: 0
Becuzz's Avatar
Becuzz Becuzz is offline Offline
Unverified User

Re: get length of a dynamic array

 
1
  #6
Jun 1st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: get length of a dynamic array

 
0
  #7
Jun 1st, 2005
>> 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:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2
Reputation: Becuzz is an unknown quantity at this point 
Solved Threads: 0
Becuzz's Avatar
Becuzz Becuzz is offline Offline
Unverified User

Re: get length of a dynamic array

 
0
  #8
Jun 1st, 2005
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)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: get length of a dynamic array

 
0
  #9
Jun 1st, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,941
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: get length of a dynamic array

 
0
  #10
Jun 1st, 2005
I get 4 for the sizeof(string), hmmm?
I 'd say vectorize!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC