Why not able to create segmentation fault in second program?
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; Code:
int *noptr; 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'; Code:
$ g++ -o bug_c -g bug_c.c buggy.c Code:
#include <stdio.h> Code:
$ ./buggy bug_c.c Code:
#include <stdio.h> Code:
$ ./bug_c |
Code:
int *noptr; Also you seem to think you have a right to get a Segmentation Fault whenever you have some bug. That is a false assumption. |
Quote:
A pointer, is like your finger, point it somewhere. Where it is pointing is the value. Since you did not initialize the pointer, it's pointing to some arbitrary memory location, where "now" that has a value of '5'. The problem is, the memory the pointer is located in probably doesn't get changed much unless you run a bunch more programs. The location that pointer, points to, also has that same fortune. So you can run this a lot and still see '5'. That doesn't mean it is correct. The fact that you declared a pointer variable reserves space for it on the stack. The contents of the pointer itself are random unless you initialize it by assigning a valid address for it to point to. Above where you initialized it to NULL is actually valid in some way. Just that the finger is pointing to 0x00000000 which is an illegal address to try to modify, hence why you get the segmentation violation. What instead you need is a variable to actually point to. Code:
int value; While your second code doesn't always cause a segment violation, it is far worse, it is modifying memory it has no idea what program owns it, Back to your finger as a pointer, say you're picking people for a team. If you initialize your finger/pointer, you point to someone you want on your team, and it works correctly. If you initialize your finger to point to nowhere, or an invalid address, your hand locks up saying, "I don't know what to do!" If you initialize you finger/pointer to a random, but fortunately valid address, when you select your team member, you may get "bunny rabbit", "cloud", something valid, but random. Quote:
Code:
char myChar = 'c'; |
Quote:
|
Request details: which was the first word you didn't understand?
|
Quote:
That's just random luck. ================================ In fact, you also stated a similar statement as your second line, though in the first line you referred to a different area of 'garbage collection'. --------------------------------- Want to add that now for the below program, am able to compile, with only a warning: Code:
warning: initialization of ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion] Code:
#include <stdio.h> |
you still do not understand what does pointer [type] mean in c. First you must to learn that (not to speak about the syntax of the language). Next you need to understand what is a null pointer and what is a valid pointer. What is an initialized and an uninitialized variable. And after that we can discuss what does segmentation fault mean.
pointers always store a memory location, where the given variable is stored. https://www.javatpoint.com/c-pointers https://en.wikipedia.org/wiki/Uninitialized_variable probably this: https://stackoverflow.com/questions/...t-have-a-value https://www.geeksforgeeks.org/segmentation-fault-c-cpp/ |
> Want to add that now for the below program,
Want to add what? Random luck? Undefined behavior? > am able to compile, with only a warning That's because variable initialization and assignment are different things, even if they look alike. Code:
char somechar= 'X'; |
Do you not know the difference between a null pointer and an uninitialized pointer?
EDIT: Okay, I'll explain it. Memory is a column in Excel. Nothing is at row zero. A memory address is a row number. Some things you should already know are:
So this simply stores the the value zero in the pointer variable: Code:
int *noptr = NULL; Code:
*noptr = i If you do this, however: Code:
int* nopr And then when you do this: Code:
*nopr = i Code:
~/Documents/segfault via C v13.2.1-gcc When you got that warning, you should really have fixed it before continuing. Unfortunately, when I try to explain background basics like this, the response I get is usually that I'm going "off topic" (I have gotten this reaction from two different individuals). I assume this poster will act more maturely than some others have. |
I concur with the advises of NevemTeve and pan64 to review as many references about C pointers as you can tolerate.
I will also add that "pointers are not easy", and I would also say that the syntax allowances for the C language help to keep it confusing. It is very possible to write code that has no compilation errors or warnings, but which still has a bug. |
Are you familiar with the phrase “undefined behavior”?
|
All times are GMT -5. The time now is 01:47 AM. |