943,662 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 87155
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jun 1st, 2005
0

Re: get length of a dynamic array

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

Re: get length of a dynamic array

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
swets_here is offline Offline
4 posts
since May 2005
Jun 3rd, 2005
1

Re: get length of a dynamic array

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()
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jun 3rd, 2005
0

Re: get length of a dynamic array

>> 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:
C++ Syntax (Toggle Plain Text)
  1. // Get the size of a dynamic array
  2. char *p = new char[10];
  3. int sz = 10; // Save the size!
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
Dec 25th, 2006
0

Re: get length of a dynamic array

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 )
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vamp74100 is offline Offline
1 posts
since Dec 2006
Dec 25th, 2006
0

Re: get length of a dynamic array

Click to Expand / Collapse  Quote originally posted by Phaelax ...
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. }
std::string has a function size() that will tell the current size.
C++ Syntax (Toggle Plain Text)
  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()
C++ Syntax (Toggle Plain Text)
  1. const char* array = names.c_str();
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Dec 25th, 2006
0

Re: get length of a dynamic array

std::string has a function size() that will tell the current size.
C++ Syntax (Toggle Plain Text)
  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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Dec 25th, 2006
0

Re: get length of a dynamic array

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Sep 13th, 2008
0

You can find the size of a dynamic array

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[]"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
s0me1 is offline Offline
3 posts
since Sep 2008
Sep 13th, 2008
0

Re: You can find the size of a dynamic array

Click to Expand / Collapse  Quote originally posted by s0me1 ...
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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008

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