1

A stackoverflow is when stack hits the heap and then stops. There are security issues associated with stackoverflow while a inifinite loop just keeps on running.

Someone asked in this in an interview and I answered stackoverflow. Is it correct?

Thom
  • 14,013
  • 25
  • 105
  • 185
Muku
  • 538
  • 4
  • 18
  • @ANS this doesn't seem to be a duplicate to http://stackoverflow.com/questions/214741/what-is-a-stackoverflowerror. OP question is asking which is worse between a SOE and an infinite loop in a program and is a valid question. – Ovidiu Dolha May 16 '17 at 07:12
  • 1
    I don't think it's a duplicate, since the original question was not "What is a StackOverflowError?" but if his answer to the interview question was correct. I would think that his answer that stack overflow is the worse of the two things _is_ correct, but not his explanation. Stack overflow is something you hardly ever intend to produce, while infinite loop can be something intentionally produced in several situations. (Although the correct answer for me is just "that depends..." ;-)) – cyberbrain May 16 '17 at 07:14
  • My bad. reopened – Suresh Atta May 16 '17 at 08:01
  • @cyberbrain What do you mean by "that depends"? can you elaborate? – Muku May 17 '17 at 04:23
  • @MukulSharma: If it was a job interview, probably there is no "totally correct answer" to this question - the interviewer would like to hear your thoughts on the question too. If you answer starts with "that depends..." you can elaborate on both - you can show that you know the terms by explanation of them. And be sure to ask back if the interviewer meant _this_ community here, which is of course far better than an endless loop! ;-) But that all does not belong here as it's not technical stuff. (From time to time I do job interviews, sides of the table change.) – cyberbrain May 18 '17 at 06:44
  • @cyberbrain that's exactly what i did. I don't think he was satisfied with the answer. – Muku May 18 '17 at 09:44

2 Answers2

1

I would tend to agree with you. A stackoverflow can cause problems outside of your immediate shell where an infinite loop just eats up processor time.

This is probably an example of a question with no right answer, though. They probably just wanted to see you process the question and give a well-thought out answer.

Thom
  • 14,013
  • 25
  • 105
  • 185
-2

Security problem can be caused by SO, but it can be caused by anything. Infinite loop tends to cause also memory leak (not always, but often).

Buffer overrun is caused typically not the SO, but by overloaded buffer (tricking the code to overwrite its own return address, in ideal case to what we just sent to the buffer). However:

  • Today there are many defences against buffer overruns (various address space randomizations, compiler support, etc - anyways it works only in machine-level code and today all most used languages are interpreted or tokenizing).
  • The stack can be defended by mapping some unreachable page to its end, resulting that SO causes memory protection error, but no corrupted heap.

Both makes the program not working any more.

The difference is that in the SO, you have a stack trace and you know on the spot what the cause was. It is mostly easy to fix (the typical hotfix is to alter big stack allocations to dynamic, heap allocations, i.e. in C, you convert int x[BIG_BIG_NUMBER] to int *x = malloc(sizeof(int) * BIG_BIG_NUMBER); ...; free(x);.

In infinite loop, you have not this information, furthermore you have no easy way to know if a program is in infinite loop, or it is simply slow. Furthermore, the most important thing: you can easily debug the current state of the program, but you have no retroactive information about its previous states which caused it.

In these reasons, I believe SO is the better. Although it depends a lot on the circumstances. For example, SO caused by a recursive function and not by large stack allocations, can be about so problematic like the infinite loops.

Coderrrr
  • 230
  • 1
  • 16
peterh
  • 11,875
  • 18
  • 85
  • 108