Passed array inconsistent bahevior; C++ [duplicate] - c++

This question already has answers here:
Using sizeof on arrays passed as parameters [duplicate]
(3 answers)
What is array to pointer decay?
(11 answers)
Closed 5 years ago.
I'm trying to learn some basic C++ and the moment I started to think I got a grasp of all those pointers I stumbled across this problem:
int sizeOf(string texts[]) {
return sizeof(texts);
}
int main() {
string texts[] = {"apple", "banana", "orange", "watermelon"};
cout << sizeof(texts) << endl;
cout << sizeOf(texts) << endl;
}
and this function returns
128
8
My question is: what is happening when I pass this array as an argument? Why suddenly C++ forgets that this is an array? I have tried to dereference it inside a method (return sizeof(*texts)) but it returned 32 instead which is the size of one string element, not a whole array. Is it possible to do what I want to do? Where am I mistaken?

Related

Trying to initialize a 2d zero array in C++ [duplicate]

This question already has answers here:
Segmentation fault on large array sizes
(7 answers)
Fill multidimensional array elements with 0's
(12 answers)
Closed 1 year ago.
I'm very new to C++ and was trying to initialize a large 2d zero array. However, if I run this it gives no errors but "test a" is not printed. Anyone know what I'm doing wrong?
#include<iostream>
using namespace std;
int main(){
cout << "test a" << endl;
int arrone[60480][12] = {};
}

Why is there a difference in size of bytes between an array and a vector in C++? [duplicate]

This question already has answers here:
What is the size of sizeof(vector)? C++
(4 answers)
sizeof() a vector
(6 answers)
Closed 2 years ago.
I'm going through a beginner course on C++ and like to pause the lesson and tinker around with the code on what I'm learning. I had the idea to use the sizeof() operator in C++ on a vector and an array. Ended up finding out that with the same amount of numbers in the vector and array the size of them in bytes comes of different. Here is my code.
#include <iostream>
#include <vector>
int main() {
std::vector <int> test_scores { 100,99,90,70,75 };
int test_scores_array[]{ 100,99,90,70,75 };
std::cout << sizeof(test_scores) << std::endl;
std::cout << sizeof(test_scores_array) << std::endl;
return 0;
}
with an output of this
16
20
after about 30 minutes of googling I can't figure out why the size is different. I don't really "need" to know but I just find it really interesting that it is different and would really like to know why.

Why does the sizeof operator return the wrong value when used on an array passed to a method? [duplicate]

This question already has answers here:
sizeof an array passed as function argument [duplicate]
(3 answers)
Why do C and C++ compilers allow array lengths in function signatures when they're never enforced?
(10 answers)
Closed 8 years ago.
int size(int arr1[])
{
int size1=sizeof(arr1)/sizeof(int);
cout<<size1<<endl;
return size1;
}
void main()
{
int b[]={1,2,3,4,5};
int size2 = size(b);
cout<<size2<<endl;
for (int i=0;i<size2;i++)
{
cout<<b[i];
}
}
I have put the b[] function into size() and check the size then return value.
however, it just return 1 as the answer.
Can anyone please help me to solve this.
A beginner of C++
sizeof(arr1) in the function returns the size of a pointer, not of the whole array.
That´s just how the language is.
You´ve to determine the array size without sizeof:
Either pass a second parameter with the number, or fill the array in a way
you can find the end because a certain value is there (and nowhere else)

Std::cout function as parameter [duplicate]

This question already has answers here:
Why do function pointers all have the same value?
(4 answers)
Closed 8 years ago.
I recently noticed you can do stuff like
void foo(){ }
//...
std::cout<<foo<<std::endl;
This tends to print out "1", regardless of the passed function.
Now I'm curious: What exactly is this doing?
foo is decaying to a function pointer, which is then converted to a boolean with value true (because the function pointer is not null), which is printed as "1", because booleans are printed as numbers by default.
Try this:
std::cout << std::boolalpha << foo << std::endl;

Why does the length function return 2? [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)
sizeof(array) / sizeof(int) [duplicate]
(2 answers)
Closed 9 years ago.
I have written a function size which takes integer array as an argument.
int length(int a[])
{
return sizeof(a)/sizeof(int);
}
int main()
{
int a[] = {1,3,5,6,9,4,2,1,0,0};
int len = sizeof(a)/sizeof(int);
cout << len; // This correctly prints 10 .
len = size(a);
cout << len; // But this print 2 .why ??
return 0;
}
Can someone explain me this behaviour?
Thanks.
The reason you get 2 is because sizeof(int *) is twice as large as sizeof(int), and arrays decay into pointers when passed into a function.
There are various ways to work around this. In C++, you could use a std::vector<int> a = { ... }; instead, which would solve the problem (by calling the a.size() to get the size, as sizeof(a) wouldn't work).
Because sizeof(a) is returning the size of the pointer which will be either 4 bytes or 8 bytes depending upon whether your app is 32-bit or 64-bit. So you would need to maintain the length of your array using a separate variable.
Or, better still, I would recommend using one of the standard collection classes, like std::vector for example since this will maintain the length automatically.