![]() |
| ||
| 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. |
| ||
| 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? |
| ||
| 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() |
| ||
| 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: // Get the size of a dynamic array // Get the size of an array 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. |
| ||
| 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 :D) |
| ||
| Re: get length of a dynamic array Quote:
void addNodes(string names[]) >> 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() const char* array = names.c_str(); |
| ||
| Re: get length of a dynamic array Quote:
|
| ||
| Re: get length of a dynamic array Quote:
|
| ||
| 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[]" |
| ||
| Re: You can find the size of a dynamic array Quote:
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. |
| All times are GMT -4. The time now is 1:07 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC