-5
#include<stdio.h>
#include <stdlib.h>
int *ip_range ;

int main()
{
  ip_range = (int *) malloc(1);
  ip_range[0]=2;
  ip_range[10]=2;
  ip_range[20]=2;
  ip_range[33787]=12444;
  printf("%d\n", ip_range[33787]);

}

I have malloc just 1 block then why it is accessible till 33787 and generating core on 33788.

Vikash Kumar
  • 31
  • 11
  • Acessing memory which is not your's is undefined behaviour. Anything can happen. – Jabberwocky Jul 20 '18 at 06:07
  • `Allocating one block` means reserving one block of memory for the program. It does not make the rest of the memory *inaccessible*. You can still read/write other locations. You get the error when you try accessing some protected location, like some memory occupied by another process. – Susmit Agrawal Jul 20 '18 at 06:09
  • I have tried this block of code on several machines with different Configuration,,and It failed at 33788 only – Vikash Kumar Jul 20 '18 at 06:10
  • @VikashKumar it's __undefined behaviour__, google that term. – Jabberwocky Jul 20 '18 at 06:12
  • @SusmitAgrawal `You can still read/write other locations.` - that's not always correct. – babon Jul 20 '18 at 06:13
  • I guess PC-Lint will cry – Mike Jul 20 '18 at 06:13
  • @ Fantastic Mr Fox :I am aware of array out of bound behaviour but my question is It is not showing Undefined behaviour when I tried this block of code in several machines – Vikash Kumar Jul 20 '18 at 06:23

2 Answers2

1

You are writing to memory which you do now own i.e. was not handed back by malloc , calloc or realloc. This results in undefined behaviour. Your program can do anything at all, including not producing any error message or core dump.

babon
  • 3,615
  • 2
  • 20
  • 20
  • what will be the ideal behaviour? and better approach to understand this type of results – Vikash Kumar Jul 20 '18 at 06:13
  • @VikashKumar _What will be the ideal behaviour?_: what do you mean? Please elaborate. – Jabberwocky Jul 20 '18 at 06:14
  • @VikashKumar There *is* no ideal behaviour. Have a look at this: http://www.catb.org/jargon/html/N/nasal-demons.html – babon Jul 20 '18 at 06:16
  • As I have stated In my comments, I have tried this code in various machine with different Configurations ,But failed at 33788 only. Then How we can say It is undefined Behaviour? – Vikash Kumar Jul 20 '18 at 06:18
  • @VikashKumar try on some other architecture and the outcome will be different. Accessing memory that is not your's is undefined behaviour by definition. For example on my platform I get a segfault at 18000. – Jabberwocky Jul 20 '18 at 06:20
  • thanks guys for awesome discussion – Vikash Kumar Jul 20 '18 at 06:26
  • 1
    @VikashKumar We can say this is UB, because the C standard says so. Have a look at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf article `J.2 Undefined Behaviour` – babon Jul 20 '18 at 06:27
0

It is undefined behaviour because you are trying to access memory out of bound. C does not check memory bound.

According to cppreference :

undefined behavior - there are no restrictions on the behavior of the program. Examples of undefined behavior are memory accesses outside of array bounds, signed integer overflow, null pointer dereference, modification of the same scalar more than once in an expression without sequence points, access to an object through a pointer of a different type, etc.

msc
  • 33,420
  • 29
  • 119
  • 214