Interview preparation of the C / C ++ basics

1. The transmission value, and passed by reference address transfer

(1) the value transfer

Only the values ​​of the parameters passed, the parameter is a function of the operation, no operation arguments,

Function declaration:

void swap(int px,int py)
{
      int tmp = px;
      px = py;
      py = tmp;             
}
Function call:
int a,b;
swap(a,b);

(2) transfers the address

Delivery address, the address operation is transmitted, the function will change the internal argument.

Function declaration:

void swap(int *px,int *py)
{
      int tmp = *px;
     * px =* py;
      *py = tmp;             
}

Function call:

int a,b;
swap2(&a, &b);

(3) passed by reference

Alias ​​variable is referenced, the address operation is transmitted, the function will change the internal argument.

Function declaration:

void swap(int px,int py)
{
      int tmp = px;
      px = py;
      py = tmp;             
}

Function call:

int a,b;
swap2(a, b);

2. pointers and references

Reference is an alias object can not be null, define a reference, you must initialize

Reference can not be changed hands, more secure,

* Difference 3.const int p and int * const p's

(1)const int *p

Constant pointer is referred to as a division boundary *, const left, the pointer indicates the data is a constant.

* P is a constant, * p can not operate as a left value,

(2)int * const p

He referred pointer constant, indicating that the pointer itself is a constant,

p is a constant.

4.C in memory of the five regional

(1) Stack

Assigned when necessary, automatically cleared when not needed variables storage area, storing local variables and function

(2) heap

new allocated memory block, and delete corresponding

(3) free storage area

malloc allocated memory area, and it is very similar to heap

(4) global / static variable storage region

Global and static variables storage area

(5) Constant storage area

Constant storage area

The focus here distinguish between the stack and the free storage area, is free store in C ++ by new and delete dynamic allocation and release objects of abstraction, while the heap is the C language and operating system terminology, is the operating system maintains a dynamic allocation of memory.

5.C ++ class static member variables in an ordinary member variables What is the difference? Static function? 

(1) member variables

static member variables no matter how many objects you create only the only variable is assigned only one address.
Ordinary members of the variables you create an object have a copy.

(2) function

Not belong to the class object.

Without defining the object can be called, but can only call the static member.

Static member functions can not access non-static class members.

6. TCP UDP difference with the

(1) is connected

TCP connection, three-way handshake,

UDP no connection for radio,

(2) Security

TCP reliable data security

UDP does not guarantee reliable

(3) transmission efficiency

TCP transmission efficiency is relatively low.

UDP high transmission efficiency for a higher communication or broadcast communication and high-speed real-time transmission.

Number (4) connection object

TCP connection can only point to point, one to one.

UDP support one to one, one to many, and many-to-many interactive communication.

 7. the memcpy function, to achieve the memset, the code Shredded

Operations are byte, it is important

(1)memcpy

memcpy memory resource (src points to memory) copied to the target memory (memory pointed desc)

void *Mem_copy(void *dst,const void *src, int n)
{
    if (dst == NULL || src == NULL || n < 1)
        return NULL;
    char *Dst = (char *)dst;
    char *Src = (char *)src;
    while (n > 0)
    {
        *Dst++ = *Src++;
        n--;
    }
    return dst;
}

(2)memset

memset are byte operations, which is the focus, it is the fastest method of clearing the operation of the larger structures or arrays.

void *Mem_set(void *dst, int val, int n)
{
    if (dst == NULL || n < 1)
        return NULL;
    char *Dst = (char *)dst;
    while (n > 0)
    {
        *Dst++ = val;
        n--;
    }
    return dst;
}

To be continued. . .

Guess you like

Origin www.cnblogs.com/jxLuTech/p/11072765.html