GDB Debug &C Language pointer Advanced Programming (5.7) __ Programming

Source: Internet
Author: User
Tags define local

Introduction to GCC compilers:

I. Understanding of basic knowledge

GCC is a compiler under Linux developed by Gun's father, Stallman. The full GNU C complier

(GNU C language compiler). This is an early version of the idea that in the early days it was just used to compile our C

Language, and can then be compiled by multiple of our multiple languages. For example, C,C++,JAVA,OBJECTC and so on.

GCC is a super compiler that can compile executable programs on a variety of hardware platforms, and its execution efficiency is 20%~30% higher than average compiler efficiency.

Second, GCC supported the source code format

GCC is a cross platform compiler, currently supports almost all mainstream CPU processor platform, it can complete from C, C + +, objective C and other source files to run on the specific CPU hardware to the target code conversion, the current compiler can support the format of the source program is as follows.

Suffix Format description

. C C Language Program

. cc/cpp C + + programs

. M Object-c's original program

. I pre-processed C language Program

The original program of the. S assembly language

. O Target File

. a/.so Compiled Library files

Three, GCC compile process analysis

The students who have compiled the C language with GCC should all know that when we compile the executable with GCC, it looks as if it was done just by compiling this step. The detailed process is shown below.

hello.c

Run Result:

The steps above seem very simple. However, in fact, the process of converting our GCC compiler tools from C to executables is not simply a compilation process. Instead, go through the following steps.

Preprocessing------> Compilation------> Assembly--------> Links

Preprocessing phase: is to our source code before compiling, to our header file or macro definition to parse.

Compile: This phase of the attention is to check the code, whether there are syntax errors and so on. To determine the actual work to be done by the code. After the check is correct, the code is translated into assembly code.

Assembly: This stage is to convert our already generated assembly code into our target file (binary).

Link: Generate executable code. Link to the required library file. The normal C program defaults to/usr/lib to find the link in the library.

Four, parameter analysis and principle detailed

We have just explained the knowledge from the point of view of the principle to explain to us, our GCC compile process. Maybe everyone thinks it's a more abstract. Next, let's look at the actual code demos. First, you need to know a few options.

<1> Common compilation options

-e pretreatment selection, pretreatment operation of the use of

-S compile option to compile the preprocessed code into assembly code

-C assembly option to generate our target file

-o Specifies the compiled content, specifying the file to save to the specified output.

-G to generate the symbolic information needed for GDB debugging

<2> Code Analysis

Pretreatment

Compile

Assembly

Link

Next, let's take a look at our GDB debugging

Introduction to GDB Debugging

Classification of <1>c language errors

When we write code, it is always necessary to write typos or to run into some wrong problems. So how do we solve it? First, let's take a look at the types of errors we're familiar with.

(1) C Syntax error

Error message: The nth line in the file source.c has a syntax error (Syntex errror)

Grammatical errors for us, because we are unfamiliar with C language, or typing speed too fast, inadvertently produced. This error general compiler will come to help us detect it. Comparatively speaking, it is easier to solve.

(2) header file error

Error message: Cannot find header file Head.h (Can not found include file head.h)

In general, the function we call is not added to the header file. For example, you wrote printf but did not invoke the Stdio.h header file. Or the header file was written incorrectly. This is also relatively easy to solve. The compiler will generally help to find out.

(3) Logic error

This kind of mistake is often encountered by everyone. Generally, the compiler compiles and generates the executable, but after we execute it, we often find that the result of execution is not what we want. This kind of mistake is generally difficult to find out.

<2>GDB debugging

For logic errors, our clever programmers developed a tool called the GDB Debugging tool. Similar to the Debug Debugging tool in VC.

Detailed usage detailed picture gdb.jpg gdb1.jpg

GDB Debug Segment Error:

1. Show the core files

Ulimit-c Unlimited to set the core file to infinity

2, compile the program gcc-g hello.c

3, GDB debugging it gdb a.out core

The debugging commands are as follows:

L LIST The contents of the source file to facilitate setting breakpoints

Start debugging

N (Next) Next

S (step) stepping, skipping function

BT (backtrace) view the stack frame of a function call

F (Frame) 1 Select stack frame 1th

I (Info) Locals view the local variable of the 1th stack frame (that is, the local variable of the function), that is, the local variable of the main function

Attached: What is the stack frame, logically, the stack frame is a function of the implementation of the environment:

function arguments, local variables of functions, where functions are returned after execution, and so on.

Each call to each function has its own independent stack frame, which maintains all the information it needs.

P (print) sum view the value of the sum variable

Finish jump out of the current function, back to main function

Set Var sum=0 the value of modifying the variable sum is 0

B (Break) 9 setting a breakpoint parameter on line 9th can also be a function name; b fun_name

I breakpoints show breakpoints that have been set

or Info b

C (Continue) indicates continuous running, jumps to the next breakpoint, and runs until the end if there is no next breakpoint.

Delete Breakpoints 2 Remove Breakpoint 2

or D 2

Delete breakpoints Remove all breakpoints

Display sum shows the value of sum every time it stops

Info display view all trace variables

Undisplay Number: Cancels tracking of this variable

R re-executing continuously from the program

Second, C language pointer detailed

(1) When we write the program, the most things we do is what we do.

1. Data--->ram

2. Ram--->data

(2) Method of providing memory allocation in C language

<1> Define Variables

A. Define local variables---> function call ends, memory is released

B. Defining global Variables---> when the program call ends, our program is released.

<2> Manual Assignment

(3) The distribution of the C language runtime in memory

See the blackboard "Memory distribution Diagram"

The allocation of variables in <3>c language

(1) Ordinary variable

Role: Save Our data

(2) pointer variable

Role: Save Our Address

Methods of defining variables in <4>c language

<1> General variables

Storage type data type variable name;

<2> pointer variables

Storage type Data type * variable name;

Storage type: Auto, static, Resigter,extern

Data type:

1. Basic data types

Integer: Small integer or character type (char), short int (shorter), int (int.), Long

Floating-point type: float,double

2. Construct Data types

Arrays, structs, shared bodies, enumerations

Thinking: What is the difference between our basic types and our constructed types?

Our basic data type size is fixed, and our construction type knows the size is determined by our programmers themselves.

(5) Memory reading and writing in C language

<1> ID symbol (variable name) through memory

int data;

data = 50; Write operations

printf ("data =%d\n", data); Read operations

<2> read and write via memory address

&

int data;

The address of data is &data

Write operations

* (&data) = 50;

printf ("data =%d\n", * (&data)); Read operations


Practice:

1, define two variables int a = 20,int B = 10;

Defines two pointers p,q pointing to a,b respectively

Requires the use of pointer variables to achieve C language program addition, subtraction, multiplication, except

#include <stdio.h>

int main ()
{
	int a = 20,b = ten;
	int sum = 0;
	int *p = &a;
	int *q = &b;

	sum = *p + *q;
	printf ("*p + *q =%d\n", sum);

	sum = *p-*q;
	printf ("*p-*q =%d\n", sum);

	sum = *p * *Q;
	printf ("*p * *q =%d\n", sum);

	sum = *p/*q;
	printf ("*p/*q =%d\n", sum);
	return 0;
}

2, define two variables int a = 10,int B = 20;

Two pointer p,q are defined to point to a,b, which requires p,q to realize the exchange of a,b value.

Let's look at a few ways.

#include <stdio.h>

int main ()
{
	int a = 10,b =;
	int *p = &a;
	int *q = &b;
	int temp = 0;

	printf ("A:%d B:%d\n", a,b);

	temp = *p;
	*p = *q;
	*q = temp;

	printf ("A:%d B:%d\n", a,b);

	A = ten;
	b =;

	*p ^= *q; *p = *p ^ *q
	*q ^= *p;//*q = *q ^ *p ===> *q = *q ^ (*p ^ *q) ==>*q = *p;
	*p ^= *q; *p = *p ^ *q ===> *p = *p ^ (*q  ^ *p) ==>*p = *q;
	printf ("A:%d B:%d\n", a,b);


	A = ten;
	b =;
	*p = *p-*q;
	*q = *p + *q; *q = (*p-*q) + *q ===>*q = *p *p = *q
	-*p;//*p = *p-(*p-*q) ===>*p = *q; 
	printf ("A:%d B:%d\n", a,b);
	
	return 0;
}

Classification of special pointers in C language

1. Wild pointer: Pointer to "junk" memory

The reason for the wild pointer formation is that it does not point to a valid memory.

2. Null pointer/0/null

A null pointer is a pointer that is assigned a value of 0. The Linux system does not allow us to access ordinary users.

3. void pointer

void literal means "no type", void * is "no type pointer" and void * can point to any type of data.

Often used to receive pointers of any type.


About A and &a:

First, the preface

About A and &a, there are a lot of students, not very understanding of its meaning, in order to help students understand, I wrote this document, to help students deepen their understanding. Hope to help you understand the array.

Second, detailed

<1>a Address Detailed

Let's take a look at what the array name represents, this concept may be misunderstood by the fact that the array name represents the address of the array, of course, the array name represents an address, but the key is that through this address, we are concerned about how much space it can get the value of the data. For example, for a char-type address, we can get a byte value, and for an int-type address, we can get a value of 4 bytes. OK, let me talk about the type of understanding.

Types in the <1>c language

Char a;====> A is a char type

Short B; ====> B is the short type

int C; ======> c is the int type

Char *p; =====>p is int * type

Short *p;=====>p is short * type

int *c ======> c is int * type

Therefore, we can draw a conclusion: The variable name is removed, the rest is the type.

Therefore, our array can be said to be a special type. As an example of an array of type int, say we

of a and &a.

For example: int a[4];

As we all know, a is an address. In fact, A's original face should be a + 0, but, this 0 we often

omitted, * (A + 0) indicates what. , this is the data that represents the first element we get, namely a[0].

Therefore, we can understand that a represents the address of the first element. Which is the address of a[0]. Therefore, we have drawn a

Conclusion: The array name of the array represents the first address of the first element of the array.

int A[4] Array, the data type of a[0] is of type int, and its address should be saved with int *.

So the type of a should be saved with an int * type.

<2>&a Address Detailed

Understanding A, we have to understand &a, relatively easy. Just said, we remove the variable name, the rest is our type. or an array of type int as an example.

For example:

Char A; ===>&a represents our address. &a + 11 times to move a char type

Size

Short A; ===>&a represents our address. &a + 11 times move a short type

The size

int A; ===>&a represents our address. &a + 11 times to move an int type of

Size

similarly: int a[4] ===> The type of an array is int [4];&a is the address of the entire variable that we int [4]. Put

Int [4] as a whole, &a + 11 times moves know the size of the entire array, which is 20 words

Section. &a can understand the real address of a group.

Through the above analysis, &a is good to understand, he is the address of variable A, which is scoped to four int space, that is, if the type of &a assigned to a variable p,p should be: Int (*) [4]; that's a pointer to an array that contains 4 int data. , this is called an array pointer.

Looking at the difference between a+1 and &a+1, a+1 represents the first element address of the array, and &a+1 represents the next address across array a.

The first level pointer variable in C language

1, first-level pointer variable

Essence: Used to save our address.

The method defined:

Data type * variable name;

int *p;

Thinking: How to read and write a piece of memory through a pointer variable.

int data = 10;

int *p = &data;

Write operations

*p = 40;

Read operations

printf ("%d\n", *p);

Rule: On a 32-bit machine, all pointer variables are 4 bytes in size

Thinking: C language Why to design different types of pointer variables.

Different types of pointer variables in the C language are addressed in the same range.

Practice: int A[5] = {1,2,3,4,5};

int *p;

Ask P to point to our array, and to find all the data elements of the array.

#include <stdio.h>

int main ()
{
	int a[5] = {1,2,3,4,5};
	int *p = A;
	int i = 0,sum = 0;
	for (i = 0;i < sizeof (a)/sizeof

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.