Array not printing Properly in C++ [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
When running this program, if the value of n is set to 10 it will print the indices of array a and stored values from a[0] to a[9]. However if I set the value of n to more than 10 the then it only prints the indices of array a from a[0] to a[5] with their stored values. Can someone explain to me why this is happening?
#include <iostream>
using namespace std;
int main()
{
int a[10];
int i, n=11;
for(i=0; i<n; i++) {
a[i]= 5;
}
cout<<"The array is: \n";
for(i=0; i<n; i++)
{
cout<<"a["<<i<<"] = "<<a[i]<<endl;
}
return 0;
}

If you increase the value of n past the size of the array (or equal to it indexing from 0), you are going past the end of the array. And going past the end of the array is undefined behavior. If your program is exhibiting undefined behavior. Anything can happen. See this blog post from Microsoft for more on undefined behavior
If you switch to an std::array instead of a C array and use .at(), then something well defined will happen, you will get an std::out_of_range exception. For more see http://en.cppreference.com/w/cpp/container/array/at and http://en.cppreference.com/w/cpp/container/array

Related

C++ What exactly happened here? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
While doing a program, I came up with an unintended behaviour in dynamic arrays. After I did a few tests, I ended up with this code.
#include <iostream>
unsigned long index;
template<typename T>
class A
{
public:
void Set()
{
T* target = new T[index];
for (unsigned int i = 0; i < index; i++)
{
target[i] = i;
}
this->value_ = target;
index++;
}
T* value_;
};
int main()
{
A<int> test;
test.Set();
std::cout << test.value_[0] << '\n';
test.Set();
std::cout << test.value_[1];
}
Returning the following values:
11104472
11075776
The values change each time I execute the program, however, I thought the output would be "0" and "1". I suppose they are memory addresses, but why is the program returning them and not the expected output?
Just look which values index will have. It is initialized with 0. So in the first test.Set(); you create an 0 length array. To even read it later is wrong and leads to so called undefined behavior. In the second call of set, index will have a value of 1, so you create an array of length 1 and initialize that first element on position 0 correctly with 0, but you then try to print out test.value_[1] which is not the first element but the second. Remember array indices start with 0. So this is also an out of bounds read i.e. a wrong doing punishable by random behavior.

Pointer in c++for scanning an array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
How to scan an array in c++ using pointer?
I have learned c programming .so i tried that way.
include<iostream>
using namespace std;
main()
{
int a[5],*p,i;
p=&a[5];
for(i=0;i<5;i++)
{
cout<<"enter"<<i+1<<endl;
cin>>(p+i);
}
for(i=0;i<5;i++)
{
cout<<*(p+i)<<endl;
}
}
I am expecting to scan using pointer just like in c programming
p=&a[5];
&a[5] is the address to one past the last element of the array. Incrementing this pointer, as well as indirecting through the pointer have undefined behaviour.
What you need is a pointer to the first element. You can use:
p = &a[0]; // this
p = a; // or this
Or you could simply not use p in the first place, and access a[i] directly.
cin>>(p+i);
This is wrong. p+i is a pointer. You cannot extract into an int pointer. You should extract into the integer object instead:
cin >> p[i];
cin >> a[i]; // or without p, as I pointed out above

wrong printing 2D char array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am doing my homework and I declared a 2D array but it's not printing what I want.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char NOTE[7][3] = { {'D','O'} , {'R','E'} , {'M','I'} , {'F','A'} , {'S','O','L'} , {'L','A'} , {'S','I'} };
for (int i = 0; i <7 ; i++)
cout <<setw(10)<< NOTE[i] << "\n";
return 0;
}
I want to get:
DO
RE
MI
FA
SOL
LA
SI
DO
but I got:
DO
RE
MI
FA
SOLLA
LA
SI
DO
NOTE[i] is an array of characters. << NOTE[i] inserts this array into a character stream. The array argument decays to a pointer to the first character.
The documentation says that behaviour is undefined unless the pointer operand points to a null terminated array. The array {'S','O','L'} is not null terminated. Therefore the behaviour of the program is undefined - although it could be argued that the behaviour of iterating across boundaries subarrays of a multidimensional array should be well defined (with the behaviour shown in your program), but strict interpretation of the standard is that doing so is UB.
So, to get the output you want, you either need to 1. null terminate each sub array (and therefore need a larger array), or to 2. print each character individually.

I cannot understand why this code gives a segmentation fault. Please, can anyone tell me where have I allocated a memory which cannot be used [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
#include<cmath>
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
//There were problems in this code. the include wasnt showing up. So i just put them as comments.
using namespace std;
int main()
{
int n, a[n],count[100],temp;
cin>>n;
if(n<100||n>pow(10,6))
return 0;
for(int i=0;i<100;i++)
{
count[i]=0;
}
for(int j=0;j<n;j++)
{
cin>>a[j];
if(a[j]<0||a[j]>=100)
return 0;
}
for(int m=0;m<n;m++)
{
temp=a[m];
count[temp]++;
}
for(int s=0;s<100;s++)
{
cout<<count[s]<<" ";
}
return 0;
}
Whoops!
Looks like you're trying to create a variable sized array (which doesn't exist... kinda) with an undefined variable (which can be anything the system wants it to be).
Use pointers instead, and let the user fill the variable before creating the array:
int n;
std::cin >> n;
int* a = new int[n];
Or use the magic C++ vectors, since you're including them. They offer many more features than plain pointers like dynamic allocation and some newer methods of type-safety, along with multiple in-out methods like LIFO and FIFO:
int n;
std::cin >> n;
std::vector<int> a(n);
Your below statement is wrong:-
int n, a[n],count[100],temp;
cin>>n;
a fix size array declaration must need the size during compilation. In the above declaration n does not have a size during compilation. Hence compiler can not allocate memory from stack.
a

Run-Time Check Failure #3 - The variable 'result' is being used without being initialized [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Having issues with my code... the program compiles, but then it gives the following statement "Run-Time Check Failure #3 - The variable 'result' is being used without being initialized." It then ends the program at that point. Everything I have after trying to run the function is ignored. What should I do?
double result;
for (int i=0; i<nRows; i++)
{
absum.push_back(vector<double>());
for (int j=0; j<nColumns; j++)
{
double temp;
temp = matrixa[i][j]+matrixb[i][j];
absum[i].push_back(temp);
cout << temp << '\t';
}
cout << endl;
}
return result;
At the top of your code you have:
double result;
At the moment it's not initialised to anything at all, so the compiler won't use it. So you need to need to initialise it thus:
double result = 0;
It's also generally good practice to initialise every variable you use in C++, that way you don't get nasty compiler messages, and don't run the risk of returning some random chunk of memory. You always want to start your program from a known state, so if you know that result is 0, then all is good.
C++ is picky about this sometimes, have you tried double result = 0?