Pointers: Read access violation when trying to dereference pointer by adding a large number


I am trying to explore pointers more,below is one sample piece of code i am trying

This works:

int *arth, art;
art = 100;
arth = &art;
printf("arth address is %p arth+1 value is %d", arth, *(arth + 1));

now if i try to add a large number to arth,i get read access violation

This doesn't:

printf("arth address is %p arth+1 value is %d", arth, *(arth + 1000));


Exception thrown: read access violation. arth was 0xC9C5EFF964. occurred

can some one explain why this works when adding 1 and not when adding 1000

Show source
| c   | pointers   | visual-studio   | pointer-arithmetic   2017-09-05 21:09 5 Answers

Answers to Pointers: Read access violation when trying to dereference pointer by adding a large number ( 5 )

  1. 2017-09-05 21:09

    Because this *(arth + 1) invokes Undefined Behavior!

    Undefined behavior is when a program can act unexpectedly. In your case, it might work in your first code snippet, and in the second. Or, it might do not work at all for both cases, today. Tomorrow it can. In your machine it might work, in mine's..maybe not! Read more about UB.

    You are accessing memory that your pointer shouldn't be accessing.

    When you add 1, you probably are (un)lucky and you access memory that your program owns, thus you get away with it from the police (the OS).

    When you are adding 1000, you go out of the program's segment, causing a segmentation fault, or an access violation (you name it!).



    You act like arch is pointing to an array, and when array[i] is valid, array[i + 1] is valid too (assuming i + 1 is not greater or equal than the array's size).

    However, arch is just pointing to one variable, nothing more, nothing less!

    C allows you to do pointer arithmetics all day long, but you are responsible for the result of this. In your case, both increments are valid, in terms of syntax, etc., but invoke Undefined Behavior, which is a logical error (which results in an invalid runtime, one could say).

  2. 2017-09-05 21:09

    arth is pointing to a single instance of an int. Adding any non-zero value to arth and subsequently dereferencing that new pointer value reads a memory location outside the bounds of the original int. This invokes undefined behavior.

    With undefined behavior, anything can happen. Your program may crash, it may output strange results, or it may appear to work properly. The fact that you didn't crash on the *(arth + 1)) case but did crash on the *(arth + 1000)) is an example of that.

    Just because the program didn't crash doesn't mean it didn't do something wrong.

  3. 2017-09-05 21:09

    Arth is pointing to an integer, but arth+1 is not pointing to anywhere you defined in your code, neither is arth+1000.

    C actually allows you to increment the pointer to one element past the memory allocation so arth+1 is technically allowed, but arth+1000 is not. The behavior for arth+1000 is undefined, so anything can happen. In your case, it was an error.

  4. 2017-09-05 21:09

    *(arth + 1000), is similar to arth[1000] and (arth+1) is equal to arth[1].(arth + 1000) for which you have not allocated or initialised any space in the memory, and hence the compiler will throw the undefined behavior and there is no such allocated data to that location.

  5. 2017-09-07 15:09

    Both are invalid whether it is *(arth + 1) or (arth +1000) since you are accessing memory which is not allocated at all.It may work due to undefined behavior.Run with valgrind and check the valgrind log it will always report invalid read whether it *(arth +1) or *(arth +1000).Always run valgrind when any pointer related code you are adding .

Leave a reply to - Pointers: Read access violation when trying to dereference pointer by adding a large number

◀ Go back