Passing array into function only recieves first two items (newbie) [duplicate] - c++

This question already has answers here:
getting size of array from pointer c++
(6 answers)
What is array to pointer decay?
(11 answers)
Closed 6 years ago.
This definitely has a really simple answer, but I can't work it out. I've just made a simple function that outputs an array, but it only ever outputs the first two values of the array. I think it has something to do with the way I'm passing the array into the function.
#include <iostream>
using namespace std;
void outputArray(int arrayOut[]){
int size = sizeof(arrayOut)/sizeof(arrayOut[0]);//size is only 2??
cout << "{";
for(int i = 0; i < size; i++){
cout << arrayOut[i];
if(i != size-1){
cout << ", ";
}
}
cout << "}";
}
int main(){
int myArr[6] = {0, 1, 2, 3, 4, 5};
outputArray(myArr);
return 0;
}
Thank you very much!

Array variables are treated like pointers, so sizeof(arrayOut) is actually returning the size of the pointer arrayOut, not the size of the array. In 64-bit code, pointers are 8 bytes. sizeof(arrayOut[0]) returns the size of int, which is 4 bytes. So you get 2.
As #0x499602D2 states in their comment, you need to pass the array length as a separate parameter.

Related

How to properly delete pointer to an array in a C++ struct? [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Why can freed dynamically allocated memory still be accessed after a delete operation in C++? [duplicate]
(5 answers)
Closed 2 years ago.
I'm learning C++ from Stroustrop's A Tour of C++ 2E and I'm playing around with his examples.
I have a struct Vector, with a member elements that is a pointer to an array of doubles. I'm trying to delete[] the array which I allocated with new[], but even if there are no compile- or run-time errors, the array does not seem to be getting deleted. What's going on here?
#include <iostream>
struct Vector {
int size;
double* elements;
};
void print_array(double* first, int size) {
double* cursor = first;
for (int i = 0; i < size; ++i) {
std::cout << *cursor << " ";
++cursor;
}
std::cout << "\n";
}
int main() {
Vector v;
v.size = 5;
v.elements = new double[5];
for (int i = 0; i < v.size; ++i) {
v.elements[i] = 9-i;
}
print_array(v.elements, v.size);
delete[] v.elements;
std::cout << "v.elements==nullptr ? " << (v.elements==nullptr) << "\n";
print_array(v.elements, v.size);
return 0;
}
The output I'm getting is:
9 8 7 6 5
v.elements==nullptr ? 0
9 8 7 6 5
I do not yet know what will happen if I print out a deleted array, but I'm baffled as to why the third line in the output is happening at all. Thanks in advance.

C++ "new" operator not working as expected [duplicate]

This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
How do I find the length of an array?
(30 answers)
Closed 2 years ago.
int main()
{
int *array = new int; //Should provide space to store only one integer right?
for(int i =0; i < 10; i++)
{
cout << "Assigning" << i << "th value\n";
array[i] = i + 1;
}
for(int i = 0; i < 10; i++)
cout << array[i] << '\t';
delete array;
cout << '\n';
return 0;
}
Output
Assigning0th value
Assigning1th value
Assigning2th value
Assigning3th value
Assigning4th value
Assigning5th value
Assigning6th value
Assigning7th value
Assigning8th value
Assigning9th value
1 2 3 4 5 6 7 8 154274097 154405171
I know pointer size on my system is 8 bytes, checked with sizeof(int*) and integers take 4 bytes. I want to know how many chunks of memory in heap or free store (like literally boxes they use to teach students) does int* array = new int created, because books say I can store just one integer in it; to store more(like 10 ints) I need to do int* array = new int[10]. So how does this memory location pointed to by array can store 8 integers instead of one. Please note, I ran for loop 10 times as shown. Please tell me why are there just 2 garbage values, instead of 9 (since new should only allocate one integer worth space). Using Ubuntu with g++ 9.3.0.
int *array = new int; //Should provide space to store only one integer right?
Allocates a single int not an array.
for(int i =0; i < 10; i++)
{
cout << "Assigning" << i << "th value\n";
array[i] = i + 1;
}
Since there is no array but only a single element any access beyond the first (array[0]) array elements causes undefined behavior.
Any action, even formatting your hard drive, would be a valid program behavior.
Edit:
adapted to nearly completely rewritten question

Why size of c++ dynamic array is not changed? [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)
Closed 2 years ago.
Hi guys I tested c++ dynamic array and I changed 2 inputs every time.
but array size is not changed. Why?
#include <iostream>
using namespace std;
int main()
{
int r = 0;
int c = 0;
int cnt = 1;
cin >> r;
cin >> c;
int** arr1 = new int* [r];
for (int i = 0; i < r; i++)
{
arr1[i] = new int[c];
}
cout << sizeof(arr1) << endl;
cout << sizeof(arr1[0]);
}
I knew that If I entered two value 3 and 4 then results are 3 and 4
but the results are 4 and 4
You are testing the size of a pointer which is always 4 on 32 bit.
That's because sizeof is a compile time thing; it cannot determine an array size.
Generally arrays do not carry size information, that is why functions like strlen need a null terminator.
Suggestion: use std::vector.

Is there a FUNCTION that takes in an array and converts it to a vector? [duplicate]

This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
How to find the size of an int[]? [duplicate]
(13 answers)
Closed 2 years ago.
vector<int> array2vector(int array[]) {
int l = sizeof(array) / sizeof(array[0]);
vector<int> result(array, array + l);
return result;
}
int main() {
int my_array[] = { 1,3,5,2,6 };
vector<int> baby = array2vector(my_array);
for (int i : baby) {
cout << i << endl;
}
return 0;
}
There have been many ways to convert an int array to a vector, but the examples always starts with initializing an int array, and then converting it into a vector. However, I'd like to know if a function takes in an int array, and I want to convert it into a vector within the function, how I should do it?
The above code has the problem that sizeof(array) is "Diving size of a pointer by another value".
The code prints out
1
3
instead of the entire array.

Calculate array sum by given array only, without passing it's length [duplicate]

This question already has answers here:
How to get the length of an array in C? Is "sizeof" a solution? [duplicate]
(3 answers)
Closed 5 years ago.
I need to calculate array sum, but attribute must be ONLY this particular array.
bool solution(int arr[]) {
int counter = 0;
int len = sizeof(arr) / sizeof(arr[0]);
std::cout << len << std::endl;
for (int i=0; i < len; i++){
counter += arr[i];
}
if (counter == 21)
return true;
return false;
}
It won't work, I need to pass an array length from outside.
how to reach this without passing array length as an attribute?
how to reach this without passing array length as an attribute?
You cannot unless your array holds a sentinel value that marks the end of valid numbers.
Use std::vector if you have the option to. Then, the size information comes along for the ride.