Article 6F80K Why not able to create segmentation fault in second program?

Why not able to create segmentation fault in second program?

by
ajiten
from LinuxQuestions.org on (#6F80K)
Had posted the below question earlier here, where was advised to post it in a separate thread titled: What is memory-garbage and how to avoid it?
Though am confused as never intended to have the memory garbage as the issue, and assume that @NevemTeve wanted me to use the : free() to free the dynamically allocated memory.

However, the intent of my question was different, as asked about why segmentation fault occurred in one program's running, and not in the other; though both have similar pointer assignment.
The first program (buggy.c) has invalid assignment: Code:int *noptr = NULL; which if would replace by : Code:int *noptr; then no error of 'segmentation fault' would occur; that occurs due to no address assigned to: *noptr, hence any value assignment is causing the given error.

Wanted to create a similar error in the second program (bug_c.c) but could not get the same fault by the code line :
Code:char *noptr = 'c';as that now gives error in compilation.
Code:$ g++ -o bug_c -g bug_c.c
bug_c.c: In function int main()':
bug_c.c:6:18: error: invalid conversion from char' to char*' [-fpermissive]
6 | char *noptr = 'c';
| ^~~
| |
| charThe codes for the two files : buggy.c, bug_c.c are given belwo:
buggy.c
Code:#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
int *ptr = (int *)malloc(sizeof(int)); //not a memory garbage problem, as this line is not an issue, as can have another version with this line removed, and the for-loop removed; and the entire body of the main() replaced by:
//int i = 5; int * noptr = NULL; *noptr = i;
// still the same error of segmentation fault would occur.
// $ ./buggy
// Segmentation fault (core dumped)

int *noptr = NULL;

for (int i=0; i<10; i++) {
if (i == 5) {
*noptr = i;
}
else {
*ptr = i;
}
printf("i is %d\n", i);
}
}It gives run-time error of Segmentation fault; after compiling using the command: $ g++ -o buggy -g -Wall buggy.c.
Code:$ ./buggy
i is 0
i is 1
i is 2
i is 3
i is 4
Segmentation fault (core dumped)---------------------------------------------------------------
bug_c.c
Code:#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
//char *noptr = 'c'; //causes compilation error,
/* $ g++ -o bug_c -g bug_c.c
bug_c.c: In function int main()':
bug_c.c:6:18: error: invalid conversion from char' to char*' [-fpermissive]
6 | char *noptr = 'c';
| ^~~
| |
| char

though a similar assignment in buggy.c, compiles */

char *noptr;
char i = '5';
*noptr = i;
printf("i: %c, *noptr:%c", i, *noptr);
}Due to the modification of the commented code-line, the program executes smoothly:
Code:$ ./bug_c
i: 5, *noptr:5
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments