Sizeof in function [duplicate] - c++

This question already has answers here:
determine size of array if passed to function
(10 answers)
size of array passed to C++ function? [duplicate]
(7 answers)
Closed 3 years ago.
How do I get the size of bits of an Array from a function
int NumberOfElements(int Array[]);
int main()
{
int Array[] = { 5,5,6,5,5 };
std::cout << NumberOfElements(Array);
}
int NumberOfElements(int Array[]) {
return sizeof(Array);
}
It's returning 4.
Result should be 20.

Arrays decay into pointers when passed as arguments to functions etc.
The size 4 means that the pointer has that size. It does not tell you anything about the number of elements in the actual array.
You may want to use a std::vector<int> instead where the size is part of its interface:
#include <vector>
int main()
{
std::vector<int> Array{ 5,5,6,5,5 };
std::cout << NumberOfElements(Array);
}
int NumberOfElements(const std::vector<int>& Array) {
return Array.size();
}

Related

Why can you pass an array as a pointer? [duplicate]

This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 6 years ago.
I was reading http://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm and the first method it recommends in passing an array to a function is to pass it as a pointer:
void myFunction(int *myArray) {
.
.
.
}
Wouldn't this not work, because it's impossible to determine the length of myArray in myFunction?
The usual convention is to pass a separate parameter for the array length.
void myFunction(int* myArray, int length) {
for (int index = 0; index < length; ++index) {
// ... do something with each array item ...
}
}
void caller() {
int array[10];
// ... put some useful values in the array ...
myFunction(array, sizeof(array) / sizeof(array[0]));
}

c++ size of array is changing using sizeof [duplicate]

This question already has answers here:
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
Closed 8 years ago.
i have simple main that called simple methods with array as parameter
the size in the array is right , but then when i try to print the array im getting different
sizeof array :
int bubbleSort(int arr[]) // yeah i know this sort is not complete
{
int arrSize = sizeof(arr); // HERE IS SIZE IS 4
bool bSorted = true;
while(bSorted)
{
for(int i=0;i<arrSize;i++)
{
if(arr[i]>arr[i+1])
{
int tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
}
}
bSorted = false;
}
return 1;
}
int main(int argc, char* argv[])
{
int arr[] = {4,3,7,8,9};
bubbleSort(arr);
int sOf = sizeof(arr); // HERE THE SIZE IS 20 ????
for(int j=0;j < sOf ;j++)
{
printf("%d",arr[j]);
}
return 0;
}
You cannot pass arrays* by value as function parameters (nor return them in such fashion). The syntax void f(int a[]) is merely syntactic sugar that is in every aspect identical to void f(int * a), and the function parameter is a pointer, not an array. So you end up measuring the size of the pointer.
The alternative syntax may serve as a loose kind of documentation of intent, signalling that you plan to call this function with the address of an array element, but there is no actual difference to the pointer syntax.
*) Array types and function types are both constrained thus, which is why people often say that they are not first-class citizens of the type system.
Due to the language being stupid, this:
int bubbleSort(int arr[])
actually means this:
int bubbleSort(int* arr)
So sizeof(arr) is the size of a pointer.

Passing an array of ints as an argument in C++? [duplicate]

This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Passing Arrays to Function in C++
(5 answers)
What is array to pointer decay?
(11 answers)
Closed 9 years ago.
I made a function in C++ to find the length of an array. I find the sizeof the array passed in the argument and divide it by the sizeof the variable type. This should work but it always returns 1! Am I missing something obvious? Or does this have to do with pointers and memory? This is my code:
#include <iostream>
using namespace std;
int lengthOf(int arr[]);
int main() {
int x[] = {1,2,3,0,9,8};
int lenX = lengthOf(x);
cout << lenX;
return 0;
}
int lengthOf(int arr[]) {
int totalSize = sizeof arr;
cout << totalSize << endl;
int elementSize = sizeof(int);
return totalSize/elementSize;
}
Output (should be 6 instead of 1):
4
1
I am fairly new so excuse me if this is a bad question.
When passing an array as parameter, it always decays into a pointer. If you want to see the size of the array, you need to pass the array by reference, e.g.:
template <int Size>
int lengthOf(int (&array)[Size]) {
return Size;
}
You should use the pointer.
(sizeof(arr)/sizeof(*arr))
Even though int arr[] looks like you are passing an array, you are actually passing a pointer. int arr[] is equivalent to int* arr when used as a function parameter, this comes from C.
In C++, if you want to pass an array, the proper way is to do it by reference:
template <int N>
int lengthOf(int (&arr)[N]) {
int totalSize = sizeof arr;
cout << totalSize << endl;
int elementSize = sizeof(int);
return totalSize/elementSize;
}

C++ array size different result [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sizeof an array in the C programming language?
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
string a[] = {"some", "text"};
void test(string a[])
{
int size_of_a = sizeof(a) /sizeof(a[0]);
cout << size_of_a;
}
int _tmain(int argc, _TCHAR* argv[])
{
test(a); //gives 0
int size_of_a = sizeof(a) /sizeof(a[0]);
cout << size_of_a; //gives 2
return 0;
}
as u can see in the comment test(a) gives 0 instead of 2 as i would expect. Could someone explain why and how could i correct it? thanks
When you pass an array to a function, it decays to a pointer to the first element of the array and so within your test(string a[]) function
sizeof(a);
actually returns the size of a pointer and not the size of your array.
To prevent array decaing to pointer, you can pass reference to array to the function. But it causes types of array of function formal argument and actual argument must coincide (including their sizes). So you need to use template to make your function work with an array of any size:
template <int N>
void foo(const string (&a)[N])
{
int size_of_a = sizeof(a) /sizeof(a[0]);
cout << size_of_a;
}

Change of behavior when passing int array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Passing an array as an argument in C++
Sizeof an array in the C programming language?
Can you please explain the output of the following code:
#include<iostream>
using namespace std;
void foo(int array[])
{
int size = sizeof(array) / sizeof(array[0]);
cout<<size<<endl;
}
int main()
{
int array[] = {1,2,3};
int size = sizeof(array) / sizeof(array[0]);
cout<<size<<endl;
foo(array);
return 0;
}
The corresponding output is:
3
2
Both the code inside foo() and inside main() looks similar to me so as to produce the same output, but it does not, can you please explain why?
void foo(int array[])
in C or C++ you cannot pass arrays by value, so the above declaration is interpretted as:
void foo(int * array)
Consider passing the array by reference:
template <size_t N>
void foo( int(&array)[N] )
{
cout << N << endl;
}
You are not passing the array into the function, you are passing the pointer to the array.
That means, in the function foo(), the result of sizeof(array) is the size of a pointer to an array of char, not the size of the array itself, so the function is effectively doing this:
cout << sizeof(int *) / sizeof(array[0]);
In this case size of int * is 8, size of int is 4, so you are getting an output of 2 from the function.