get length of a dynamic array

Reply

Join Date: Mar 2008
Posts: 1,386
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 112
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: get length of a dynamic array

 
0
  #21
Sep 13th, 2008
Don't kill me for this (I know this must be the worst idea ever), but WOOH! I figured it out!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void addNodes(string names[])
  5. {
  6. bool exception = false;
  7. size_t length = 0;
  8. string temp;
  9. do {
  10. try {
  11. temp = names[length];
  12. ++length;
  13. } catch (...) {
  14. exception = true;
  15. }
  16. } while (!exception);
  17.  
  18. // No idea why, but you have to take away 3
  19. cout << (length - 3);
  20. }
  21.  
  22. int main() {
  23. string names[10];
  24.  
  25. addNodes(names);
  26.  
  27. cin.ignore();
  28. return 0;
  29. }

edit: but I just realised this doesn't even work if you dynamically allocate the names.. darn
Last edited by William Hemsworth; Sep 13th, 2008 at 6:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: get length of a dynamic array

 
0
  #22
Sep 13th, 2008
I don't understand why this ancient thread is vivified.

It's impossible to get an array size by array parameter only.
For slow-witted reanimators: it's impossible in C and C++ to get a size of array argument by this array parameter only.

You all reanimators forget that:
1. An array parameter is a pointer to the 1st array element only; it's not a pointer to some kind of array descriptor. For example, you may pass a pointer to subarray of an arbitrary array - it's a valid argument for an array parameter.
2. It's impossible to recognize in a portable and standard conforming manner what's a storage class of a passed array argument - is it static, automatic or dynamic (heap allocated). So all tricks with delete operator overloading are senseless: you can't delete static or automatic arrays (you can try to do it, of course, but you know what happens after that).
3. Obviously, williamhemsworth's trick does not work too:
3.1 Illegal outside array boundaries access is capable to get (senseless) result without exception.
3.2 The C++ Language Standard does not require that memory access violation translated to C++ exception.

Regrettably, starting question of this thread was incorrect and inexact (3 years ago, today and tomorrow). It was a mix of two distinct issues: how builtin operator delete [] recognizes deleted array size and how to get array argument size by array parameter. The answer to the 2nd part - see this post. The answer to the 1st part: the C++ language Standard does not specify operator delete[] implementation; in practice most of C++ implementations allocate special memory descriptor just BEFORE allocated array memory chunk...

Please, don't waste time and traffic on idle discussions...
Last edited by ArkM; Sep 13th, 2008 at 8:12 pm.
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

Re: You can find the size of a dynamic array

 
0
  #23
Sep 14th, 2008
Since the destructors of all elements of the array will have been invoked before operator elete[]() 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.
I agree about the destructors being called, but I tried monitoring the memory status on my computer whilst stepping through the following code, and the memory doesn't seem to get freed untill you explicitly call the global delete[].

  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. class obj{
  5. public:
  6. obj(){}
  7. ~obj(){}
  8. unsigned long long data[10000];
  9. void operator delete[] (void* ptr,size_t size){
  10. cout<<"in operator delete : ";
  11. }
  12. };
  13. int main(){
  14. obj* objs=new obj[1000];
  15. delete[] objs;
  16. delete[] objs;
  17. ::delete[] objs;
  18. cout<<"everything gone!";
  19. return 0;
  20. }
Last edited by s0me1; Sep 14th, 2008 at 8:50 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
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: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: get length of a dynamic array

 
0
  #24
Sep 14th, 2008
>the memory doesn't seem to get freed until you explicitly call the global delete[].
That's pretty obvious when you consider that your example is a glorified version of this:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class obj {
  6. public:
  7. void do_delete()
  8. {
  9. cout<<"In do_delete\n";
  10. }
  11. };
  12.  
  13. int main()
  14. {
  15. obj *p = new obj[1000];
  16.  
  17. p->do_delete();
  18. p->do_delete();
  19.  
  20. delete[] p;
  21.  
  22. cout<<"Everything gone!\n";
  23. }
You managed to hide the inanity of your example by using operator overloading. Your overloaded delete doesn't do jack except print a message. The actual memory is allocated and owned by the global operator new, which can then logically only be released by the global operator delete.
I'm here to prove you wrong.
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: get length of a dynamic array

 
0
  #25
Sep 14th, 2008
As Narue said, the reason is that your class's operator delete() does not release the memory. If you do this;
void operator delete[] (void* ptr,size_t size){
		cout<<"in operator delete : ";
                ::operator delete[](ptr);
	}
it will release the memory.

The whole purpose of overloading an operator delete is to correctly manage deallocation. In other words, you have to do it.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: get length of a dynamic array

 
0
  #26
Sep 14th, 2008
> For slow-witted reanimators: it's impossible in C and C++ to get a size of array argument by this array parameter only.

Funny how there are all kinds of C and C++ libraries to do just that sort of thing. They are most popular for debugging and finding memory leaks. A very good library is DUMA. There are others.

The thing they typically have in common is judicious allocation of memory such that any invalid access will generate a page fault --which can be intercepted and interrogated.

WW's trick likely fails for two reasons, though neither are "obvious":

3.2 C++ exceptions != OS exceptions --but it is a common Win32 compiler option to integrate SEH into C++'s exception handling (and it can be done on other platforms too)

3.1 ...because you may very well own the memory outside of the array's bounds. This can happen 1) because the mm can divy its pool (called "the heap") any way it likes, and 2) because your object is very likely data-aligned. In WW's case, it appears to be on 32-bit boundries.


> Regrettably, starting question of this thread was incorrect and inexact...

Fortunate how forums like this exist to clarify such confusions, no?


> Please, don't waste time and traffic on idle discussions...

Please, don't waste bandwith on content-free, self-aggrandizing high-mindedness. If the topic was worth closing it would have been two years ago. Or perhaps people like Narue and AD are too dimwitted to judge stupid topics.
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

What I'm saying is that you can retreive the size of a dynamic array!!!

 
0
  #27
Sep 15th, 2008
"
As Narue said, the reason is that your class's operator delete() does not release the memory.
"

"
>the memory doesn't seem to get freed until you explicitly call the global delete[].
That's pretty obvious when you consider that your example is a glorified version of this:
"

I don't think you've understood the point that I'm trying to make (partly my fault: I should have reiterated what I said in my first post). I wasn't complaining that my delete[] doesn't delete - it's not supposed to delete! I'm trying to say that by overloading the delete[] operator with both the void* and the size_t parameter, you CAN find the size of a dynamic array from just the pointer. Although the destructor will be called, the memory will not be freed until, as I said, "you explicitly call the global delete[]". This means that as long as you declare an empty destructor, finding the length in bytes of the array will not affect the array in any way shape or form.

Yes, I know it's probably pointless to go to all that trouble when you could just store the array length, but I'm just saying that it is possible to find the size of a dynamic array.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
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: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: get length of a dynamic array

 
0
  #28
Sep 15th, 2008
>I'm trying to say that by overloading the delete[] operator[...]
Yea, your post didn't read like that at all.

>I'm just saying that it is possible to find the size of a dynamic array.
Not really. You're finding the size of a non-dynamic array within a dynamic object. That's far from the same thing, and you could easily do it without all of the rigmarole simply by taking the sizeof your object[1]:
  1. #include <iostream>
  2.  
  3. class obj { unsigned char data[10000]; };
  4.  
  5. int main(){
  6. obj *objs = new obj[1000];
  7.  
  8. std::cout<< sizeof *objs <<'\n';
  9.  
  10. delete[] objs;
  11. }
This is a variation of the age-old workaround for arrays being passed around by pointer rather than by value or reference (though it started in C and used structures rather than classes). There's no magic, and you're certainly not finding the length of a dynamic array, or more generally, determining the number of elements in an array given nothing but a pointer to the "first" element.

[1]: This is assuming that the class has no hidden data (such as a virtual table) and the only data member is the array.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: cog_bn is an unknown quantity at this point 
Solved Threads: 0
cog_bn cog_bn is offline Offline
Newbie Poster

_msize

 
-1
  #29
May 13th, 2009
Basically, see what you make of this:

http://msdn.microsoft.com/en-us/libr...04(VS.60).aspx
Last edited by cog_bn; May 13th, 2009 at 10:02 am. Reason: link was wrong
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