What makes you so scared of C/C + +?
The C + + language is so powerful and addictive, but the obscure syntax and many programming pitfalls make people scalp numb.
Segment Error
The most common error we encounter is a segment error, the following is a classic segment error, you never met? Kiss, that's impossible ~
Well, most of these errors are caused by pointers and see what our code says:
#include "stdio.h"#include "string.h"#include "stdlib.h"voidFunc1 (Char* * Dest,Char* SRC,intN) {(*dest) = (Char*)malloc(sizeof(Char) *n);strcpy(*DEST,SRC);}intMainintargcChar* * args) {Char* * p = NULL;CharStr[] ="Foreach_break";intLen =sizeof(str);printf("%d\n", Len); Func1 (P,str,len);printf("%s\n", *p); Free(p); p = NULL;}
This is a trap code and this article will focus on the problem of segment errors.
I'm not going to tell you how to quickly locate a segment error, because that will keep you away from the truth, and if you plan to conquer a section error to better understand the C + + language, then you need to look at the following content.
look from a program that can run
Here we have a version that we can run
#include "stdio.h"#include "string.h"#include "stdlib.h"voidFunc1 (Char* * Dest,Char* SRC,intN) {(*dest) = (Char*)malloc(sizeof(Char) *n);strcpy(*DEST,SRC);}intMainintargcChar* * args) {Char* p = NULL;CharStr[] ="Foreach_break";intLen =sizeof(str);printf("%d\n", Len); Func1 (&p,str,len);printf("%s\n", p); Free(p); p = NULL;}
It runs the following results:
Why can it run?
Note the comparison with the wrong code, what is different:
Error code:
char ** p = NULL;func1(p,str,len);
Correct code:
char * p = NULL;func1(&p,str,len);
Here if you have a certain C-language foundation, you will certainly feel
Working with one char * p
pointer &p
will get one char ** p
, which seems to be no different from the correct code!
So why is a segment error, one is normal operation?
See what the assembly says.
We will begin to disassemble two pieces of code and find out the cause of the segment error:
We main
look at the 1th line of the function:
Wild Hands
The so-called wild pointer is the address where the pointer is stored, but the pointer to the address is ambiguous.
0 Hands
The so-called 0 pointer is that this pointer points to the virtual address 0x0.
Segment Error Analysis
You already know that the pointers in the two-part code p
are already 0 pointers.
Parameter passing
Let's look func1
at the beginning of the function:
Error code
Correct code
As you might have guessed, in the wrong code, we called func1
the function and dest
passed in a 0-pointer address to the parameter 0x0
.
Just like this:func1(p,str,len);
This doesn't have anything to do with the stack.
This error is not related to the stack.
The pointer to the pointer in the error code is simply char ** p = NULL
pointing 0x0
, so *p
the value is 0x0
, then you pass *p
it 0x0
.
This is the reason for the error, which 0x0
is in a reserved or unused space called the user process space, and this space is protected and you cannot access it.
We can also try to access the 0x0
address here and get an unreachable hint:
Each application is started and an instance of the operating system (process) is raised.
The process has its own virtual address space (VMA).
This is over?
Why is your C/C + + program not working? Secret segmentation Fault (core dumped) (1)