| | |
get length of a dynamic array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>> 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.
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.
•
•
Join Date: May 2005
Posts: 4
Reputation:
Solved Threads: 0
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?
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?
>> 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:
>> 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.
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)
// Get the size of a dynamic array char *p = new char[10]; int sz = 10; // Save the size!
C++ Syntax (Toggle Plain Text)
// Get the size of an array parameter void foo(char array[], int size); // Pass the size!
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.
•
•
Join Date: Dec 2006
Posts: 1
Reputation:
Solved Threads: 0
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
)
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
) •
•
•
•
Can't seem to figure this out.
C++ Syntax (Toggle Plain Text)
string *array; void addNodes(string names[]) { array = names; //how many elements in the array??? }
C++ Syntax (Toggle Plain Text)
void addNodes(string names[]) { cout << "size of array is " << names.size() << "\n"; }
>> 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)
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.
•
•
•
•
std::string has a function size() that will tell the current size.
C++ Syntax (Toggle Plain Text)
void addNodes(string names[]) { cout << "size of array is " << names.size() << "\n"; }
I don't accept change; I don't deserve to live.
•
•
•
•
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.
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.
•
•
Join Date: Sep 2008
Posts: 3
Reputation:
Solved Threads: 0
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[]"
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[]"
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
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[]"
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.
![]() |
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 arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






