#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[4]={1,2,3,4};
for(int i=0; i<6; i++){
cout << arr[i]<<endl;
}
return 0;
}
I tried running this code in visual studio and I'm getting the output as:
1
2
3
4
4
4200976
After modifying it also, I'm getting arr[n] as the length of the array.
So is it always true, and can we assume that it is the most optimised way to get the length of the array?
The behavior of accessing elements outside the bounds of the array is undefined.
and can we assume that it is the most optimised way to get the length of the array?
This isn't a way to get the length of the array.
A correct, and efficient way to get the size of the array is to use std::size(arr).
Related
I was solving a hackerrank problem named 'Variable Sized Arrays' where the problem statement is:
Consider an n-element array,a, where each index i in the array contains a reference Kito an array of integers (where the value of Ki varies from array to array). See the Explanation section below for a diagram.
Given a, you must answer q queries. Each query is in the format i j, where i denotes an index in array and j denotes an index in the array located at a[i] . For each query, find and print the value of element j in the array at location on a[i]a new line.
Input Format
The first line contains two space-separated integers denoting the respective values of n (the number of variable-length arrays) and q (the number of queries). Each line of the subsequent lines contains a space-separated sequence in the format k a[i]0 a[i]1 … a[i]k-1 describing the k-element array located at a[i]. Each of the q subsequent lines contains two space-separated integers describing the respective values of i (an index in array a) and j (an index in the array referenced by a[i]) for a query.
Sample Input
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
Sample Output
5
9
I am a beginner in c++, I did the code below:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
vector<int*> vec;
int n,q;
cin>>n>>q;
////Adding n arrays to the vector////
for(int i=0;i<n;i++) {
int a;
cin>>a; ///// a is the length of the particular array
int arr[a];
for(int j=0;j<a;j++) {
cin>>arr[j];
}
vec.push_back(arr); ////// push the array in the vector
}
////// Answering to queries /////
for(int i=0;i<q;i++) {
int y,z; /// z^th element in y^th array in the vector
cin>>y>>z;
int* arr;
arr = vec[y];
cout<<*(arr+z)<<endl;
}
return 0;
}
This code only works for 1st array in the vector. For later arrays, it gives a random value.
i.e. input:
2 4
3 1 5 8
5 1 2 8 9 3
0 1
0 2
1 0
1 2
Output:
5
8
7208632
4199698
You are not considering the lifetimes of the objects you are creating.
In this code the array arr exists only inside the for loop.
vector<int*> vec;
for(int i=0;i<n;i++) {
int a;
cin>>a;
int arr[a]; // arr created here
for(int j=0;j<a;j++) {
cin>>arr[j];
}
vec.push_back(arr);
} // arr destroyed here
But you have a vector of pointers and you are storing a pointer to arr inside the vector. So your vector has pointers to objects which have already been destroyed. This leads to undefined behaviour.
C++ does not keep an object alive just because you are storing a pointer to it. So when you use pointers you must always consider how long the object you are pointing to is going to live.
The simple way to solve this is to use a vector of vectors.
vector<vector<int>> vec; // change here
for(int i=0;i<n;i++) {
int a;
cin>>a;
vector<int> arr(a); // change here
for(int j=0;j<a;j++) {
cin>>arr[j];
}
vec.push_back(arr);
}
Now because you are not using pointers the arr is copied into the vector vec, so it doesn't matter that the original arr has been destroyed.
Try to avoid using pointers. There's very little need for them in C++.
Also the notation int arr[a]; is a variable length array (VLA) because a is a variable not a constant. VLAs are not legal C++ and some compilers would not even compile your code.
I am trying to multiply array elements with 5 but I am getting the error of core dumped and if not, then, it only multiplies 5 with first element.
Here the code:
#include <iostream>
using namespace std;
int ptr_multiply(int[],int);
int main(){
int size;
cout<<"Enter the size of an array ==";
cin>>size;
int arr[size];
cout<<"Enter the elements of array ==";
for (int i=0;i<size;i++){
cin>>arr[i];
}
ptr_multiply(arr,size);
}
int ptr_multiply(int a1[],int s1){
int *ptr;
for (int i=0;s1;i++)
{
*ptr=a1[i];
ptr*=5;
cout<<"The Elements"<<" "<< i <<" "<<" after multiplying 5 is =="<<*ptr;
}}
*ptr=a1[i];
ptr*=5;
First line: You dereference a pointer that points nowhere (it is uninitialized). That is wrong and causes undefined behavior. You try to assign to an int when there is no int.
Second line: You do not dereference the pointer. You multiply the value of the pointer by 5 when you actually want to multiply the int it points to by 5 (remember: there is no int it points to).
You do not need a pointer here:
for (int i=0;i < s1;i++) {
int value = a1[i];
value *= 5;
cout<<"The Elements"<<" "<< i <<" "<<" after multiplying 5 is =="<<value;
}
Also the condition was wrong. It should be i < s1.
Last but not least, dont use C-arrays in C++. They are difficult to use and error-prone. Prefer std::array for fixed size arrays and std::vector for dynamic size. Actually int arr[size]; is not standard C++, see Why aren't variable-length arrays part of the C++ standard?
I'm learning C++ (reasonably confident in JavaScript) and I can't find a C++ equivalent to the JS array.length;. I want a simple way to loop through an array based on the array length?
I have followed a number of tutorials but all seam to require the array length to be manually stated in the for loop (or pushed into the function)?
Below code gets an error and adding the #include causes a compiler error (DEV++).
Is there a C++ or reason why there is no simple call of the array length?
#include <iostream>
using namespace std;
int main () {
int shoppingList [3] = {1, 2, 3};
for (int i=0; i < shoppingList.size(); i++) {
cout << shoppingList[i] << endl;
}
}
For a C-style array like you have you can use range-based for loops:
for (int value : shoppingList)
{
std::cout << value << '\n';
}
As for getting the number of elements from an array, there's a "trick": You know the size of the array itself, and you know the size of each element in the array. That means if you divide the size of the array with the size of each element, you will get the number of elements.
In short
size_t numberElements = sizeof shoppingList / sizeof shoppingList[0];
Important note: This "trick" only works with actual arrays! Once an array have decayed to a pointer (to its first element, which often happens) all you're left with is the pointer. There's no way to get the size of the memory it points to, and this "trick" can not be used.
And as mentioned in my comment, the "best" solution is to use either std::array or std::vector.
Built-in types don’t have member functions with C++. However, there is a non-member std::size(array) function:
for (std::size_t i{}; i != std::size(shoppingList); ++i) {
// ...
}
Note that the counter is usinf std::size_t to match the signedness of the result of std::size(...). Also, do not use the sizeof hack suggested in other answers: it is a trap! The cde will happily compile when passing pointers to it but it will also produce the wrong result.
Of course, the idiomatic way to iterate over a range in C++, including arrays, is (assuming you only need the values and not their position):
for (int val: shoppingList) {
// ...
}
Array doesn't have a direct size() member function, you can find the size of an array like this int size = sizeof(arr)/sizeof(arr[0]); and use it in the for loop.
#include <iostream>
using namespace std;
int main () {
int shoppingList [3] = {1, 2, 3};
int size = sizeof(shoppingList)/sizeof(shoppingList[0]);
for (int i=0; i < size(); i++) {
cout << shoppingList[i] << endl;
}
}
You can do something like, sizeof(A)/sizeof(A[0]). This would give you the total number of elements in the array.
Let's take an example A = {1,2,3,4,5} // all ints
If you evaluate sizeof(A)/sizeof(A[0]), it would give you 5 and as you see, you would not need to manually enter the size of the array.
Hope this helps. :)
#include <iostream>
using namespace std;
int main()
{
A = {1,2,3,4,5};
for (int i = 0; i< sizeof(A)/sizeof(A[0]); i++)
//whatever you want to do with this
return 1;
}
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 5 years ago.
I made a code in which I had to take the size of an array as the user’s input and its elements too and print them.
#include <iostream>
using namespace std;
//Compiler version g++ 6.3.0
int main()
{
int i;
cout<<"\nenter the size of array";
cin>>i;
int n[i];
for(int j=0;j<=i;j++)
{
cout<<"\nn["<<j<<"]=";
cin>>n[j];
}
for(int k=0;k<=i;k++)
{
cout<<endl;
cout<<"\nn["<<k<<"]=";
cout<<n[k];
}
}
Suppose in the following:
The value of i is 3 (according to user’s input).
In the first loop the condition for j is up to <=i where i is the size of array (this shouldn't happen as i begins from 0) due to which the Compiler asks me to input 4 values for the array (n[0], n[1], n[2] and n[3]) but the size of the array is 3 only. How can it store 4 objects?
Change this:
for(int j=0;j<=i;j++)
to this:
for(int j = 0; j < i ; j++)
since array indexing ends at the size of the array minus 1. In your case i - 1.
Likewise, you neeed for(int k=0;k<i;k++).
Your posted code invoked Undefined Behavior, by accessing the array out of bounds.
This:
int n[i];
is a Variable Length Array (VLA), which is not Standard C++, but is supported by some extensions.
and if you compiled with pedantic flag, you would get:
prog.cc: In function 'int main()':
prog.cc:9:9: warning: ISO C++ forbids variable length array 'n' [-Wvla]
int n[i];
^
If you want something like this data structure, then I suggest you use an std::vector instead, which is Standard C++.
By the way, it's not a syntax error or something, but i is usually used as a counter (like you used j), an index if you like. As a result, I would chnage it's name to size, for instance, or something related.
EDIT:
Example with std::vector and variable renaming:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int tmp, n;
cout<<"Input number of elements\n";
cin >> n;
vector<int> v;
for(int i = 0; i < n; ++i)
{
cin >> tmp;
v.push_back(tmp);
}
for(auto number: v)
cout << number << endl;
return 0;
}
You need to check for less than i and not less than equal i. Otherwise it will try to store value for 0,1,2,3 in that case the last object will cause memory corruption. In c++ it will not give you any error even if you try to add 100 members in array of size 3.
It better you read memory management in c++ before jumping into coding.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[5];
for (int i = -1; i < 7; i++)
cout << i << " " << setw(2) << a[i] << endl;
}
So I'm trying to figure out why this won't work. If i take out a[i] it works, but if i leave it in, the for loop goes from -2 until the 1000's which is obviously not right. Is it because a[i] is not bound to the parameters of the for loop (i < 7)? I'm not really sure I understand.
EDIT As many of you have explained it was a matter of uninitializing the array and using bounds outside of the array (e.g -2). It was not something I thought of, nor found when searching for why this was happening.
First, as marcadian pointed out, the array a is not initialized, so values in the array are completely random.
Also, the size of the a array is 5, meaning that you can access between indexes 0 and 4 inclusive.
However, in your loop, you try to access at index -1, and index 6. Attempting to write and read at invalid indexes ( such as -1 and 6, in this case ) is undefinded behavior ( it can crash with a segmentation fault, or corrupt other variables, which can make debugging process very hard ... )
A way to avoid buffer overrun like you did is to use std::array STL container, like this :
std::array<int, 5> a;
//Access elements using the method 'at()', it has bound checking
a.at(0); // 0 is a valid index
//a.at(-1); // -1 is not a valid index, it will crash ( at least in debug mode )
//a.at(6); // 6 is also not a valid index
The std::array STL container does the same things normal array does, but provides useful methods
You're printing out random value in memory because variable a is not initialized. Uninitialized variable has random value in memory. What are you trying to do?
in computers an array is really just a given number of values, starting at array[0], and ending at array[n].
int a[5]; statically allocates space for 5 integers on the stack, starting at a[0], and ending at a[4].
Try this iteration instead:
for (int i = 0; i < 4; i++)
In addition to not initializing the array, a, the indices you would be using to access its elements are out of bounds. C++ uses zero-indexing - you could try, for instance:
int main() {
int a[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
cout << i << " " << a[i] << endl;
}
}
Output would be:
0 1
1 2
2 3
3 4
4 5