Some small exercises

Source: Internet
Author: User

1.
Pointer Parameters

Void F (char * P)

{

P = (char *) malloc (10 );

}

Int
Main ()

{

Char * P = NULL;

F (p );

Free (P );

Return
0;

}

Free (null) is acceptable, but the pointer variable for function parameters is still one-way value transfer, and the P pointer is still 0, so the memory is leaked.

2. character array

Char S1 [24] = "How are u ";

Char S2 [24] = "how are you ";

Int I, Len = strlen (S2 );

For (I = 0; I <Len; I ++)

S1 [I] = S2 [I];

Printf ("S1 = % s S2 = % s/n", S1, S2 );

Both S1 and S2 are displayed normally, because S1 is followed by auto-completion 0.

3. Void pointer Conversion

Class

{

Public:

A (): A (2), P (void *) 5)

{

Printf ("A = % d p = % d/N", A, P );

}

Int;

Void * P;

};

Int main ()

{

A B;

}

Here, P must be forcibly converted with (void *), but the value of P is 5. Referencing it will affect the system.

4. heap memory, data segment, and code segment

# Include <stdio. h> char * F1 () {char a [] = "hello,
World !! "; Return ;}

Char * F2 () {char * A = "hello,
World !! "; Return a;} Char * F3 () {static char * A =" Hello, world! "; Return a;} void main () {char * S1; char * S2; char * S3; S1 = F1 (); S2 = F2 (); s3 = F3 (); puts (S1); puts (S2); puts (S3);} a program is in the memory in the ascending order of the memory address, contains code segments, constant segments, data segments,
Heap memory and
Stack memory

 

Code segment

Machine code used to store programs

Constant segment

Used to store all string constants

Data Segment

Used to store global variables and static variables (this segment is automatically cleared after the program is loaded, which is why the initial values of global variables and static variables are both 0)

Code segment, regular
Bulk and number
The data segment memory is the same as the life cycle of the program. The memory will be released only after the program ends. The data segment is further divided into initialized data segments.

And uninitialized data segments

,
Uninitialized data segments do not affect the size of the target file.

Heap memory

In the program, the memory blocks applied for by memory sub-functions such as malloc are allocated from the top of the Data Segment and from the low address to the high address. The free and other memory release functions must be called to release unused memory.

Stack memory

Stores the temporary variables in the function. Each time the program enters a function, it allocates a piece of memory from the stack memory, which is equal to the size of the temporary variables in the function. When the function ends, the memory is automatically released. Stack memory from
High addresses are allocated to low addresses.

Heap memory and stack
Inner
After allocation, it will not be re-initialized, So what data is stored in the memory during allocation and what data is in the memory you get. This is why the temporary variables and the data in malloc's internal output are
Machine Value
.

Next let's take a look at the several situations mentioned above: 1). Char A [] =
"

Hello, world! ";

A is a local character array variable. The array size is 13, and the elements in the array are initialized as "Hello, world! /0

".

A is stored in the stack memory of function F1, return
A returns the first address of. After the F1 function is completed, the stack memory of F1 is released. The memory address of a may be moved for use. Therefore, it is unknown what is put in that address.
Puts outputs the content of the address to obtain random results. 2

).
Char * A =


"

Hello, world! ";

A is a char * pointer variable with a size of 4 bytes. Its value is"

Hello, world! "The first address of the constant string.

"

Hello, world! "The constant string is saved in the constant segment and cannot be changed.

A is stored in the stack memory of function F1, return
A returns the value of pointer A, that is
"

Hello, world! "The first address of the constant string. After the F1 function is completed, a is released, but
The content of the constant segment to be pointed to has not changed. Therefore, puts output is correct.

3

). Static char a [] =
"

Hello, world! ";

A is a static character array variable. The array size is 13, and the elements in the array are initialized as "Hello, world! /0

".

A is stored in the data segment, return
A returns the first address of. After the F1 function is complete, the memory in which a is located will not be released. Therefore, puts outputs the content in this address to obtain the string stored in.

5. Single-chain table reversal

Struct tagt

{


Int data;


Struct tagt * next;

};

Typedef struct tagt;

T * revert (T * head)

{


T * P = head, * pnext = NULL, * newhead = NULL;

While (P! = NULL)

 
{


Pnext = p-> next;


P-> next = newhead;


Newhead = P;


P = pnext;

}

Return newhead;

}

T * insert (T * head, int data)

{


T * newnode = (T *) malloc (sizeof (t ));


Memset (newnode, 0, sizeof (t ));


Newnode-> DATA = data;


Newnode-> next = NULL;

T * P = head;


If (null = P)


{


/* No one node */


Return newnode;


}


Else


{


/* Point to end */


While (p-> next! = NULL)


{


P = p-> next;


}


/* Insert */


P-> next = newnode;


Return head;


}

}

Void print (T * head)

{


T * P = head;


While (P! = NULL)


{


Printf ("% 2D", p-> data );


P = p-> next;


}


Printf ("/N ");

}

Int main ()

{


T * head = NULL;


Head = insert (Head, 1 );


Head = insert (Head, 2 );


Head = insert (Head, 3 );


Print (head );

Head = revert (head );


Print (head );

Return 0;

}

6. multiply two large numbers

Char * multi (char * a, char
* B)
{
Int n = strlen (A), M = strlen (B );
Int I = 0, j = 0;
Int temp = 0;
Int * c = (int *) malloc (sizeof (INT) * (N + M ));
Memset (C, 0, (n + M) * 4 );

For (I = 0; I <n; I ++)
For (j = 0; j <m; j ++)
C [I + J + 1] + = (a [I]-'0') * (B [J]-'0 ');

For (I = N + m-1; I> = 0; I --)
{
If (C [I]> = 10)
{
C [I-1] + = C [I]/10;
C [I] = C [I] % 10;
}
}

Char * P, * result = (char *) malloc (n + m + 1 );
Memset (result, 0,

N + m + 1

);
I = 0;
While (C [I] = 0)
I ++;
P = result;
For (; I <n + m; I ++, P ++)
(* P) = (C [I] + '0 ');
Return result;

}

7. Use preprocessing commands

# Define
Declare a constant to indicate the number of seconds in a year (ignore the leap year problem)

# Define seconds_per_year (60*60*24*365) UL

8. Write a "standard" macro min

This macro
Enter two parameters and return a smaller value.

# Define min (A, B) (a) <= (B )? (A): (B ))

Let's look at the following example. What is the output?

# Define max (A, B) (a)> = (B )? (A) (B ))

Int B = 3, A = 4;

Int * P = &;

Int least = max (* P) ++, B );

Printf ("least = % d A = % d B = % d/N", least, a, B );

The result is, 5, 6, 3, which is the side effect of macro definition. The three-object operator can be regarded as two expressions.

Add A_1 and assign it to least. In addition, the * P ++ expression is above *, so brackets are required.

9.

What does the keyword volatile mean?

Three different examples are provided.

Is that this variable may be unexpectedly changed,

The following are examples of volatile variables:
Sub:

1). Hardware registers of parallel devices (for example, Status Registers)

2). A non-automatic variable (non-automatic
Variables)

3) variables shared by several tasks in multi-threaded applications

Int
Square (volatile int * PTR)

{

Return * PTR ** PTR;

}

The compiler will generate code similar to the following:

Int
Square (volatile int * PTR)

{

Int A, B;

A = * PTR;

B = * PTR;

Return
A * B;

}

It should be

Long
Square (volatile int * PTR)

{

Int;

A = * PTR;

Return a *;

}

10.

Bit operation

Write two pieces of code. The first one is to set the bit of.
3. The second one clears bit 3 of. In the preceding two operations, you must keep the other bits unchanged.

# Define bit3
(0x1 <3)

A | = bit3;

A & = ~ Bit3;

11.

Pointer address

Set an absolute address to 0x67a9.
Type variable value: 0xaa66

Set

An integer

Convert

One pointer needs to be forcibly converted

Short * PTR;

PTR =
(

Short

*) 0x67a9;

* PTR = 0xaa55;

12.
Type Improvement

What is the output of the following code? Why?

Void Foo (void)

{

Unsigned int A = 6;


Int B =-20;

(A + B> 6) puts ("> 6"): puts ("<= 6 ");

}

If the expression contains a signed or unsigned type
All operands are automatically converted to the unsigned type. Therefore,-20 is a very large positive integer, so the result calculated by this expression is greater than 6.

13. Evaluate the following code snippet

:

Unsigned int zero = 0;

Unsigned int compzero = 0 xFFFF;

/* 1's
Complement of zero */

For a 16-bit processor of the int type, the above Code is incorrect. It should be written as follows:

Unsigned int compzero = ~ 0;

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.