Reputation: 13
I'm curious as to why in this code the value of *(ptr + 1) becomes 1 instead of 6 when the program is run.
#include <stdio.h>
int main (void)
{
int *ptr, content = 3;
ptr = &content;
*(ptr + 1) = 6;
*(ptr + 2) = 10;
*(ptr + 3) = 14;
int i = 0;
for (i = 0; i < 4; ++i)
{
printf("The address is %p and the content is %d \n", ptr+i, *(ptr+i));
}
return 0;
}
When run int 6 is modified to a value of 1.
*(ptr + 1) = 6;
Here's the output of the program:
The address is 0x7fff7b400a88 and the content is 3
The address is 0x7fff7b400a8c and the content is 1
The address is 0x7fff7b400a90 and the content is 10
The address is 0x7fff7b400a94 and the content is 14
I ran it in valgrind and no errors show up or anything, also tried googling it maybe I was searching for the wrong thing but had no luck finding a result.
Upvotes: 1
Views: 108
Reputation: 13484
*(ptr + 1) = 6;
*(ptr + 2) = 10;
*(ptr + 3) = 14;
Stack corruption is happening in the above statments, which is an undefined behaviour. Because your program has the rights to access only *(ptr + 0), luckily crash is not happening.
And why *(ptr + 1)
is printing 1
is, its pointing the next variable which is declared in stack. That means its pointing i
. Try to print *(ptr + 1)
before for
loop, it will print 0. And also try to print the address of i
it will be same as (ptr + 1)
.
After int i = 0;
and before for
loop declare two more variables like below, and then run you wont get 10
and 14
also while printing *(ptr + 2)
and *(ptr * 3)
int i = 0;
int x = 2;
int y = 2;
printf("%d", *(ptr + 1)); //this will print 0
for (i = 0; i < 4; ++i)
{
....
}
Fix for your problem is, make content
as array of size 4 like below.
...
int *ptr = NULL;
int content[] = {3, 0, 0, 0};
ptr = content;
...
Upvotes: 0
Reputation: 31972
You are modifying memory that isnt allocated to you, thus this is undefined behavior.
What is likely happening is that the variable i
is allocated to that spot because it is the next use of stack space in the function. And i
is 1 at that point. So the output is 1.
Change
int content = 3;
ptr = &content;
to
int content[10] = {3};
ptr = &content[0]; //or ptr = content; but this may be harder to grasp if you are new to C
(thanks @KeithThompson for fixing the pointer assignment)
If you are just trying out pointer arithmetic. This will work since now you own that entire stack space that is in the array of 10 int
s
Upvotes: 4
Reputation: 755006
Your code is writing to addresses that were not allocated as part of an array and is therefore invoking undefined behaviour. Anything can happen and it is correct.
Most likely what is happening is that the variable i
is stored at 0x7FFF7B400A8C and is modifying what is printed.
Upvotes: 0
Reputation: 95375
You are moving ptr to point to memory that you haven't allocated, and getting whatever the compiler puts there.
Upvotes: 0
Reputation: 1152
Looks like you're writing the variable over memory that's been set aside for i
. Don't know why it matters to you anyway, since you shouldn't write over data that you haven't explicitly allocated.
Upvotes: 1
Reputation: 182865
The program modifies memory through a rogue pointer. Most likely, the pointer winds up pointing to where i
happens to be stored and so it outputs the value of i
at that point.
Fix the bug and the mystery will go away. Understanding buggy programs is much more complicated than understanding correct programs.
Upvotes: 2