Why is your C/C + + program not working? Secret segmentation Fault (1)

Source: Internet
Author: User

What makes you so scared of C/C + +?

An obscure grammar? Or the lack of a good IDE?
I guess that's not a problem, most likely a mistake like this:

Segment Error (segmentation fault)

This is a mistake that the novice can not avoid, but also frequently encountered by the veteran to avoid.
This article, attempts to briefly analyze a section of the program that will cause this error, bring some inspiration.

Look at two copies of the code first, one is wrong.

Error code
#include "string.h"#include <stdlib.h>#include <stdio.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;}
Correct code
#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;}

The code is intended to come from an issue where a Huffman tree cannot run in a technical question.
Of course, I stripped off most of the parts of the Huffman and changed them slightly.

They are the biggest differences:
Error code:

char ** p = NULL;func1(p,str,len);

Correct code:

char * p = NULL;func1(&p,str,len);

You might be surprised that you see the line in the "correct" code that actually commented:

//p = NULL;
Parameter passing

At the same time, some people may feel that pointers char ** p to function Func1 pass *p , and pointers to the function of the char * p transfer is func1 &p no different ah?

Well. This idea is also very inertia, because C does not have a function declaration like this:
void func(int &)

At the same time, char * p &p do you get one for the operation char ** ?

Run the program

So, let's see what the program says.

If you have a lot of mistakes, I hope you will look closely at what the above figure says.

Wild Hands

The so-called wild Pointer, is a very wild pointer, you do not know which address it points to, and do not know whether the value of this address will be wrong, but the wild pointer is also a pointer, there is a memory address to store it.

In the correct code, the p address where the pointer is stored is 0x7fffffffddc0 ;
In the wrong code, the p address where the pointer is stored is 0x7fffffffdd78 ;

0 Hands

The so-called 0 pointer is a pointer to 0x0. Is the value of this 0x0 error? You think about it.

Floating hands

Are you confused about commenting in the correct program p = NULL ?
In fact, it doesn't matter, depending on how the pointer p is used in subsequent code.

free(p);

The execution of this code releases the memory address pointed to by the pointer and p returns it to the operating system.
Of course, the premise is that the address does mean that you have access to it.

p = NULL;

The execution of this code is to let the pointer p point 0x0 back to the null pointer.

Although the memory it points to has been freed, it also points to that address.

This is the hover pointer, the address pointed to is not available to point, it does not really mean.

Since our function is about to be main executed, the address that holds the null pointer is freed after it returns p .

Because p the pointer is main a temporary variable for a function.

So we can comment out without concern p = NULL .

Memory leaks

In addition, if free(p) not executed, and executed first p = NULL , then p the original point to the memory space may not be properly freed, if no other reference to the address, the block address is forgotten there, and can not be recycled.

This is called Memory leak (Leak).

Keep looking at the program .

Now, let's take a look at func1 the function invocation.

First, it's the C code:

Then there is a comparison between the two pieces of code:

You should have seen the difference.

The parameters of the wrong code are dest passed in 0x0 .

Followed by execution:

(*dest) = (char*)malloc(sizeof(char)*n);

This code is going to (*dest) happen with a segment error.

The reason for this is that char ** p = NULL it p becomes a 0 pointer, which is *p equivalent to the 0x0 value of this address.

When the application starts, the operating system establishes a process, which has its own independent address space called the virtual address space.

0x0In this space, it cannot be accessed.

Attempting to access a space that cannot be accessed will cause a segment error.

Summary

Part of the error, we have explored.
Now do you know the meaning of the following two actions?

/* p指向的地址,对其取值,如果这个地址有东西,也有权取,没问题.*/char ** p -> *p; (1)/* 存放p的地址,或者指向p的引用,这个地址必然有东西,所以没问题*/char * p -> &p   (2);

The end of this article.

Why is your C/C + + program not working? Secret segmentation Fault (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.