| | |
get length of a dynamic array
![]() |
•
•
Join Date: Mar 2008
Posts: 1,386
Reputation:
Solved Threads: 112
Don't kill me for this (I know this must be the worst idea ever), but WOOH! I figured it out!
edit: but I just realised this doesn't even work if you dynamically allocate the names.. darn
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void addNodes(string names[]) { bool exception = false; size_t length = 0; string temp; do { try { temp = names[length]; ++length; } catch (...) { exception = true; } } while (!exception); // No idea why, but you have to take away 3 cout << (length - 3); } int main() { string names[10]; addNodes(names); cin.ignore(); return 0; }
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.
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
Please, don't waste time and traffic on idle discussions...
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.
•
•
Join Date: Sep 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> using namespace std; class obj{ public: obj(){} ~obj(){} unsigned long long data[10000]; void operator delete[] (void* ptr,size_t size){ cout<<"in operator delete : "; } }; int main(){ obj* objs=new obj[1000]; delete[] objs; delete[] objs; ::delete[] objs; cout<<"everything gone!"; return 0; }
Last edited by s0me1; Sep 14th, 2008 at 8:50 am.
>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:
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.
That's pretty obvious when you consider that your example is a glorified version of this:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class obj { public: void do_delete() { cout<<"In do_delete\n"; } }; int main() { obj *p = new obj[1000]; p->do_delete(); p->do_delete(); delete[] p; cout<<"Everything gone!\n"; }
I'm here to prove you wrong.
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
As Narue said, the reason is that your class's operator delete() does not release the memory. If you do this;
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.
void operator delete[] (void* ptr,size_t size){
cout<<"in operator delete : ";
::operator delete[](ptr);
}The whole purpose of overloading an operator delete is to correctly manage deallocation. In other words, you have to do it.
> 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.
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.
•
•
Join Date: Sep 2008
Posts: 3
Reputation:
Solved Threads: 0
" "
" "
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.
•
•
•
•
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.
>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]:
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.
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]:
C++ Syntax (Toggle Plain Text)
#include <iostream> class obj { unsigned char data[10000]; }; int main(){ obj *objs = new obj[1000]; std::cout<< sizeof *objs <<'\n'; delete[] objs; }
[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.
![]() |
Similar Threads
- how to expand arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: Sorting vectors
- Next Thread: making a graph in c++
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






