getting size of an array in c++
Hi there. I want to make an function that calculates the vector product of two vectors. Something like
Code:
int dotProd(int *v1, int *v2) |
hmm... I am weird so I normally don't do stuff like this with arrays. Instead I'll create a simple linked list object, with the last element having the usual NULL next-element-pointer. I then pass the address of the list's head nodes to the function you're writing rather than the addresses of arrays. To calculate the length of a linked list you can simply traverse it and keep count of nodes (you stop of course when you hit that NULL next element pointer). Or you can have the list object have a function that returns the size.
|
the classical way of getting the number of elements in an array is:
sizeof (array)/ sizeof(elem) But when the array is passed as a pointer, sizeof(array) returns the size of a pointer ! That's the strength and weakness of the C language. A pointer is a pointer is pointer... The compiler has no way to know what the pointer refers to. That provides a convenient way to switch the objects a pointer is pointing to but then it is your entire responsability of knowing what it is referring to. Just look at Windows api (must be the same for Linux) : all those structures passed as arguments. Each time the structure has an element to give the size of the structure. Looking at Stroustrup's (p147): an array can't be passed by value in a function's arguments. Ways around: When you got a zero-terminated string array it's easy to get the length. Or you pass the number of elements as well Or use standard lib vector type (which do the same thing only automatically) If you have used other higher-level languages like Python,.. that must look tedious but don't forget that the objects they manage store their length inside them. When you work with C you are as close as possible to the brass metal; when you ask for an array of 5 elements you got just that: 5 memory cells no more no less. And a pointer can point to anything (or be made to); if you use the pointer of the array to access a 6th inexistent element nobody's there to tell you you are getting in big troubles... |
schatoor & gradedcheese,
I would suggest that you dont use arrays, nor create your own linked list class (well not if your using c++ anyway). The STL has some great containers (including linked list) that will do what you want much better. Incase you dont know, the STL is part of the C++ standard is part of your c++ library.. (with most modern compilers) Code:
#include <vector> std::vectors are nice since they allow indexing like arrays (the operator[] is overloaded to return a reference to the item at the specified location) Depending on what you are actually doing, you might want to look as using the vector's push_back() method instead of size() and operator[]. push_back adds an element to the vector (increases its size by 1) and assigns the value you pass to it. Like :myVec1.push_back(10); will add the int '10' to the said vector.. For some good reference material see this thread where I posted some good links: Good reference material for C++ I hope that helps. Cheers. |
Wow, cool guy's, thanks for the detaild answers.
I my self thought of an solution along the lines of what cdlen said. I made an c++ vector class with an member var. indicating the size of the vector. But mr_segfault solution seems better. Thanks again for all the help. |
Another alternative (depending on your use of the vector class - i.e. it is not going to change size):
Code:
template<typename T, size_t N> Another advantage of making your own vector class is that you can include things like normalisation and all the other useful vector operations. |
|
dakensta,
Why did you choose float when it is storing ints? |
Most vector calculations I use are float or double precision (and I would have thought in general too), so force of habit I guess - int's would be fine for a dot product but add in, say magnitude, and you really need better precision.
1, 2, 3 were just the first numbers to come into my head, the type has already been specified in the template parameter, so there is no ambiguity ;) While I am in the confession box, I used a struct in the example, rather than a class, out of habit too - slightly easier when selecting an example and cutting and pasting some code to quickly check and compile. The example was really only to show the use of a template parameter as one possible method to include the size into a vector, where the size is not going to change. I added the type T to show maybe more familiar(?) template syntax for comparison and also allows int's to be used if that is what the original poster specifically requires. The operator[] was to keep the array-like syntax of arrays and std::vectors --- there are plenty of extensions that could be added (one useful one is cast to pointer, to substitute with functions that are defined like schatoor's dotProd) Sorry for any confusion, point duly noted and I hope this clears things up. |
All times are GMT -5. The time now is 04:30 PM. |