1
#include <iostream>
using namespace std;

int a[4];
int main(){
    int b=7;
    cout<<a[b]<<endl;
    return 0;
}

I was trying to access the memory that was not allocated. So I expect the output to be a segmentation fault,instead the output is some garbage value.

Later I increased 'b' to 1000, now the output is 'Segmentation fault'. Is there a specific reason for this kind of behaviour?

I'm using gcc-4.3.2 compiler.

Deanie
  • 2,316
  • 2
  • 19
  • 35
banarun
  • 2,305
  • 2
  • 23
  • 40
  • 3
    its undefined behavior. It could spawn a chorus line of New York sewer rats singing "One" for all you know. See [this answer](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior/4105123#4105123) or a slew of others on this forum for more information. – WhozCraig Apr 08 '13 at 04:45
  • 3
    If your undefined behaviour causes Hot Problems to spew out of my speakers, I'll throw you out the window. – chris Apr 08 '13 at 04:47
  • 1
    What actually happens is the compiler is mailing the segmentation faults to your mother, who knows exactly what they are but loves you too much to let on. – jthill Apr 08 '13 at 05:10

5 Answers5

6

A segmentation fault may be thrown if you are attempting to access a nonexistent memory address (outside process's address space).

a[7] might not be outside its accessible space in this case but a[1000] most certainly is. But even the former will blow up, when you least expect it ;)

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • `a[1000]` might not necessarily be outside the process's virtual memory either (although in this case it is). It depends on how memory allocation is implemented on your OS. – Anthony Apr 08 '13 at 04:58
2

Segmentation faults only occur if you access memory that the operating system has not allowed you to access.

However, you can access memory that, while it isn't what you expect, has been granted to you, for example memory addresses that point earlier or later into the stack. But it's not something that you can rely on being consistent, as how locally stored variables are placed on the stack is decided by the compiler according to arcane optimizations that will vary - from debug to release, as your code changes, etc.

But if you ever see something like, 'why does accessing this variable give me garbage, but always the same garbage?' you're probably unintentionally peeking at parts of the stack you ought not to.

Patashu
  • 21,443
  • 3
  • 45
  • 53
1

It's undefined behaviour so anything can happen, you just can't tell. Check this out (for C++) to know the difference between undefined behaviour, unspecified behaviour, and implementation defined behaviour.

C - **ISO C9899** in Annex J clearly talks about this.

EDIT
C++ - N3485 ISO/IEC in section 1.9.2 ,1.9.3 , 1.9.4 talks about the above behaviors.

Community
  • 1
  • 1
Koushik Shetty
  • 2,146
  • 4
  • 20
  • 31
  • Shouldn't you be quoting the [C++ standard](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf) instead? – Anish Ramaswamy Apr 08 '13 at 05:29
  • @AnishRam The OP tagged it as both **C** and **C++**. in the link i have given for **C++** the reference to standard is already mentioned(so i dint reiterate). But for **C** it was not mentioned so i explicitly mentioned it. – Koushik Shetty Apr 08 '13 at 05:53
  • Regardless of the tag, the code is C++. – Jim Balter Apr 08 '13 at 08:18
0

Out of bounds access is undefined behavior. It can access arbitrary memory in your process space. If the accessed memory is not allocated or not in process address space (may be in kernel) then you program will crash with segmentation fault. In such cases faults will help you identify problem and if lucky will get a crash else it may get unnoticed and will get some bug later in program corrupting memory which is then difficult to track. The probabilty of a[1000] to be un allocated is more and hence the crash. Memory is allocated in pages.

Abhijit-K
  • 3,569
  • 1
  • 23
  • 31
0

Undefined behaviour isn't required to cause segfaults. Consider int i = INT_MAX + 1; for another example of undefined behaviour that doesn't commonly cause a segfault.

autistic
  • 1
  • 3
  • 35
  • 80