+ 1
Why does the second print function doesnt print any outcome? ( coded in C )
#include <stdio.h> int main() { int a = 3; int *ptr; ptr = &a; printf("%u \n", *ptr); ptr++; *ptr = 5; *ptr += 5; printf("%u \n", *ptr); return 0; }
14 Antworten
+ 6
Your code triggered segmentation fault by trying to dereference an address beyond what your program was allowed for.
That happens after you increment the pointer's referred address
ptr++;
And then attempted to modify a value on the new address, while your program has no access right to use that address.
*ptr = 5;
*ptr += 5;
This prematurely ends the program, and thus you see no output from 2nd printf() call.
+ 3
In the beginning we have a pointer named <ptr> and later it was asssigned an address (address of variable <a>).
This means <ptr> is only safe to be used when it refers to address of <a>. If we do pointer arithmetics on it, then we *should be* sure the new address assigned to <ptr> was an address we can access.
For example, if <ptr> referred to an array of 5 elements in the beginning. Then <ptr> will refer to address of first array element.
If we do ptr++; <ptr> will advance to address of the next array element
If we do ptr--; <ptr> will step back to address of the previous array element.
Basically, we the coder are fully responsible to assure that our code accesses and/or modifies data on addresses that our code was allowed for.
+ 3
William Owens, I believe Ipang was talking about a protected memory space for the process, so that different processes cannot interfere with each other. And that, I believe, is correct (modulo shared resources and privileged processes).
+ 3
William Owens,
Thanks, but if you don't mind, allow me to clarify what I was trying to say ...
System is responsible to make sure no program gets access to read or more strictly write other programs' memory, causing problems in doing so. Unlike in the old days, modern O/S should not allow arbitrary memory address usage, at least not as easily as it was.
It is true that in C language programmers are fully responsible for memory allocation & deallocation. But that fact does not mean a program should be allowed a right to access and/or modify other programs' memory.
A system that still allows any process a full access to other processes' memory should be flagged insecure.
+ 2
Yes, ptr++; changes the address referred to by <ptr> to the next memory block. But your program is not allowed any access to that address.
So the program crashes when it tries to assign value to that address, because the big boss (system) sees it as an access violation attempt and must be stopped.
+ 2
System manages memories for programs, and sets boundaries, which area was allocated for which program. Restriction is applied and an attempt to access/modify data on arbitrary address is not allowed to prevent one program from messing around with other program's memory, which can cause problems.
+ 2
Ipang you always have good comments. Your very helpful to those who are trying to learn. You stated "system manages memory for programs and sets boundaries." I would caution in C this is a dangerous statement as it is the programmers responsibility to protect the memory:
int main(void){
int num[2] = {1,2};
for(int i = 0; i < 3; i++) printf("%i\n", num[i]);
}
bad things can happen.
You will really enjoy this video.
https://www.youtube.com/watch?v=7mKfWrNQcj0
Shows how memory can be our enemy using strings.
+ 2
Ipang Awesome point about flagged insecure and awesome rebuttal. Thanks again my friend.
+ 1
Ipang can you explain it in more simple language.
Also this is what i was thinking while writing the program " ptr++ changes the stored address and points to a different block which doesn't have any value inside it. Now when i give it a value by *ptr=5 , why doesn't it take the value
+ 1
Ipang oh got it, but why does the system doens't allow the values to be assigned, is there any problem
0
William Owens
int main(void){
int num[2] = {1,2};
for(int i = 0; i < 3; i++) printf("%i\n", num[i]);
}
why does this return
1
2
2
why the last one is two?
0
Garbage value a sometimes it will segfault
0
Ipang can you explain bro
int main(void){
int num[2] = {1,2};
for(int i = 0; i < 3; i++) printf("%i\n", num[i]);
}
why does this return
1
2
2
why the last one is two?
0
Anirban The num array holds two memory locations that are of type int size let's say 1000 and 1003 if int is 16 bits.
[1000] [1001] [1002] [1003]
[. 1 ] [. 2. ]
When we go through the for statement staring at 0 we first look at num[0] which is the first address of the num array at 1000 the value is 1
Next i changes to 1 still less then 3 so we go to num[1] which is the address of the second location at 1002 of the array which is a value of 2.
Next i goes to 2 still less than 3 so now num[2] which would be at 1004 would be the third location in the array. Nothing tells the for to stop accept the condition i less than 3 and nothing tells the for statement the end of the array has been reached. So it goes to where the third location would be and reads the bits. In this case it read bits that would equate to the integer value of two. If there would have been something the program should not Have touched there it would have errored with a segfault.
Hope that helps.