A collection of small interview questions

Source: Internet
Author: User
Tags naming convention

About Makefile:

How to generate a dynamic-link library and a static-link library, and what is the purpose of generating these libraries.

Both the static library and the dynamic library are created by the. o file.

The naming convention for static library file names is prefixed with Lib, followed by a static library name, with the extension. A.

For example, we will create a static library named Myhello, then the static library file name is LIBMYHELLO.A. You need to be aware of this when creating and using static libraries. Create a static library with the AR command.

Type the following command at the system prompt to create the static library file LIBMYHELLO.A.

# ar CRV libmyhello.a hello.o

The dynamic library file name specification is similar to the static library file name specification, and also adds prefix lib to the dynamic library name, but its file name extension is. So.

For example, we will create a dynamic library named Myhello, then the dynamic library file name is libmyhello.so. Use GCC to create a dynamic library.

At the system prompt, type the following command to get the dynamic library file libmyhello.so.

# Gcc-shared-fpic-o libmyhello.so hello.o

You can also create a dynamic library directly from the. c File: Gcc-shared-fpic-o libmyhello.so hello.c

We still use the LS command to see if the dynamic library file is generated.

How to use a static link library.

The static library is finished, how to use its internal functions. You only need a prototype declaration that contains these common functions in the source program that uses them, and then indicates the static library name when the target file is generated with the GCC command, and GCC will connect the common function to the destination file from the static library. Note that GCC appends the static library name with the prefix lib, and then append the file name of the static library that the extension. A finds to the static library file.

# Gcc-o Test Test.c-l.-lmyhello

Above reference: http://blog.csdn.net/dingyuanpu/article/details/5788379

Makefile Command Parameters-fpic what the function is.

When you use a library of. So, when you encounter multiple executables that share this library, in memory, the library will not be copied multiple copies, so that each executable file is used one-to-one, instead of having multiple executables pointing to a library file for common use. Purpose: To save the memory space, improve the space utilization.

Reference: http://blog.csdn.net/azr22005/article/details/6678914

Makefile Command Parameters-shared what the function is.

-shared This option specifies that a dynamic connection library is generated (which allows the connector to generate an export symbol table of type T, and sometimes a weakly connected W-type export symbol) without which the external program cannot connect. Equivalent to an executable file

grep command using a small description

grep is a filter,
It can filter out rows that have a string from one or more files.
or from standard input
Filters out rows that have a string.
Fgrep a bunch of strings that you want to filter in a file,
And then use Fgrep will contain the words that belong to this group
The rows of the string are filtered out.
The use of grep and Fgrep is as follows:
grep [-NV] match_pattern file1 file2 ....
Fgrep [-NV]-F pattern_file file1 file2 ....
-n lists row numbers before rows are found
-V put the row that does not contain Match_pattern
Match_pattern the string to search for
-F to Pattern_file store the string to search for


Features and advantages of dynamic link library.
First, let's take a look at the benefits of deferring library functions to load during program run time:
1. Resource sharing between processes can be achieved.
What kind of concept. That is, when a program is running to invoke a dynamic link library function, the operating system first looks at all the running programs to see if there is a copy of the library function in memory. If so, let it share that copy; only the link is not loaded. Such a pattern, although it will bring some "dynamic link" additional overhead, but greatly save the system's memory resources. The standard library of C is the dynamic link library, which means that all the programs running in the system share the code snippets of the same C standard library.
2. Upgrading some programs becomes simpler. Users only need to upgrade the dynamic link library without recompiling the other original code of the link to complete the entire program upgrade. Windows is a good example.
3. Even the actual link loading is completely controlled by the programmer in the program code.
When programmers write a program, they can specify when or where the link loads the dynamic link library function. You can have a fairly large software, but each time it runs, only a small number of programs are loaded into memory due to different operational requirements. All functions are based on the principle of "the need to be transferred", thus greatly saving the system resources. For example, today's software is usually able to open several different types of files, these read and write operations are usually implemented using dynamic link library. In one run, only one type of file is normally opened. So until the program knows the type of file and then loads the corresponding read and write function instead of loading all the read and write functions in the first place, then it finds that they are not used throughout the program.


What is the difference between a global variable and a static local variable.

The storage is the same, except that they differ in scope:
Global variables can be seen almost anywhere in the program
A static local variable can only be used within its specified range.

Like what

int i;  Global variable

class C
{public
:
    static int i_c;  Static local variable
}

void Main ()
{
    i = 1;        Legal
    I_c = 1;      Error
    C::i_c = 1;   Legal
    
    C cc;
    Cc.i_c = 2;  Valid
}

Global, the startup code helps initialize before main and WinMain entry points.

Local, initialized at the declaration.


A global variable is accessible to the code after it has been defined.
A static variable is accessible only in the module in which the variable is defined.
The scope of the static variable is within this file and cannot be extended to other files.


Although global and static local variables are stored in a static storage mode, however, the scope of global variables is from the start of the definition to the end of the file, all functions within the scope are visible, that is, all functions within the scope can be used, while static local variables are stored in a static way, However, its scope is limited to defined functions and is not visible to other functions and therefore cannot be used.


Variables can be divided into global variables, static global variables, static local variables, and local variables
By storage area: Global variables, static global variables, and static local variables are stored in the global data area of memory, local variables are stored in the memory stack area
Divided by scope: Global variables are valid throughout the engineering document; A static global variable is valid only in the file in which it is defined; a static local variable is only valid within the function that defines it, except that the program allocates only one memory, and the function returns, the variable does not disappear;


global variables and static variables are initialized to 0 by the compiler if they are not initialized manually. The value of the local variable is not known.


The code is as follows:

Test.cpp
#include <iostream.h>
extern int a;//variable declaration, not defining
static int b=5;
void Func1 ()
{
  cout<<b<<endl<<a<<endl;//result is 5
}


//main.cpp
int A;
int b=10;
void Func2 ()
{
  static int i;
  i++;
  cout<<i<<endl;
}


void Func1 ();


void Main ()
{
  a=20;
  Func1 ();
  Func2 ();
  Func2 ();
}


Output results are
5
20
1

2


the difference between new and malloc.


1,malloc and free are standard library functions for c++/c languages, and new/delete are the operators of C + +. Both can be used to request dynamic memory and free memory.


2, for objects that are not internal data types, Maloc/free cannot meet the requirements of dynamic objects.

Objects are created with the constructor automatically executed, and the destructor is automatically executed before the object dies.

Because Malloc/free is a library function and not an operator, it is not possible to impose the task of executing constructors and destructors on Malloc/free without the compiler controlling permissions.


3, the C + + language requires a new operator to complete the dynamic memory allocation and initialization work, with an operator delete that cleans and frees up memory work. Note that New/delete is not a library function.

4,c++ programs often call C functions, while C programs can only manage dynamic memory with Malloc/free


New is an operator, and what "+", "-", "=" ... Have the same status.

Malloc is a function of allocating memory for you to call.

New is a reserved word and does not require header file support.
malloc requires header file library function support.


New creates an object,
malloc allocates a piece of memory.

New object you can use as an ordinary object, access by member function, do not directly access its address space
malloc allocates an area of memory that is accessed with pointers, and can also move pointers inside.


Nutshell:
New is an operator that can overload
Malloc is a function that can overwrite
New initializes the object, calls the object's constructor, and the corresponding delete calls the corresponding destructor
malloc only allocates memory, free only reclaims memory


Above from: http://www.cnblogs.com/sukairui/archive/2007/10/19/931001.html


the method of constructing the hash function.

(1) method of square-taking
(2) The method of removing remainder
(3) Multiplication and rounding method
(4) Random number method

above from: http://student.zjzk.cn/course_ware/data_structure/web/chazhao/chazhao9.4.2.htm

the difference between a declaration and a definition.


A statement is a definition, unless
-it declares a function that does not specify the function body in detail.
-it contains an extern definition character and does not initialize or function body
-it is a declaration of data members contained within a class
-it is a declaration of a class
-it's a typedef statement


A definition is a declaration, unless
-it defines a static member function
-it defines a member function

Above from: http://bbs.csdn.net/topics/80311941

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.