How to retrieve the size of this array? - c++

I'm using IMidiQueue to queue/add IMidiMsg objects to my IMidiQueue mMIDICreated;
At some times, I'd like to retrieve the number of items I've added on it. I've tried this:
char buffer[50];
sprintf(buffer, "size %d\n", sizeof(mMIDICreated) / sizeof(IMidiMsg));
OutputDebugString(buffer);
but after adding 8 items:
for (int i = 0; i < 4; i++) {
IMidiMsg* one = new IMidiMsg;
// ...
mMIDICreated.Add(one);
IMidiMsg* two = new IMidiMsg;
// ...
mMIDICreated.Add(two);
}
it returns 2, not 8. Where am I wrong?

sizeof will return the size of the object or type itself, it's a constant and is evaluated at compile-time, has nothing to do with the number of items which could be known only at run-time.
You should use IMidiQueue::ToDo:
Returns the number of MIDI messages in the queue.

Assuming that mMIDICreated is a pointer, doing sizeof on a pointer returns the size of the actual pointer and not what it points to. Also note that when passing an array to a function, it decays to a pointer to its first element.
If a function needs the number of elements in an array, you need to pass that along to the function as an argument.
An alternate solution, and one that I recommend over using plain arrays/pointers, is to use std::array (for arrays that are known at time of compilation) and std::vector for "run-time" or dynamic arrays.

Looking at your link:
class IMidiQueue
{
...
IMidiMsg* mBuf;
}
The buffer that stores the elements is not taken into the size returned by sizeof(). Only the size of the pointer itself.
However, there is also a method int GetSize() that could be useful to you.

Related

Is there a possible way to set a c++ array size to the return value of a function

I'm pretty new to C++ so please bear with me:
I am looking to set an array's size to the output of a function, for example:
//this is not the actual function, (go figure)
int getSizeInt(int size)
{
return size;
}
int main()
{
char charArray[getSizeInt(6)]; // Error: *function call must have a constant value in a constant expression*
return 0;
}
This may not be possible, I honestly don't know. I googled the issue and have been tinkering with different ways of initializing an array, but upto to no avail.
Is there a possible way to set a c++ array size to the return value of a function
Yes.
The size of an array variable must be compile time constant. A function call is a constant expression if the function is constexpr and its arguments themselves are constant expressions.
Your function does not satisfy those constraints, so its return value cannot be used as the size of an array variable.
It however can be used as the size of a dynamic array. Simplest way to create a dynamic array is to use std::vector (std::string may be considered instead if your intention is to represent text):
std::vector<char> charArray(getSizeInt(6));
Array sizes in C++ must be constant at compile-time, so the answer is sort of.
If your function is constexpr and called as part of a constant expression, then it can be used to statically set the size of the array. For example:
constexpr std::size_t square(std::size_t n) { return n * n; }
int my_array[compute_size(2)]; // array of 4 integers
However, this only works if you know all the data up-front at compile-time. If you are working with runtime values, such as things coming from files or from a user, then this will not work -- and you will have to resort to some other form of dynamic memory to handle this. In C++, generally this would be handled by a container such as a std::vector:
std::size_t compute_size() { /* some computation based on runtime */ }
// ...
auto vec = std::vector<int>{};
vec.reserve(compute_size()); // reserve the size up-front
vec.push_back( ... ); // push_back or emplace_back any data you need
If you reserve the size up front, you are able to avoid reallocation costs from push_back/emplace_back, provided you don't exceed the capacity.
Alternatively, you can initialize a vector of entries by doing either:
auto vec = std::vector<T>{};
vec.resize(N);
or
auto vec = std::vector<T>(N);
The difference here is that reserve only changes the capacity, which means you can't actually index up to N until you insert the elements -- whereas resize or vector<T>(N) will zero-initialize (fundamental types like ints) or default-construct (aggregate/class types) N instances immediately, which allows indexing.
Generally, reserve + push_back is better unless you know you want default-constructed / zero values.

Passing array values from a function back into a struct

I am currently working on a program that requires a function to figure out array values and then stores those values in arr1[], which has an unknown size and is calculated within the function. It then passes back the entire array along with the size of the array back to the main function. Using a struct, I returned the size, but I cannot return the array for some reason. Can someone please direct me in the right direction with what I'm doing wrong?
Here is my struct:
struct Arr
{
int size_1;
int arr_1[];
};
And here is part of my function that returns the array, where arr1[] is the array in which I need to return:
Arr smallElement(int arr[], int size)
{
Arr tempArr;
for (int count = 0; count < newSize; count++)
{
tempArr.arr_1[count] = arr1[count];
}
return tempArr;
}
This is what I use in my main function to call the function to print the array, but it just prints 3 random numbers every time:
Arr a;
a = smallElement(array, n);
cout << "The array is: ";
for (int count = 0; count < a.size_1; count++)
{
cout << a.arr_1[count] << " ";
}
Inside a struct, int arr_1[] does not define an actual array. It is actually just a placeholder called a flexible array member. In C, this is used by allocating memory for the fully defined part of the struct plus additional space for the array—you have to manually add space when using malloc or other memory allocation. In standard C++, you should not use this. (It is not part of standard C++. It is an extension adopted by some compilers from C, where it is standard.)
When you declared tempArr, it created just the fully defined portion of the struct. When you assigned values to tempArr.arr_1[count], there was no allocated array there, so the behavior is undefined. When you return tempArr, a copy of the struct is returned, but, again, only a copy of the defined portion.
If the caller knows the size the array will be, you may want to have them pass the array to the function, perhaps by allocating it with new or by using std::vector and passing the vector by reference. If the size is not known by the caller, then you may want to have the function allocate the array and return a pointer to it (in which case the caller is responsible for deleting it later), or you may want to pass an empty (or otherwise available for re-use) std::vector to the function, preferably by reference. Inside the function, it can add more elements to the vector, and the std::vector will manage memory allocation for you.

Select a random object from an array of objects

I'd like to implement a function that selects a random object from an array of objects and returns it to me. It should be something like (in C++ instead of psuedocode):
getRandomObject(objectList) {
return objectList[int(random(length of objectList))];
}
My current code looks like this, but doesn't seem to work:
//definition of random selector
object getRandomObject(Object* objectList) {
return objectList[int(ofRandom(0, sizeof(objectList)))];
};
//create a pointer for the listOfObjects
object* listOfObjects;
//create an empty object to put the randomly selected object in
object randomObject;
//later in the code, populate the array:
object* listOfObjects[] = {
new Object(),
new Object(),
new Object()
};
//select random object
randomObject = getRandomObject(listOfObjects);
But this seems to return a segmentation fault. A few problems I've noticed:
sizeof() returns the size of the pointer in getRandomObject, not the size of the array. is there a good way to get the size of the array? It might involves not using a float* pointer for the array. Is this a good use case for vectors?
I think that much of the problem lies in how I'm creating my arrays, and not so much in how I'm selecting the random object from them. I'm relatively new to C++ (coming from a Java background), so much of pointers / references / memory management in general is new to me.
thanks!
I see one definite problem and one possible one. The definite problem is that sizeof(objectList) returns the size of the objectList pointer, which will be 4 or 8 on most platforms. It does not return the number of elements in the array, objectList. Either pass in the length of the array or use std::vector or std::array.
The second possible problem relates to ofRandom. Make sure that ofRandom(a,b) returns numbers >= a, but strictly < b. If it returns values <= b, then you'll need to us ofRandom(0, objectVector.size() - 1). Typically, functions like this are written to return values strictly < b, but you should check.
C++ has an array template class that you may want to consider using. Check out the documentation here:
http://www.cplusplus.com/reference/array/array/
This type has a method, size(), that will return the length of the array.
When the sizeof operator is applied to an array, it yields the total
number of bytes in that array, not the size of the pointer represented
by the array identifier.
Quote
So you take the space alocated for your whole array and divide by the memory need just for one element: sizeof(objectList) / sizeof(*objectList).
Mr Fooz noticed issues that cause a segfault.
Other compilation issues are:
listOfObjects is declared with 2 different types: object* and object*[3] while getRandomObject expects a type Object*.
listOfObjects[] contains elements of type object* while getRandomObject reads elements of type Object and returns object.

Arrays and length in C++

I'm trying to get the length of an array passed as a parameter on some function.
The code is look like this :
double getAverage(int numbers[])
{
int length = sizeof(numbers)/sizeof(numbers[0]);
// here the result of the length is 1.
int sum = 0;
for (int i = 0 ; i < length ; i++)
{
sum += numbers[i];
}
return (double)sum / length;
}
int main()
{
int numbers[8] = {1,2,3,4,5,6,7,8};
//if I call here sizeof(numbers)/sizeof(numbers[0] the result will be 8 as it
//should be.
cout << getAverage(numbers) << endl;
return 0;
}
My question is how to get the array length which is passed as argument of a function by reference(although I know that every array is passed by reference)?
I know that there is a lot of questions about finding the array length in C/C++ but no one of them give me the answer which I'm looking for.
Thanks in advance.
You will have to explicitly pass the length of the array as an parameter to the function.
What you pass to the function is just an pointer to the array, not the array itself, so there is no way to determine the length of the array inside the function unless you explicitly pass the length as an function parameter.
You can probably use std::vector, which provides member functions to get no of elements in the vector, using std::vector::size(), that is the best you can do there is no way to do so using c-style arrays.
Arrays decay to pointers when passing them as parameters. You can't retrieve size information inside the function.
Why aren't you using std::vector? It's the c++ way.
At run-time, there is no information associated with an array that tells you its length. The array pretty much "decays" into just the address of the first element.
At compile-time, the length is part of the type, so if you declare your function to take e.g. int numbers[8] you can get the length using the sizeof expression you mention.
Of course, this means you can only validly call the function with arrays of length 8, which kind of makes it a bit useless.
Thus, the only way around this is to explicitly add information at run-time about the array's length, by adding a second size_t length argument to the function.
In C++, you could also use templates to have the compiler create specialized versions of the function for each array length, but that is kind of wasteful.
As pointed out by others, you can also "level up" your abstraction and use e.g. std::vector<int> to get a size() method. That is of course pretty much the same thing, the vector container adds run-time information about the number of elements.
This might not be "the answer which you're looking for", I'm sorry about that.
If you must use an array, you could 'templatize' your function:
template <size_t length> double getAverage(int (&numbers)[length]) {
int sum = 0;
for (int i = 0 ; i < length ; i++)
{
sum += numbers[i];
}
return (double)sum / length;
}
You have to pass in the length as a parameter, or use std::vector which "contains" the length. You can access it with the size() method.
Or use std::vector (instead of int[]) which provides a size() function
You can use std::vector, or std::list as all have give. But if you are adamant that you want to use an int[] without a second argument, then you can insert a code number as the last element of the array. that way you can know the end.... Or u can save the length of the array in its first element and use the rest normally.
You can pass an array by reference in which case the areay size has to be specified. However, the size of a statically sized array can be deduced for a template argument:
template <int Size>
double getAverage(int (&numbers)[Size]) { ... }
The only problem with this approach is that it creates a new instantiation for each array size. Of course, the fix to this is to actually pass begin and end iterators to the function doing the actual work. The iterators can easily be determined using begin() and end() functions using the trick above. The code would look something like this:
double average
= std::accumulate(begin(numbers), end(numbers), 0.0)
/ std::distance(begin(numbers), end(numbers));
You can use templates:
template<std::size_t Length>
double getAverage(int (&numbers)[Length])
{
...
}
but this may lead to code bloat as the compiler will create this for every new array size you pass in. You might be better off combining a template with a parameter
template<typename T, std::size_t Length>
std::size_t GetCount(T (&numbers)[Length])
{
return Length;
}
[main]
getAverage(numbers, GetCount(numbers));

Declaring char[][512]?

I have an C++ SDK that requires a char[][512] as a parameter. I know that this is supposed to be a list of file names and the number of files could vary. For the life of me I cannot figure out how to declare this. I have an array of CStrings and I am trying to copy them over using strcpy_s and then pass them into the SDK. Any idea on how to do this?
This declaration has a special meaning when used to declare parameter of a function. Within the parameter list it is equivalent to char[100][512], char[123][512], char[3][512] (you get the idea - the first size can be just anything, it is simply ignored) and also to char (*)[512]. Effectively, it will accept as an argument a 2D array of chars with flexible (arbitrary) first size.
The array that you will actually pass to this function should be declared with a concrete first size, for example
char names[3][512] = { "abc", "cde", "fgh" };
if you know the first size at compile time, of course.
If the first size is only known at run time (say, n), you'll have to allocate the array dynamically
char (*names)[512] = new char[n][512];
// Now fill it with names
or, more elegantly, with a typedef
typedef char TName[512];
TName* names = new TName[n];
// Now fill it with names
I expect that the SDK function you are talking about also asks you to pass the first size of the name array as another parameter.
It means 2D array of char. The number of rows could vary, and it should/may be specified in another parameter. C/C++ compilers need to know the number columns when a 2D arrays is passed ,So they can build the mapping function. Simply because arrays decay to pointers when they are passed as parameters, size information is lost. For example:
void fun(char matrix[][512], int rows);
...
char matrix[100][512];
...
fun(matrix, 100);
The mapping function that the compiler construct for a 2D array is similar to:
// arrays in C/C++ are stored in Row-Major Order
matrix[i][j] == matrix[i*numberOfColumns + j]
As you can see, when a 2D array is passed and the size information is lost, we need only the number of columns to index any element in this array.
Here is a way to convert an argv-style array of filenames into the form your SDK needs.
typedef char Char512[512];
Char512 * convert(const char *names[], int n)
{
Char512 * arr;
arr = new char[n][512];
for (int i = 0; i < n; n++)
::strncpy(arr[i], names[i], 512);
return arr;
}
When in doubt, use a typedef.
Just a reminder, if you new[] something, you must delete[] (not delete) it sometime.