Why does the second print function doesnt print any outcome? ( coded in C ) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

23rd Apr 2022, 12:16 PM
blueshipswims
blueshipswims - avatar
14 Answers
+ 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.
23rd Apr 2022, 12:39 PM
Ipang
+ 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.
23rd Apr 2022, 1:10 PM
Ipang
+ 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).
23rd Apr 2022, 3:26 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 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.
23rd Apr 2022, 3:41 PM
Ipang
+ 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.
23rd Apr 2022, 12:57 PM
Ipang
+ 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.
23rd Apr 2022, 1:14 PM
Ipang
+ 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.
23rd Apr 2022, 1:37 PM
William Owens
William Owens - avatar
+ 2
Ipang Awesome point about flagged insecure and awesome rebuttal. Thanks again my friend.
23rd Apr 2022, 4:43 PM
William Owens
William Owens - avatar
+ 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
23rd Apr 2022, 12:51 PM
blueshipswims
blueshipswims - avatar
+ 1
Ipang oh got it, but why does the system doens't allow the values to be assigned, is there any problem
23rd Apr 2022, 12:59 PM
blueshipswims
blueshipswims - avatar
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?
24th Apr 2022, 7:36 AM
blueshipswims
blueshipswims - avatar
0
Garbage value a sometimes it will segfault
24th Apr 2022, 2:03 PM
William Owens
William Owens - avatar
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?
24th Apr 2022, 4:44 PM
blueshipswims
blueshipswims - avatar
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.
24th Apr 2022, 9:25 PM
William Owens
William Owens - avatar