Arithmetic exception with rand() and mod [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 8 years ago.
Improve this question
Twice now my server has crashed after several days of running on this line.
int randomValue = rand() % m_list.size();
where m_list is
std::list<int> m_list;
The crash is
Program terminated with signal 8, Arithmetic exception.
getting the size of a list should be guaranteed to not be negative. What could cause this crash? Can something with rand be the cause? I seed rand at the start of my server with
srand(time(NULL));
Any tips are appreciated!

I don't have that much information about the situation, but does the list have anything in it? If not, you'd be dividing by zero, and that would explain everything.
So, first step is to make sure that m_list is not zero.
If it is, perhaps you could check to make sure that the size of the list is not zero before performing the operation.

Related

The inner loop only runs once [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 2 years ago.
Improve this question
I am curious as though, why when I use an array in for-loop in the given manner below, the inner loop only runs once.
Thank you for your reply.
#include<iostream>
using namespace std;
int main(){
int a[2]={0};
a[0]=5;
for(a[0];a[0]<10;a[0]+=1){
for(a[1];a[1]<10;a[1]+=1){
cout<<a[0]<<" "<<a[1]<<endl;
}
}
}
You are never resetting a[1], so once it hits 10 the first time through the inner loop, it will never execute again. Modify to for(a[1]=0;a[1]<10;a[1]+=1){

Loop ignores limits: How can I handle this corrupt c++ program? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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
When I execute my program it doesn't terminate. I haven't changed anything and it has been working before. There is clearly something wrong but I don't understand what and why.
In the screenshot you can see the line
for ( size_t n = 0; n <= (size_t)maxState; n++ ) nodes.push_back(nullptr);
You can also see that maxState is -1, so (size_t)maxState should be 0. On the right side of the image you can see the values for n and the size of the nodes vector.
I've taken the screenshot after pausing execution. When I resume it doesn't terminate. I've read some things about heap corruption, but without working solutions. Also I'm not sure if heap corruption is the real cause for this strange effect.
I already tried to clean and rebuild.
Don't mess with size_t here. You haven't mentioned the actual type of maxState, but if its value is -1, then it's a signed type. So use a signed index variable:
for (int i = 0; i < maxState; ++i)
This will execute zero times when the value of maxState is negative.

python: check if all elements the same in an list [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 6 years ago.
Improve this question
I am using the following code to check if all the elements in a list are the same:
def sameItem(myList):
return all(x==myList[0] for x in myList)
However, in my test case:
myL1 = ['dog','cat','dog']
sameItem(myL1)
returns True. Shouldn't it be False? Or did I have a bug in the sameItem() function?
Also, I am using Jupyter Notebook, could it cause any problem is this scenario?
Thanks!
Your method should be correct and works for me. As an alternative, you can try this method to double check, which is a one line that does the same thing
return myList[1:] == myList[:-1]

recursive function core dumped [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 7 years ago.
Improve this question
I tried to compile the following program
void rec(int n)
{
if(n>0)
{
rec(n-1)
print ("n");
}
}
Here n value is a big no. like 1000000.
o/p is : segmentation fault (core dumped)
Can anyone explain what exactly happens here ?
Thanks.
For programs compiled with GCC with default settings stack size is about 2 megabytes. So you are limited in recursive calls by your stack size, because every not-tail recursion function call reduces free stack memory. Thats why you will get stack overflow when n is big number.
You missed a semicolon and i guess that you want to print the number n and not
n times the letter n. Here is your fixed code.
void rec(int n){
if(n>0){
rec(n-1);
printf("%d\n",n);
}
}
To get back to your problem. Recursive function have the problem that each call creates a new stack. You can imagine that with each call you go deeper and deeper and are only climbing back up at the very end.
The number of stacks is limited, so the program will eventually crash if you increase the number of recursive calls.

C++ pow(400,-9) is giving wrong answer [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 8 years ago.
Improve this question
double testpower;
testpower = pow(400,-9);
testpower giving me 3.8146972656250003e-024 which is different calculator output of 4E-7
Anyone have any idea why??
calculator output of 4E-7
You entered the wrong calculation into your calculator.
You entered 400×10-9, instead of 400-9.
These are absolutely not the same thing!
The C++ program is correct: pow(400, -9) calculates 400-9, which is approximately 3.815×10-24.
Here is some further reading for you:
http://en.wikipedia.org/wiki/Scientific_notation#E_notation
4E-7 seems like you accidentally input 400 * 10^-9 or 400E-9.
You're looking for 400^-9, which should give 3.8146972656250003e-024.
The result you are getting 3.8146972656250003e-024 is completely correct. Maybe your calculator does not have that precission and that is why you are getting that error. Try to do 1/400^9.
I just tested 400^(-9) on the Windows calculator tool and I got the same output as your program. I think the program is fine, it may be your manual calculation that is the problem here.