get length of a dynamic array

Reply

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
  #11
Jun 1st, 2005
>> Just make it this and it will work (i think)
No offense, but when you're guessing, you're not helping. Most of the time a guess is either grievously wrong, or has subtle problems.

>> so stop criticizing people
It's called constructive criticism. If you don't want to learn then don't bother trying to help because you'll just give bad advice. If you think you know everything then don't bother trying to help because you'll probably give bad advice and then turn the thread into a flame war when someone who knows better corrects you.
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

 
0
  #12
Jun 3rd, 2005
I tried using the sizeof operator as suggested above. But everytime for an int array it gives "1" as output and for a char array it gives "4". No matter how many elements I store in the array.
I donot know the usage of std::vector or boost::array. Can someone help? Pls tell me how can I get the number of elements stored in a dynamic array using them?

P.S:I had changed the example to char type as I was not sure if 'String' is a valid data type in C or C++. Is it?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: get length of a dynamic array

 
1
  #13
Jun 3rd, 2005
Basically, the only way to do this is what narue suggested and pass in the actual size as a parameter to the function. Either that, or use an STL collection, and use .size()
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
  #14
Jun 3rd, 2005
>> I tried using the sizeof operator as suggested above.
Don't bother, it won't work.

>> Pls tell me how can I get the number of elements stored in a dynamic array using them?
The only way to get the size of a dynamic array is to save the size when you allocate memory. The only way to get the size of an array parameter is to pass the size as another parameter:
  1. // Get the size of a dynamic array
  2. char *p = new char[10];
  3. int sz = 10; // Save the size!
  1. // Get the size of an array parameter
  2. void foo(char array[], int size); // Pass the size!
>> I donot know the usage of std::vector or boost::array.
That's not a good excuse. If you don't know the usage of a feature but you clearly need it, then it's time to learn. It's almost always better to use a container class than it is to use arrays anyway. Arrays are error prone while container classes work hard to protect you from those potential errors.

>> Is it?
String is not, unless you define it yourself, std::string is a class in the standard library.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1
Reputation: vamp74100 is an unknown quantity at this point 
Solved Threads: 0
vamp74100 vamp74100 is offline Offline
Newbie Poster

Re: get length of a dynamic array

 
0
  #15
Dec 25th, 2006
hi,

i'm studing computer sciences and i just met this problem.
i know it's been a long time the thread has been opened but i have one more question about it.

i already thought of passing the size of the dynamic array in parameter and i'm sure this will work fine, but as i am a studient, i always want to know more about it and even more when something seems wiered to me : if the only way of knowing the size of a dynamic array is by sending its size, how does free works ? you just pass the pointer as parameter and it cleans up the memory... i bet this function has the solution we are looking for.. but unfortunatly i have no books detailed enough to answer my question and i can't find anything on the web neither...

maybe there is a "Mister know-it-all" over here who can help me to understand that.

(i'm sorry if my english isn't verry correct but this is not my first language and so, please don't make answers to hard to understand, thank you )
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: get length of a dynamic array

 
0
  #16
Dec 25th, 2006
Originally Posted by Phaelax View Post
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. }
std::string has a function size() that will tell the current size.
  1. void addNodes(string names[])
  2. {
  3. cout << "size of array is " << names.size() << "\n";
  4. }


>> array = names;
That does not work because you variable names is a c++ class, not a pointer. If you want the pointer, then use c_str()
  1. const char* array = names.c_str();
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: get length of a dynamic array

 
0
  #17
Dec 25th, 2006
Originally Posted by Ancient Dragon View Post
std::string has a function size() that will tell the current size.
  1. void addNodes(string names[])
  2. {
  3. cout << "size of array is " << names.size() << "\n";
  4. }
Hmm...you are forgetting that names is an array of strings and not a std::string or a vector. So you can't apply the size( ) function to it, it is applicable to C++ containers, not array of containers. The only way to do this thing correctly is to do it like specified by Miss Narue in the second post.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: get length of a dynamic array

 
0
  #18
Dec 25th, 2006
Originally Posted by ~s.o.s~ View Post
Hmm...you are forgetting that names is an array of strings and not a std::string or a vector. So you can't apply the size( ) function to it, it is applicable to C++ containers, not array of containers. The only way to do this thing correctly is to do it like specified by Miss Narue in the second post.
Yes you are correct -- my mistake. Best thing for the op to do is use a vector instead of an array. I was wondering at the time how Narue could possibly make such a mistake -- turns out she didn't.
Last edited by Ancient Dragon; Dec 25th, 2006 at 8:59 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 3
Reputation: s0me1 is an unknown quantity at this point 
Solved Threads: 0
s0me1 s0me1 is offline Offline
Newbie Poster

You can find the size of a dynamic array

 
0
  #19
Sep 13th, 2008
I'm not an expert, but I was reading this article:
http://www.icce.rug.nl/docs/cplusplu...us09.html#l153

Apparently, if you overloaded the operator delete like this:
void operator delete[](void *p, size_t size);

the parameter "size" will tell you how long the array is.

However, if you want the normal delete[], you'll have to write "::delete[]"
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: You can find the size of a dynamic array

 
0
  #20
Sep 13th, 2008
Originally Posted by s0me1 View Post
I'm not an expert, but I was reading this article:
http://www.icce.rug.nl/docs/cplusplu...us09.html#l153

Apparently, if you overloaded the operator delete like this:
void operator delete[](void *p, size_t size);

the parameter "size" will tell you how long the array is.

However, if you want the normal delete[], you'll have to write "::delete[]"
Curiously, the C++ standard does not actually specify what value is passed as the size_t argument. In practice, it would be reasonable to expect (hope?) it would have the value passed to the corresponding invocation of operator new[](). If so, it would have to be divided by sizeof(the_type) to get the number of elements.

Since the destructors of all elements of the array will have been invoked before operator delete[]() is actually called, there is not much that can be done with the size_t argument other than pass it to a deallocation function (eg ::operator delete[]()) or print out the number of elements that were in the array being deallocated. The elements themselves will no longer exist, so no operation on them is allowed.
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